Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Class::DBI and has_many relationship

0 views
Skip to first unread message

Matthew Keene

unread,
Jan 11, 2010, 4:44:59 AM1/11/10
to
I've noticed some slightly odd behaviour when trying to retrieve rows
through a has_many relationship in Class::DBI. I'm writing a football
tipping application, and I have a games table which has a foreign key to
a rounds table (ie a round has many games) and two foreign keys to a
teams table (ie a team has many home games and has many away games).
I've set things up like so:

my $loader = Class::DBI::Loader->new(
dsn => "dbi:SQLite:dbname=ORMTest.db",
#options => { RaiseError => 1, AutoCommit => 0, TraceLevel => 1},
options => { RaiseError => 1, AutoCommit => 0},
namespace => "Footy",
);

Footy::Games->has_a(round => 'Footy::Rounds') ;
Footy::Rounds->has_many(games => 'Footy::Games') ;
Footy::Games->has_a(home_team => 'Footy::Teams') ;
Footy::Games->has_a(away_team => 'Footy::Teams') ;

(The relationships don't load correctly via Class::DBI::Loader for
SQLite, so I set them up manually).

I then try to retrieve all of the games for a round via the round
has_many games relationship.

my $iter = Footy::Rounds->retrieve(1)->games ;
while (my $game = $iter->next) {
print $game->game,"\t" ;
print $game->home_team->name,"\t" ;
print $game->away_team->name,"\t" ;
print $game->venue,"\n" ;
}

This generates the right number of rows, but the data is wrong:

1 Richmond Carlton MCG
1 Richmond Carlton MCG
1 Richmond Carlton MCG
1 Richmond Carlton MCG
1 Richmond Carlton MCG
1 Richmond Carlton MCG
1 Richmond Carlton MCG
1 Richmond Carlton MCG

I expected to see the data for all 8 games, not the first game repeated
8 times.

I then add all of the columns to the Essential group by adding the
following statement to the setup code before trying to retrieve the
rows:

Footy::Games->columns(Essential => Footy::Games->columns) ;

I then get the result I expected:

1 Richmond Carlton MCG
2 Geelong Essendon MCG
3 Melbourne Hawthorn MCG
4 Sydney St Kilda ANZ
5 Brisbane Lions West Coast Gabba
6 Port Adelaide North Melbourne AAMI
7 Western Bulldogs Collingwood Etihad
8 Fremantle Adelaide Subiaco


Any ideas why I have to do this ? Is this a bug ?

0 new messages