Re: Cross between MightHave and HasMany?

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

From: Kingsley Kerce
Subject: Re: Cross between MightHave and HasMany?
Date: 20:08 on 02 Nov 2004
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' ]);

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)

Kings

 > 
 > Also I'd like to be able to remove the relationship, without deleting  
 > either the person or the category themselves.
 > 
 > Any ideas?
 > 
 > Jess
 > 
 > -- 
 > Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/

(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