jdtoronto has asked for the wisdom of the Perl Monks concerning the following question:

Esteemed monks,

For some reason my foreign key related values are not being inflated when I retrieve them using Class::DBI.

I have two tables. One called as_dow which has this structure:

Field Type Null Key Default Extra -------- ----------- ------- ------ ---------- -------- id int(11) PRI 0 int_dow varchar(50)
and the second, pr_businesshours which looks like this:
Field Type Null Key Default Extra + ------------ ------------- ------- ------ ---------- ----------- +--- interval_id int(11) PRI (null) auto_increm +ent provider_id int(11) 0 + personell_id int(11) 0 + int_type int(11) 0 + int_dow varchar(50) + int_start time 00:00:00 + int_end time 00:00:00 + int_int decimal(10,0) 00
The values for int_dow in pr_businesshours are 0..6 corresponding to the days of the week. Likewise in as_dow the keys (id) are 0..6 with the corresponding abbreviated text versions of the days stored in int_dow.

In my sub-classing of Class::DBI I did this:

package AppSys::DBI; use Class::DBI::Iterator; use base 'Class::DBI::mysql'; AppSys::DBI->set_db('Main', 'DBI:mysql:appsys', '****', '****'); package AppSys::BusinessHours; use base AppSys::DBI; AppSys::BusinessHours->set_up_table("pr_businesshours"); AppSys::BusinessHours->has_a('int_dow' => 'AppSys::DOW'); package AppSys::DOW; use base AppSys::DBI; AppSys::DOW->set_up_table("as_dow");
But when I search for the records, then retrieve them the value for int_dow does not get inflated. I think I am not understanding something, here is a code fragment from my CGI::Application run_mode:
@ints = AppSys::BusinessHours->search( provider_id => $provider_id +, {order_by => 'int_dow'}); my @loh; #while ( my $day = $ints->next ) { foreach (@ints) { my $day = AppSys::BusinessHours->retrieve( $_->id ); my $hashref = { map { $_ => $day->get($_); } $day->columns }; push ( @loh, $hashref ); }
Any clues please brethren?

jdtoronto

UPDATED: The thought just occurred to me that possibly the relationship is not inflated if I use the get method?

Replies are listed 'Best First'.
Re: Inflating foreign key values in Class::DBI
by perrin (Chancellor) on Jan 13, 2004 at 20:47 UTC
    Your code fragment looks wrong to me. @ints is a list of BusinessHours objects, but you iterate through them and read their id() in order to re-load the same BusinessHours object. Why? (Did you not realize that $day is a BusinessHours object?)

    Then you have this line where you are using $_ and assigning to it at the same time. $day is a BusinessHours object too, and when you call $day->get($_) that will stringify to something like $day->get($_->interval_id()).

    I can't tell what you were trying to do here, but this code is almost certainly not what you meant. If you want to get the inflated object, call $_->int_dow() instead.

Re: Inflating foreign key values in Class::DBI
by castaway (Parson) on Jan 13, 2004 at 20:58 UTC
    You tables are also incorrect. If the "int_dow" field in the pr_businesshours table is meant to be a foreign key connected to the id of the as_dow table, then it needs to be the same type as that id field, ie int(11). The 'has_a' relationship is telling it that the int_dow field contains the same data as the id field, and when you look up int_dow, it should retrieve the entry in the as_dow table with the same id value.

    C.