[Dbix-class] prefetch syntax

Matt S Trout dbix-class at trout.me.uk
Fri Apr 7 11:32:46 CEST 2006


luke saunders wrote:
>     I do have another prefetch problem where I try to two-tier join the
>     same class through two different relationship tables.  I try this now:
> 
>        my $rs = $schema->resultset('Foo')->search(
>            {   'me.foo_id' => $foo_id  },
>            {   prefetch    => [
>                    { bar => 'biz' },
>                    { bee => 'biz' },
>                ],
>            },
>        );
> 
>     or equivalently:
> 
>        my $rs = $schema->resultset('Foo')->search(
>            {   'me.foo_id' => $foo_id  },
>            {   prefetch    => {
>                    bar => 'biz',
>                    bee => 'biz',
>                },
>            },
>        );
> 
>     The sql that results does something like
> 
>         LEFT JOIN bar bar ON foo.foo_id = bar.foo_id
>         LEFT JOIN biz biz ON bar.bar_id = biz.bar_id
>         LEFT JOIN bee bee ON foo.foo_id = bee.foo_id
>         LEFT JOIN biz biz ON bar.bar_id = biz.bar_id
> 
>     and DBI throws "Not unique table/alias 'biz' at script line x".

Hmm. If it's already seen the alias, then it should add _2 the second time 
around, which certainly works for e.g.

my $rs = $schema->resultset('TreeLike')->search(
              $cond,
              { prefetch => { parent => { parent => 'parent' } } } );

If it doesn't work the way you're trying it, evidently I didn't get the code 
for this *quite* right.

Any chance you could try and put together a test case against DBICTest?

-- 
      Matt S Trout       Offering custom development, consultancy and support
   Technical Director    contracts for Catalyst, DBIx::Class and BAST. Contact
Shadowcat Systems Ltd.  mst (at) shadowcatsystems.co.uk for more information

+ Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +



More information about the Dbix-class mailing list