[Dbix-class] prefetch syntax

luke saunders luke.saunders at gmail.com
Fri Apr 7 11:03:31 CEST 2006


I reckon you'd have to

On 4/7/06, Mark Hedges <hedges at ucsd.edu> wrote:
>
>
> On Fri, 7 Apr 2006, Jason Galea wrote:
>
> > not much more than a shot in the dark, but something similar to this
> worked
> > for me the other day..
> >
> >      my $rs = $schema->resultset('Foo')->search(
> >          {   'me.foo_id' => $foo_id  },
> >          {   prefetch    => [
> >                               'baz',
> >                               { bar => 'biz' }
> >                             ],
> >          },
> >      );
>
> Oh I get it.  Thanks, that works nicely.
>
> 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".
>
> Unfortunately I'm dense and I don't get the join syntax...
> would that solve this?  There are so many confusing mixes
> of scalars, arrayrefs and hashrefs I'm not sure what I'm doing.
>
> Thanks if someone has worked this out....
>
> Mark
>
> _______________________________________________
> List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> Wiki: http://dbix-class.shadowcatsystems.co.uk/
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/


As far as I can see, the only way out of it is to rename one of the two
relationships to 'biz2'  or whatever. That's essentially what you'd have to
do if you were writing the query in SQL right?

Alternatively, I think it may be possible to do what you want using the
-from attribute, but you might go mad trying. Read:
http://search.cpan.org/~mstrout/DBIx-Class-0.06000/lib/DBIx/Class/ResultSet.pm#from

Perhaps it would be nice if it were possible to assign aliases to
relationships for situations such as this:
Your::Schema::Bar->belongs_to('biz', 'Your::Schema::Biz', undef,
                                     { aliases => ['biz2', 'other_alias']
});
Overkill probably.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.rawmode.org/pipermail/dbix-class/attachments/20060407/cb683f8a/attachment.htm 


More information about the Dbix-class mailing list