Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Class::DBI::Iterator and overriding accessors

by HuckinFappy (Pilgrim)
on Aug 08, 2006 at 18:58 UTC ( [id://566224]=perlquestion: print w/replies, xml ) Need Help??

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

Knocking meekly on the gates the lowly monk creeps forward, hoping his brethren can help save his sanity.

We're learning about Class::DBI, and have mocked up some samples to see if it meets our needs. So far, it all seems excellent, but I've hit a snag I can't understand.

The issue seems to be some interaction between many to many relationships and overriding the standard accessor names. In the base class that derives from Class::DBI (which all our other classes then derive from), we have the following:

sub accessor_name { my ($_class, $_column) = @_; my $_accessor = $_column; $_accessor = ucfirst $_accessor; $_accessor = 'Get' . $_accessor; $_accessor =~ s/id$/Id/smx; return $_accessor; } sub mutator_name { my ($_class, $_column) = @_; my $_mutator = $_column; $_mutator = ucfirst $_mutator; $_mutator = 'Set' . $_mutator; $_mutator =~ s/id$/Id/smx; return $_mutator; }
This makes all accessors and mutators follow our standard naming convention. So far, so good, it works as expected.

Now we have a many to many relationship between classA and classB. This is done using a table tableClassAtoB. The problem comes when we do the following:

my $classBItr = $classA->classBs(); while ( my $b = $classBItr->next() ) { ... }
When the next() is executed, we get the error:
Can't locate object method "classB_id" via package "ClassAtoB" at /usr +/lib/site_perl/5.8.5/Class/DBI/Iterator.pm line 77.
Due to how we've overriden the accessor name, it's clear to me that the method classB_id doesn't exist. We've used the Class::DBI functionality to change it to ClassB_Id(). But apparently Class::DBI::Iterator does not know that.

What are we doing wrong? Do we need to subclass the Class::DBI::Iterator and create our own?

Thanks Monks!

Replies are listed 'Best First'.
Re: Class::DBI::Iterator and overriding accessors
by jdtoronto (Prior) on Aug 08, 2006 at 20:36 UTC
    HuckinFappy,

    You have given us enough code to tease us, but not enough to be truly illuminating! As you know Clas::DBI doesn't implement a MANY-TO-MANY relationsuip, it is done with the external link table. Have you checked the definitions of the 'has_many' relationships to make sure that the accessors have been properly specified there?

    As for the iterator - have you tried the same relationships without the customised accessor/mutator names? How did that work?

    The Class::DBI::Iterator is very simple and at a glance if the accessor/mutator methods are all correctly in place then the iterator should deal with it. jdtoronto

      Bingo! It was an incorrect definition of the has_many relationship. Thank you very much! :)
Re: Class::DBI::Iterator and overriding accessors
by perrin (Chancellor) on Aug 08, 2006 at 21:38 UTC
    If you're just starting a new project, I'd recommend you look at Rose::DB::Object or DBIx::Class instead. Both have better support for more complex relationships and generate more efficient SQL.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://566224]
Approved by Limbic~Region
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2024-03-28 13:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found