[Dbix-class] Prefetch multiple many_to_many?

Ben Staude sben1783 at yahoo.de
Mon Nov 5 22:47:39 GMT 2007


Am Montag 05 November 2007 23:10 schrieb Matt S Trout:
> On Mon, Nov 05, 2007 at 10:21:46PM +0100, Ben Staude wrote:
> > 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.
>
> It normally throws an exception, albeit a not really clear one.

I don't get any. Could this be related to the fact it's a many_to_many, not 
only has_many?

> > 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
>
> Your DBIC is old, I think.
>
> That was fixed a while back (08000 I think, if not very soon after)

My DBIC is at 0.08007. Maybe same thing (many_to_many) as above? I'll see if I 
get the chance to test / compare with a has_many.

Regards,

Ben



More information about the DBIx-Class mailing list