[Dbix-class] select * from questions

Gavin Carr gavin at openfusion.com.au
Thu Apr 27 02:44:09 CEST 2006


Hi guys,

Just starting to use DBIx::Class in anger for the first time and have 
a few questions/comments:

1. Is there a way of accessing the $schema object from a Row or ResultSet
method, or is that a Bad Thing to do? For instance, say per the cookbook
I'm overriding Row new() to set default values, but I want to source other 
parts of the database for some of these values e.g. a config table. So I 
want something like (contrived example):

  sub new {
    my ($class, $attrs) = @_;

    my $default_domain = $class->schema->resultset('Config')->find(name => 'default_domain');
    $attrs->{email} ||= $attrs->{username} . "\@$default_domain";

    $class->next::method($attrs);
  }

Note that in this case there's no explicit relationship between the table 
I'm using and the table I want to source. What about the case where there
_is_ a relationship?


2. Trying to do something similar from a ResultSet object I tried to use
related_resultset. From the ResultSet perldocs I assumed these were
equivalent:

  $artist_rs = $schema->resultset('CD')->related_resultset('Artist');
  $artist_rs = $schema->resultset('Artist');

i.e. related_resultset was just a resultset constructor using a 
relationship name instead of a table name. Clearly that's wrong, and a
'related' resultset involves a join from the original table. This probably
needs to be spelled out more clearly in the docs for both related_resultset
and search_related. I'm not clear enough about the semantics to have a stab
at it myself, but at least the fact that it's a joined resultset should
probably be made explicit.


3. For situations where you have queries that aren't supported by DBIC yet,
is there a way to source the underlying Schema dbh for doing statement
handles directly, or this not kosher? There's nothing obvious in 
DBIx::Class::Schema or DBIx::Class::Storage::DBI.


4. Is there an equivalent idiom for overriding ResultSet methods as 
there is for Row methods in (1) above? I tried something like this:

__PACKAGE__->load_components(qw/ ResultSetManager Core /);

  sub search : ResultSet {
      my ($self) = shift;
      my ($cond, $attrs) = @_;

      $attrs->{order_by} ||= 'name DESC';
 
      $self->next::method($cond, $attrs);
  }

but that generates a Class::C3 error:

  No next::method 'search_like' found for 44285 at /usr/lib/perl5/site_perl/5.8.8/Class/C3.pm line 192


Thanks a lot,
Gavin




More information about the Dbix-class mailing list