[Dbix-class] select * from questions

Christopher H. Laco claco at chrislaco.com
Thu Apr 27 03:10:01 CEST 2006


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

I curious about the answers to your questions as well. I'm in a somewhat
related situation. I'm trying to code a Cart/Items in a way that people
can swap out schemas at will, as long as they meet certain requirements.

So far, the requirements are that it uses source_name (from current) to
present a Carts and Items resultsets, regardless of what the table/class
names actually are, and that the Carts resultset has a relationship
called "items".

Right now, I'm torn as to whether I really need to force the "items"
relationships existatnce, or if I can simple use the "Items" resultset
to find() the relevant items.

That basically depends on the lesser of evils... is it better to use
relationships, or related_resultsets?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: OpenPGP digital signature
Url : http://lists.rawmode.org/pipermail/dbix-class/attachments/20060426/14d00515/attachment.pgp 


More information about the Dbix-class mailing list