[Dbix-class] Prefetch multiple many_to_many?

Ben Staude sben1783 at yahoo.de
Mon Nov 5 21:21:46 GMT 2007


Am Samstag 03 November 2007 21:56 schrieb Matt S Trout:
> On Sat, Nov 03, 2007 at 06:23:57PM +0100, Ben Staude wrote:
> > Hi,
> >
> > I have a setup like the many_to_many-example Actor/Role in the docs, but
> > the Actor has another many_to_many relationshiop to, for example,
> > languages:
> >
> > Actor->many_to_many(roles=> 'actor_roles', 'role');
> > Actor->many_to_many(languages => 'actor_languages', 'language');
> >
> > When selecting an Actor, I'd like to prefetch his roles and languages.
> > I'm not yet sure whether this really makes sense with respect to
> > performance for my application, but at least I want to understand what's
> > going wrong when trying to do so:
> >
> > # Get actors by ids
> > my @actors = $schema->resultset('Actor')->search(
> >                 { 'me.id' => { -in => \@actor_ids } },
> > 		{ prefetch => { actor_roles => 'role',
> > 		                actor_languages => 'language' } })->all;
>
> We can't currently prefetch more than one has_many rel at the same level.
>
> Sorry.

No problem, I don't depend on it. Could an appropriate error or warning 
message be implemented?  I think it's quite dangerous because it doesn't 
really break, but silently leads to wrong / unexpected results.

What about the other issue I mentioned that, after prefetching only 
actor_roles => 'role', 
the statement

map { $_->title } $actor->roles

yields a new SELECT instead of using the prefetched data? I can circumvent 
this via

map { $_->role->title } $actor->actor_roles

but would like to understand what's the difference that here the prefetched 
data can be used. Do I need to insert any 'as' or something to the initial, 
prefetching query?

Thanks a lot!

Ben



More information about the DBIx-Class mailing list