[Catalyst] How do I access 'Catalysed' custom datasets?

Ian Sillitoe ian at sillit.com
Tue Mar 18 19:40:45 GMT 2008


This may be more of a DBIx::Class issue - if so, apologies - however I think
it come down to my lack of understanding of exactly what Catalyst is doing
when it loads DBIx::Class modules hence why I've posted this problem to the
catalyst mailing list.

I need to do some sub-selects in DBIx::Class so I've written a function that
returns a custom resultset depending on some variables - if the resultset
hasn't already been created then it creates and registers this resultset
before returning it (based on DBIx::Class::Manual::Cookbook - "Arbitrary SQL
through a custom ResultSource").


package MySchema::Domain;

use base 'DBIx::Class';

# I'm using a 'static' custom resultset in addition to the 'dynamic' ones
__PACKAGE__->resultset_class('MySchema::Domain::ResultSet');

__PACKAGE__->load_components("Core");
__PACKAGE__->table("domain");
__PACKAGE__->add_columns();

sub get_custom_resultset {
  my ($from, $to) =3D @_;
  my $result_source_name =3D "ResultSetFrom${from}To${to}";
  my $result_source;

  # check if we haven't already made this
  if ( grep { $reps_source_name eq $_ } MySchema->sources() ) {
    $result_source =3D MySchema->resultset($reps_source_name);
  }

  if (!$result_source)
  {
    my $inner_sql_select;
    my $inner_sql_where;
    my $inner_sql_group_by;

    # some gubbins that creates the SQL fields based on: $from and $to

    my $inner_sql =3D <<"SQL";
(
   SELECT * FROM
    (
       SELECT
            $inner_sql_select
       FROM domain
        WHERE
           ( $inner_sql_where )
        GROUP BY
           $inner_sql_group_by
        ORDER BY
           $inner_sql_group_by
    ) rep
   NATURAL JOIN domain
)
SQL

        $result_source->name( \$inner_sql );

       MySchema->register_source( $result_source_name =3D> $result_source );
    }
   return MySchema->resultset( $result_source_name );
}

When I run Catalyst I can see that I now get a bunch of new classes in
MyApp::Model (as expected)

[debug] Loaded components:
.-----------------------------------------------------------------+--------=
--.
| Class                                                           | Type
|
+-----------------------------------------------------------------+--------=
--+
| MyApp::Model::MySchema::ResultSetFrom1To2                       | class
|
| MyApp::Model::MySchema::ResultSetFrom1To3                       | class
|


And if I access this new resultset directly then everything works as
expected:

@entries =3D MyApp->model("MySchema::ResultSetFrom1To2")->search({}, { bind=
 =3D>
\@bind_values });

However, I would prefer to access the resultset via the same method that I'm
creating them from rather than having to know what these resultsets happen
to be called under the hood or worry about typos - i.e. use the method
MySchema::Domain::get_custom_resultset($from,
$to)

However, I can't get this to work as expected - the following code gives me
the error:

undef error - Can't call method "select" on an undefined value at
/opt/perl-5.8.8/lib/site_perl/5.8.8/DBIx/Class/ResultSet.pm line 514.

---

package MySchema::Domain::ResultSet;

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

sub get_results_from_x_to_y {
  my ($from, $to);
  my $resultset =3D MySchema::Domain::get_custom_resultset($from, $to);
  my @bind_values =3D ($from, $to);  # more complicated than this in reality
  return $resultset->search(
    {},
    { bind =3D> @bind_values }
  );
}

Is this something to do with setting the __PACKAGE__->resultset_class (if
so, I'll take it to the dbi-class list)? Side question - should I expect the
'catalysed' schema to behave exactly the same as the non-catalysed schema?

Appreciate any help at all - cheers,

Ian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/catalyst/attachments/20080318/c5644=
29e/attachment.htm


More information about the Catalyst mailing list