[Dbix-class] select * from questions

Aran Deltac aran at arandeltac.com
Thu Apr 27 17:25:37 CEST 2006


On 4/27/2006, "Gavin Carr" <gavin at openfusion.com.au> wrote:
>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

No, its not a bad thing.  Here's what you do:

$obj->result_source->schema();

I just yesterday submitted a fix to the ResultSource docs to show that
schema is available through it.  This may be why you didn't know you
could do that.  :)

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

If there is a relationship, then you can just call the methods that your
relationship created to get at the related objects.

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

I've not looked at the docs, but to be the method name
related_resuletset implies a join.  But, more verbose docs are rarely a
bad thing.  If you can come up with some extra text that makes this a
bit more explicit I'd be happy to toss it in to the docs.

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

Thats not kosher, since DBIC _should_ handle all situations (although
there are admitedly a few things it can't do, for now).  So, in those
rare cases, you can get at it with you storage object.  Such as
$obj->result_source->storage->dbh();

>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

I can't remember, but I know somewhere in the docs there is a section
that discusses writing your own result sets.

HTH

Aran

>Thanks a lot,
>Gavin
>
>
>_______________________________________________
>List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
>Wiki: http://dbix-class.shadowcatsystems.co.uk/
>IRC: irc.perl.org#dbix-class
>SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/



More information about the Dbix-class mailing list