[Dbix-class] Relationship to the same table

Charlie Garrison garrison at zeta.org.au
Tue Jan 5 04:46:01 GMT 2010


Good afternoon,

On 4/01/10 at 2:04 PM -0600, Pavel O. Karoukin 
<hippich at gmail.com> wrote:

>I've read  manual on joining, but still can't get how i can do that. For
>example here is table (simplified):
>
>CREATE TABLE  things (
>id INTEGER PRIMARY KEY,
>type INTEGER
>);
>
>How I could run this query with DBIx?
>
>SELECT t1.*, t2.* FROM things as t1 JOIN things as t2 ON t1.type <> t2.type

Could you share some of the code you've tried, specifically how 
you've got your schema class(es) defined. Otherwise there is too 
much mind-reading involved.

And as far as I'm aware, there is no '<>' condition for DBIC 
relationships. So you will need to join on something else and 
then create a custom resultset method which adds the "t1.type <> 
t2.type" search constraints. Or don't even bother with a 
relationship, just use a custom method in your schema class 
which returns a resultset with search criteria like:

$self->schema->resultset('MyClass')->search({ type => {'<>' => 
$self->type} });

Note, that syntax is just from memory, may not be valid.

I have found DBIC relationships to be a great building block, 
but the real power comes from custom methods. And DBIC has been 
great at teaching me how great the M part of MVC can be. My 
controllers have gone on a serious diet. :-)

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