AW: [Dbix-class] Avoid DB hit with many_to_many rel

Hartmaier Alexander Alexander.Hartmaier at t-systems.at
Wed Jun 25 15:23:59 BST 2008


Hi!

many_to_many is no relationship but a relationship helper, so it can't be p=
refetched.
You can of course prefetch the two relationships it consists of.

-Alex
________________________________
Von: Moritz Onken [onken at houseofdesign.de]
Gesendet: Mittwoch, 25. Juni 2008 11:42
An: DBIx::Class user and developer list
Betreff: [Dbix-class] Avoid DB hit with many_to_many rel

Hi,

I have a table 'categories' which has a many_to_many relationship to
itself using a table 'map_categories'.

I found out that prefetching the many_to_many rel does not work. So I
prefetched the has_many rel to 'map_categories' (parents) and prefetch
the belongs_to rel (parent) from 'map_categories' to 'categories' ($rs-
 >search({},{prefetch =3D> {'parents' =3D> 'parent'}})->all). This does
the sql statement I expect but when I call $row->parents->all (or $row-
 >parents->first->parent) on a row of this rs it hits the database
again. Although the resultset already contains all rows it needs.

Any ideas what I did wrong?

cheers,

moritz

package Schema::Categories;

use strict;
use warnings;

use base 'DBIx::Class';

__PACKAGE__->load_components("PK::Auto", "Core");
__PACKAGE__->table("categories");
__PACKAGE__->add_columns(
   "title",
   {
     data_type =3D> "character varying",
     default_value =3D> undef,
     is_nullable =3D> 1,
     size =3D> 100,
   },
   "id",
   {
     data_type =3D> "integer",
     is_nullable =3D> 0
   });

__PACKAGE__->set_primary_key("id");

__PACKAGE__->has_many("parents" =3D> 'MapCategories' =3D> "child");

#__PACKAGE__->many_to_many(many_parents =3D> "parents" =3D> "parent");

1;

package Schema::MapCategories;

use strict;
use warnings;

use base 'DBIx::Class';

__PACKAGE__->load_components("PK::Auto", "Core");
__PACKAGE__->table("map_categories");
__PACKAGE__->add_columns(
   "child",
   {
     data_type =3D> "integer",
     is_nullable =3D> 0,
   },
   "parent",
   {
     data_type =3D> "integer",
     is_nullable =3D> 0
   });

__PACKAGE__->belongs_to("child" =3D> "Schema::Categories", "child");
__PACKAGE__->belongs_to("parent" =3D> "Schema::Categories", "parent");
__PACKAGE__->add_unique_constraint([ qw/child parent/ ]);


1;

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@lists.rawmode.=
org


*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*=
"*
T-Systems Austria GesmbH Rennweg 97-99, 1030 Wien
Handelsgericht Wien, FN 79340b
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*=
"*
Notice: This e-mail contains information that is confidential and may be pr=
ivileged.
If you are not the intended recipient, please notify the sender and then
delete this e-mail immediately.
*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*"*=
"*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20080625/f92=
05d95/attachment.htm


More information about the DBIx-Class mailing list