Keep It Simple, Stupid | |
PerlMonks |
Re^2: Add a method to a ResultSet Class in DBIx::Class?by jasonk (Parson) |
on Jan 09, 2008 at 18:20 UTC ( [id://661443]=note: print w/replies, xml ) | Need Help?? |
My favorite often-overlooked method of avoiding having to set resultset_class on all your resultsets is to use load_namespaces instead of load_classes in your DBIx::Class::Schema subclass. For example, I usually set my schema up like this:
This way I have a custom base class for both resultsets and results, and if I create a new result class or a new resultset class, it will just work, without having to do anything extra. This also lets you add common functionality to your base classes, like I did with loading components I use everywhere in MyDB::Result. If you add custom functionality to your base MyDB::ResultSet class, that will get used whenever you don't have a specific resultset class for a given result class, which is handy for adding things like advanced searching to all your resultsets.
Now, if I do this:
I'll get back a MyDB::Result::Article object that automatically has it's resultset_class set to MyDB::ResultSet. If I later decide I have functionality to add to the resultset just for articles (as you did), I can just create the new class and it will be detected automatically at startup.
And now when doing my $article = $rs->schema( 'Article' )->new({});, the object I get back has it's resultset_class set to MyDB::ResultSet::Article instead, since there is a specific class for it now...
In Section
Seekers of Perl Wisdom
|
|