[Dbix-class] Relationship to the same table

Charlie Garrison garrison at zeta.org.au
Sat Jan 2 06:18:54 GMT 2010


Good afternoon,

On 1/01/10 at 10:32 PM -0600, Pavel O. Karoukin 
<pavel at yepcorp.com> wrote:

>in table "things" i am storing values for each "thing" and it's "type". What
>I want to achieve - select all "thing"s which has different type from
>current one:
>
>my $things = $schema->resultset("Thing")->first()->different_things();
>
>I am trying to define this as relationship, but from code seems like $cond
>in has_many() can accept only foreign and self fieldnames and that's all. No
>conditions. It's possible to set conditions in %attr, but this is something
>different.

I'm sure someone else can answer this more accurately, but maybe 
a quick pointer will help here. Rather than creating a 
relationship, create a method. Eg:

sub different_things {
     my $self = shift;
     my $type = shift || $self->type;
     # other_things is a relationship defined in this package
     return $self->other_things->search({type=>$type}, {order_by=>'sort_idx'});
}



Charlie

-- 
    Ꮚ Charlie Garrison ♊ <garrison at zeta.org.au>
    〠 PO Box 141, Windsor, NSW 2756, Australia

O< ascii ribbon campaign - stop html mail - www.asciiribbon.org
http://www.ietf.org/rfc/rfc1855.txt



More information about the DBIx-Class mailing list