Esteemed Monks,

I am using Class::DBI in a webapp. I have a table "block" which can have up to 14 children in a table "adslot" Here is the relevant portion of my Class::DBI module:

package admt::cdbi::adslot; use base 'cdbi'; __PACKAGE__->set_up_table('adslot'); __PACKAGE__->has_a('blockid' => 'admt::cdbi::block'); package admt::cdbi::block; use base 'cdbi'; __PACKAGE__->set_up_table('block'); __PACKAGE__->has_many('adslot' => ['admt::cdbi::adslot', 'blockid']);
And here is the relevant SQL for the table structures:
CREATE TABLE `adslot` ( `id` bigint(20) unsigned NOT NULL auto_increment, `blockid` bigint(20) NOT NULL default '0', `sl_position` int(11) NOT NULL default '0', `sl_keywords_EN` varchar(255) NOT NULL default '', `sl_keywords_FR` varchar(255) NOT NULL default '', `sl_start` datetime NOT NULL default '0000-00-00 00:00:00', `sl_end` datetime NOT NULL default '0000-00-00 00:00:00', `sl_section` varchar(255) NOT NULL default '', `sl_smallimage_EN` varchar(255) NOT NULL default '', `sl_smallimage_FR` varchar(255) NOT NULL default '', `sl_smallimagealt_EN` varchar(255) NOT NULL default '', `sl_smallimagealt_FR` varchar(255) NOT NULL default '', `sl_smallimagelink_EN` varchar(255) NOT NULL default '', `sl_smallimagelink_FR` varchar(255) NOT NULL default '', `sl_shortdesc_EN` varchar(255) NOT NULL default '', `sl_shortdesc_FR` varchar(255) NOT NULL default '', `sl_shortdesclink_EN` varchar(255) NOT NULL default '', `sl_shortdesclink_FR` varchar(255) NOT NULL default '', `sl_depcity` text NOT NULL, `sl_deal` varchar(255) NOT NULL default '', `sl_iframe` varchar(255) NOT NULL default '', PRIMARY KEY (`id`) ) TYPE=MyISAM; CREATE TABLE `block` ( `id` bigint(20) unsigned NOT NULL auto_increment, `blk_key` varchar(32) NOT NULL default '', `blk_start` datetime NOT NULL default '0000-00-00 00:00:00', `blk_end` datetime NOT NULL default '0000-00-00 00:00:00', `blk_desc` varchar(80) NOT NULL default '', `blk_notes` varchar(255) NOT NULL default '', `blk_app` varchar(32) NOT NULL default '', `blk_app_date` date NOT NULL default '0000-00-00', `blk_app_by` varchar(32) NOT NULL default '', PRIMARY KEY (`id`) ) TYPE=MyISAM;
Now, in my code, given that I have the 'od' in "block" I want to get the associated "adslot"'s. So I do this:
my $block = admt::cdbi::block->retrieve( 'id' => $q->param('blockid' +) ); my $slots = $block->adslot; my $otext; while ( my $slotdata = $slots->next ) { $otext .= Dumper $slotdata, "\n"; }
As expected, $slots is a Class::DBI::Iterator and looks like this:
$VAR1 = bless( { '_data' => [ { 'id' => '1' }, { 'id' => '2' } ], '_place' => 3, '_mapper' => [ 'blockid' ], '_class' => 'admt::cdbi::adslot' }, 'Class::DBI::Iterator' );
But when I iterate over it, $otext produces the following:
$VAR1 = bless( { 'id' => '1' }, 'admt::cdbi::block' ); $VAR1 = bless( { 'id' => '1' }, 'admt::cdbi::block' ); ;
Which tells me that the iterator is returning objects of the parent class, not the child class!

Can abybody help please? Where have I gone wrong?

jdtoronto


In reply to Class::DBI struggles by jdtoronto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.