[Dbix-class] Relationship to the same table

Charlie Garrison garrison at zeta.org.au
Sat Jan 2 16:10:10 GMT 2010


Good morning,

On 2/01/10 at 12:36 AM -0600, Pavel O. Karoukin 
<hippich at gmail.com> wrote:

>Yeah, this work for one record as my example above, but I want to get more
>"generic" aproach. For example how to code schema join for the following
>query:
>
>SELECT * FROM
>things as t1
>JOIN
>things as t2
>ON
>t1.type <> t2.type
>AND
>t1.uid  <> t2.uid

Create a self-join relationship (other_things in my previous example)

__PACKAGE__->has_many(other_things => 
'MyApp::Schema::Result::Things', 'thing_id');


>If I will be able to make it joined this way then I will be able to apply
>any conditions later and not write custom accessor each time.

Since the custom method returns a resultset, you can continue to 
chain other criteria from it:

my $things = $schema->resultset("Thing")->first->different_things->search({other_cond=>'type2'});

I think you might be stuck on what relationships are in DBIC; 
they are really just very fancy glue for returning resultsets; 
you can also compose and return your own resultsets. Write 
methods that take different arguments which allow you to compose 
the desired results. Eg. the example method I gave allows the 
'type' to be passed as an argument or taken from the current 
record as a default.

Anything you are wanting to define in the %attr for 
relationships can also be defined in your custom method. There 
is some fancy glue you don't get (eg. add_to_* methods), but for 
doing different queries the technique works a treat.

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