Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Add a method to a ResultSet Class in DBIx::Class?

by castaway (Parson)
on Jan 09, 2008 at 14:35 UTC ( #661362=note: print w/replies, xml ) Need Help??


in reply to Add a method to a ResultSet Class in DBIx::Class?

Almost there:
__PACKAGE__->resultset_class('ThreadedDB::Article::ResultSet'); ## uncomment these package ThreadedDB::Article::ResultSet; use base 'DBIx::Class::ResultSet';
(And don't forget that $self isa ResultSet after that)

Done.

C.

Replies are listed 'Best First'.
Re^2: Add a method to a ResultSet Class in DBIx::Class?
by jasonk (Parson) on Jan 09, 2008 at 18:20 UTC

    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:

    # lib/MyDB.pm package MyDB; use strict; use warnings; use base qw( DBIx::Class::Schema ); __PACKAGE__->load_namespaces( default_resultset_class => 'ResultSet', ); 1; # lib/MyDB/ResultSet.pm package MyDB::ResultSet; use strict; use warnings; use base qw( DBIx::Class::ResultSet ); 1; # lib/MyDB/Result.pm package MyDB::Result; use strict; use warnings; use base qw( DBIx::Class ); __PACKAGE__->load_components(qw( FormFu InflateColumn::DateTime UUIDColumns Core )); 1;

    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.

    # lib/MyDB/Result/Article.pm package MyDB::Result::Article; use strict; use warnings; use base qw( MyDB::Result ); __PACKAGE__->table( 'articles' ); __PACKAGE__->columns(qw( updated_time created_time ));

    Now, if I do this:

    my $schema = MyDB->connect( @connection_info ); my $article = $rs->schema( 'Article' )->new({});

    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.

    # lib/MyDB/ResultSet/Article.pm package MyDB::ResultSet::Article; use strict; use warnings; use base qw( MyDB::ResultSet ); sub insert_article { my ($self, $topic, $parent, $msgtext) = @_; eval { $self->txn_do( sub {} ) }; } 1;

    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...


    We're not surrounded, we're in a target-rich environment!
Re^2: Add a method to a ResultSet Class in DBIx::Class?
by matija (Priest) on Jan 09, 2008 at 16:22 UTC

    That's IT!

    It works now.


    Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://661362]
help
Chatterbox?
[1nickt]: marioroy Yes, I am using it with MCE, as is Discipulus I believe. I was trying to work out how to make a cpanfile that would be smart enough to know which deps to require.
[1nickt]: See this code. (I expected to simply eval loading threads as a check, but weirdness happened with Perlbrew so it's a grep of -V ...)
[choroba]: Config might be better than grepping -V
[Corion]: Also see Config::V, which is less of that hackery, or that hackery hidden in a module ;)
[1nickt]: The problem was with Perlbrew
[Corion]: Whoops - Config::Perl::V
[1nickt]: I found that when using Perlbrew as recommended, with cpanminus in the system perl lib, such tests were failing to detect the data about the perl that was the install destination.

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (11)
As of 2017-10-18 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (249 votes). Check out past polls.

    Notices?