Re: [CDBI] Has Many Problem

"James Hargreaves" <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.class-dbi
Message-ID <[email protected]>
Just for the record, it DOES stringify the object when using it as a
key to the hash. But I guess I would have known that, had I thought
about it :)

On 16/11/06, James Hargreaves <[email protected]> wrote:
> Thanks Oli - that's kind of what I did eventually, except I wasn't
> sure if the fixture object would be maintained if it was used as the
> key to a hash? Would this not attempt to stringify it?
>
> Anyway, the code I have used for the moment is:
>
> sub fixtures
> { my $s = shift;
>
>  my $fixtures;
>  foreach my $f ($s->home_fixtures)
>  { $fixtures->{$f->id} = $f unless defined $fixtures->{$f->id};
>  }
>
>  foreach my $f ($s->away_fixtures)
>  { $fixtures->{$f->id} = $f unless defined $fixtures->{$f->id};
>  }
>
>  return values %$fixtures;
> }
>
> Thanks for your suggestion - I can see that this might not be
> considered a bug, just a decision by the designer. I *think* I would
> have decided the other way though...
>
> Cheers
> Jay
>
> On 16/11/06, Oliver Jeeves <[email protected]> wrote:
> > James Hargreaves wrote:
> > > Hi everyone!
> > >
> > > I have two tables called fixtures and teams. A Fixture has a home_team
> > > and an away_team and hence:
> > >
> > > has_a(home_team => 'MyPackage::Team');
> > > has_a(away_team => 'MyPackage::Team');
> > >
> > > Hence a team has a list of fixtures:
> > >
> > > has_many(fixtures => 'MyPackage::Fixture');
> > >
> > > But this only returns the HOME fixtures. So I changed this to:
> > >
> > > has_many(home_fixtures => 'MyPackage::Fixture', 'home_team');
> > > has_many(away_fixtures => 'MyPackage::Fixture', 'away_team');
> > >
> > > sub fixtures
> > > { my $s = shift;
> > >  my @home_fixtures = $s->home_fixtures;
> > >  my @away_fixtures = $s->away_fixtures;
> > >  return ( @home_fixtures, @away_fixtures );
> > > }
> > >
> > > Which returns what I want, if in a rather naff way! However, I have
> > > some fixtures where the same team is entered for both home_team and
> > > away_team fields (for when the opposition is T.B.C). In this case the
> > > fixture is returned twice!
> > >
> > > Is there an easy way around this other than modifying my fixtures sub?
> > > I note that the following causes an error:
> > >
> > > has_many(fixtures => 'MyPackage::Fixture', 'home_team', 'away_team');
> > >
> > > This seems like a reasonable syntax for this situation? Though to be
> > > honest I would have thought the standard syntax would return the
> > > result set I want? The addition of a foreign key as the third
> > > parameter keys would then be used to select only home/away fixtures.
> > >
> > > Any help would be appreciated!
> > >
> > > Thanks
> > > Jay
> > >
> >
> > CDBI is certainly not without its problems (cue MST), but I don't
> > consider anything you've mentioned to actually be a problem.
> >
> > Given two relationships, I think it's a reasonable assumption that a
> > user of the class would only want the results of one of them at a time,
> > simply because there are two seperate relationships, not one.
> >
> > The solution to your problem, is to re-write your fixtures sub. I'd do
> > it like this:
> >
> > sub fixtures
> > {
> >         my $self = shift;
> >         my %fixtures = map {$_ => 1}
> >                 ($self->home_fixtures, $self->away_fixtures);
> >
> >         return keys %fixtures;
> > }
> >
> > That's just off the top of my head, I haven't tested it.
> >
> > -Oli
> >
> >
> >
> >
>
>
> --
> Bingo, bango, boshthedog.com
>


-- 
Bingo, bango, boshthedog.com
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.