[CDBI] Has Many Problem

James Hargreaves james.hargreaves at gmail.com
Thu Nov 16 16:50:51 GMT 2006

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!


Bingo, bango, boshthedog.com

More information about the ClassDBI mailing list