[Catalyst] Doing work inside the DBIx::Class model

Zbigniew Lukasiak zzbbyy at gmail.com
Wed Feb 20 06:33:12 GMT 2008


On Feb 20, 2008 4:32 AM, Cian Barclay <cianbarclay at gmail.com> wrote:
>
>
>
> On Feb 20, 2008 1:18 PM, Toby Corkindale <tjc at wintrmute.net> wrote:
> >
> >
> > On Wed, Feb 20, 2008 at 01:10:12PM +1000, Cian Barclay wrote:
> >
> > > which is usable both for Catalyst and something else? How do you
> > > get resultsets inside the model which could either be from the
> > > Catalyst model, or from the database connection if Catalyst wasn't
> > > being used?
> >
> >
> > I think you're looking for the $self->resultsource->schema method, inside
> your
> > Model?
> >
>
> I don't have one of those, do you mean $self->_source_handle->schema? Is
> it okay to use with the leading underscore?
>

He meant result_source:
http://cpan.uwinnipeg.ca/htdocs/DBIx-Class/DBIx/Class/ResultSet.html#result_source

But this is not all and it is something that begs for a FAQ entry - I
always forget how to do it.  The important thing is how to add new
methods to the ResultSet:

package ArchivedBooks;
  use base qw/DBIx::Class/;
  __PACKAGE__->table('books_archive');
  __PACKAGE__->source_name('Books');
  __PACKAGE__->resultset_class('ArchivedBooks::ResultSet');

package ArchivedBooks::ResultSet;
use strict;
use warnings;

use base qw( DBIx::Class::ResultSet );

sub advanced_search {
 my ( $self, $params, $attrs ) = @_;

$self now is a ResultSet - so you can use methods on it.

see http://search.cpan.org/~jrobinson/DBIx-Class-0.08009/lib/DBIx/Class/ResultSource.pm#resultset_class

At http://catalyst.perl.org/calendar/2007/16 I use that technique to
write an advanced_search method that is useful for turning web form
queries into DBIC queries.

-- 
Zbigniew Lukasiak
http://perlalchemy.blogspot.com/



More information about the Catalyst mailing list