Re: Cross between MightHave and HasMany?

[prev] [thread] [next] [Date index for 2004/11/02]

From: Jess Robinson
Subject: Re: Cross between MightHave and HasMany?
Date: 21:20 on 02 Nov 2004
On Tue, 2 Nov 2004 15:08:47 -0500, Kingsley Kerce  
<kingsley@xxxxxxxxxxxxxxxx.xxx> wrote:

> Jess Robinson writes:
>  > Hi folks,
>  >
>  > I have a setup similar to:
>  >
>  > create table person (
>  >     personid int primary key auto_increment,
>  >     firstname varchar(32),
>  >     initials varchar(16),
>  >     surname varchar(64),
>  >     date_of_birth datetime
>  >   );
>  >
>  > create table categories (
>  >     catid int primary key auto_increment,
>  >     category varchar(128)
>  >   );
>  >
>  > create table personcategories (
>  >      personid int,
>  >      catid int
>  > );
>  >
>  > So I've been looking for a way to say 'person might have a category,  
> via
>  > table personcategories', I can't see way to do it using existing
>  > relationship types, or am I missing something? Essentially I'd like
>  > $person->category() and $person->category($catobj) to do the right  
> thing.
>
> It seems as if a many-to-many would serve you fine.
> First, rename your last two tables to 'category' and 'personcategory'.
> Then set up a many-to-many as follows (code untested).
>   PersonCategory->has_a(personid => 'Person');
>   PersonCategory->has_a(catid => 'Category');
>   Person->has_many(categorys => [ 'PersonCategory' => 'catid' ]);
>   Category->has_many(persons => [ 'PersonCategory' => 'personid' ]);

This is actually what Im currently doing, its just slightly annoying to  
have to remember that it will return a list, and I need to grab the first  
item from that list. Since a person can only be in one category at once.  
(At least in my scenario ,)

> Now you can use
>
>   my $person = Person->retrieve(1);
>   foreach my $category ($person->categorys) {
>     ...
>   }
>   # or if in your scenario you know a person will only be in one category
>   my ($category) = $person->categorys;
>   ...
>
>   # your "might have" requirement is satisfied, as $person->categorys
>   # will yield nothing if a person has no category

> Not sure what you're after when you say: $person->category($catobj)

To be able to pass in a category object to $person->category and have it  
update PersonCategories as appropriate.. At the moment I'm first deleting  
any rows in there that contain that person_id, and then adding the new one.

>
>  >
>  > Also I'd like to be able to remove the relationship, without deleting
>  > either the person or the category themselves.
>  >
>  > Any ideas?
>  >


Jess

(message missing)

Cross between MightHave and HasMany?
Jess Robinson 19:47 on 02 Nov 2004

Re: Cross between MightHave and HasMany?
Kingsley Kerce 20:08 on 02 Nov 2004

Re: Cross between MightHave and HasMany?
Jess Robinson 21:20 on 02 Nov 2004

Re: Cross between MightHave and HasMany?
Kingsley Kerce 21:35 on 02 Nov 2004

Re: Cross between MightHave and HasMany?
Jess Robinson 22:09 on 02 Nov 2004

Re: Cross between MightHave and HasMany?
Tony Bowden 23:43 on 02 Nov 2004

Re: Cross between MightHave and HasMany?
Peter Speltz 22:06 on 02 Nov 2004

Re: Cross between MightHave and HasMany?
Kingsley Kerce 22:39 on 02 Nov 2004

Re: Cross between MightHave and HasMany?
Jess Robinson 06:35 on 03 Nov 2004

Re: Cross between MightHave and HasMany?
William McKee 14:21 on 05 Nov 2004

Re: Cross between MightHave and HasMany?
Peter Speltz 14:36 on 05 Nov 2004

Re: Cross between MightHave and HasMany?
Jess Robinson 20:06 on 05 Nov 2004

Re: Cross between MightHave and HasMany?
William McKee 03:34 on 06 Nov 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52