[Dbix-class] Retrieving data from a link table
Peter Rabbitson
rabbit+dbic at rabbit.us
Mon Dec 10 09:06:06 GMT 2012
On Fri, Dec 07, 2012 at 04:53:06PM +0100, Ekki Plicht (DF4OR) wrote:
> Hi.
> Two tables with a many_to_many relation via a link table:
>
> Table of web pages:
> table webpage(id, filename, ...)
>
> Table of images:
> table images (id, filename, ...)
>
> Table linking pages and images:
> table img_grp_page (id_img, groupnumber, id_page)
>
>
> The many_to_many relation works fine, I can retrieve all images belonging
> to a certain webpage and all webpages where a certain image is used.
>
> What I want to have is the 'group number' from the link table as well.
> Images are clustered in certain groups on a page, that information is
> represented by that group number.
>
> Is there a way to get this in one go (one query)?
Of course there is. The key thing here is that many-to-many is *not*
a relationship (as prominently explained in several places in the docs).
All an m2m accessor really is is sugar for
$near->search_related('link')->search_related('far');
Furthermroe the entire purpose of an m2m is to hide away *entirely* the
existence of the linker. Therefore the simple answer to your question
is: do not use m2m ;)
my @linkers_with_fars
= $near->search_related('link', {}, { prefetch => 'far' })
Cheers
More information about the DBIx-Class
mailing list