DBICx::MapMaker - automatically create a DBIx::Class mapping table
version 0.03
A common SQL pattern is the "many to many" relationship; a row in the "left table" may point to many rows in the "right table", and a row in the "right table" may point to many rows in the "left table". This module automatically creates a DBIx::Class result source for that table, and sets up the six necessary relationships.
Here's how to use it. Imagine you have some tables called MySchema::A and MySchema::B, each with a primary key, that you'd like to join. To create the mapping table, you'll write a module like this:
MySchema::A
MySchema::B
package MySchema::MapAB; use DBICx::MapMaker; use base 'DBIx::Class'; my $map = DBICx::MapMaker->new( left_class => 'MySchema::A', right_class => 'MySchema::B', left_name => 'a', right_name => 'b', ); $map->setup_table(__PACKAGE__);
Then, you can:
my $a = $schema->resultset('A')->find(1); $a->b_map; # the mapping table $a->bs; # a list of bs that this a has my $b = $schema->resultset('B')->find(42); $b->a_map; # the mapping table $b->as; # a list of as that this b has
Create a MapMaker. See "ATTRIBUTES" below for a description of the attributes you can pass to the constructor.
MapMaker
Makes $class into the mapping table. $class should be a subclass of DBIx::Class.
$class
DBIx::Class
Here are the attributes that you can pass to the constructor:
The class name of the left/right table (the tables that have a m2m relationship between them).
Required.
The column name for the left/right table's primary key in the map table.
The name of the relationship from the left/right table to the map table.
Optional. Defaults to the name of the opposite table's name with _map appended. (If right_name is foo, then left_to_map_relation will be foo_map.)
_map
right_name
foo
left_to_map_relation
foo_map
The name of the m2m relationship. rights_from_left is the method you'll call on a left row to get the corresponding rights. (lefts_from_right is the opposite.)
rights_from_left
left
right
lefts_from_right
Optional. Defaults to the name of the row returned with "s" appended. If left_name is "foo", then lefts_from_right will be "foos" by default.
left_name
The name of the created mapping table.
Optional. Defaults to "map_left_name_right_name". (With foo and bar, map_foo_bar.)
bar
map_foo_bar
Jonathan Rockway <jrockway@cpan.org>
<jrockway@cpan.org>
Stevan Little <stevan.little@iinteractive.com>
<stevan.little@iinteractive.com>
Adam Herzog <adam@adamherzog.com>
<adam@adamherzog.com>
Copyright 2008 Infinity Interactive, Inc.
http://www.iinteractive.com
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This software is copyright (c) 2011 by Jonathan Rockway.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install DBICx::MapMaker, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBICx::MapMaker
CPAN shell
perl -MCPAN -e shell install DBICx::MapMaker
For more information on module installation, please visit the detailed CPAN module installation guide.