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

I have only done marginal work with DBIx::Class, but I think simply declaring the methods in the appropriate ResultSet subclass should work:

package ThreadedDB::Article::ResultSet; #use base 'DBIx::Class::ResultSet'; sub insert_article { my ($self, $topic, $parent, $msgtext) = @_; my $articles = $self->resultset('Article'); eval { $self->txn_do (sub { # a complex operation that is not relevant yet }) }; }

I left out the use base statement, because DBIx::Class will hopefully set up @ISA correctly when it's creating the subclasses.

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:25 UTC

    I left out the use base statement, because DBIx::Class will hopefully set up @ISA correctly when it's creating the subclasses.

    This only works automatically if you are using load_namespaces instead of load_classes from DBIx::Class::Schema, then they will be detected if they are named ThreadedDB::Result::Article and ThreadedDB::ResultSet::Article. If you aren't doing it this way, then the resultset_class will always be DBIx::Class::ResultSet, which you probably don't want to add things to...


    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 12:25 UTC

    Nope, that doesn't seem to be it:

    my $articles = $schema->resultset('ThreadedDB::Article'); DB<3> x $articles->can('create') 0 CODE(0x88b1cd4) -> &DBIx::Class::ResultSet::create in /usr/share/perl5/DBIx/Class/R +esultSet.pm:1625-1630 DB<4> x $articles->can('insert_article') 0 undef

      I forgot to say that in my first reply, sorry.

      If that namespace does not work, find out the real namespace of your resultset:

      warn ref ($schema->resultset('ThreadedDB::Article'));

      and then put your routines into that namespace, whatever it is.

        No, I should be sorry, because I forgot to say in my original submission that I did look at that:

        DB<5> x ref $schema->resultset('ThreadedDB::Article') 0 'DBIx::Class::ResultSet'

        I'm not comfortable adding my methods to that...