[Dbix-class] Complex table joins

Matt S Trout dbix-class at trout.me.uk
Tue May 22 20:35:50 GMT 2007


On Tue, May 22, 2007 at 07:48:07PM +0100, Richard Jones wrote:
> Matt S Trout wrote:
> >On Mon, May 21, 2007 at 08:06:48PM +0100, Richard Jones wrote:
> >>I also think the problem is that I haven't managed to find a way to 
> >>define a relationship in Schema::Foo to a table removed by 2 levels:
> >
> >You don't. You define a rel in bar and do
> >
> >join => { name_of_foo_to_bar_rel => 'name_of_bar_to_baz_rel' }
> >
> >then $foo->name_of_foo_to_bar_rel->name_of_bar_to_baz_rel->the_field_I_want
> >
> 
> Well, it's still not working :-(
> 
> Here's what I have:
> 
> Schema::Foo
> __PACKAGE__->belongs_to( 'bar', 'Schema::Bar', 'bar_id',
>   { join_type => 'left' } );
> 
> Schema::Bar
> __PACKAGE__->has_many( 'foos', 'Schema::Foo', 'bar_id',
>   { join_type => 'left' } );
> __PACKAGE__->belongs_to( 'baz', 'Schema::Baz', 'baz_id',
>   { join_type => 'left' } );
> 
> Schema::Baz
> __PACKAGE__->has_many( 'bars', 'Schema::Bar', 'baz_id',
>   { join_type => 'left' });
> 
> >You don't. You define a rel in bar and do
> >
> >join => { name_of_foo_to_bar_rel => 'name_of_bar_to_baz_rel' }
> 
> OK, so:
> name_of_foo_to_bar_rel = foos (has_many rel. in Schema::Bar)
> name_of_bar_to_baz_rel = bars (has_many rel. in Schema::Baz)

No. Schema::Foo defines a belongs_to 'bar'

Schema::Bar defines a belongs_to 'baz'
 
> In Controller:Foo:
> 
> join => [ { foos => 'bars' }, @other_relationship_names ],

SO

{ bar => 'baz' }

> Here's what I get:
> Caught exception in MyApp::Controller::Search->db_lookup 
> "DBIx::Class::ResultSet::pager(): No such relationship foos at ...

You're using the names backwards. Of course it doesn't work.



More information about the Dbix-class mailing list