| DBIx-Class documentation | view source | Contained in the DBIx-Class distribution. |
DBIx::Class::Relationship::Base - Inter-table relationships
This class provides methods to describe the relationships between the tables in your database model. These are the "bare bones" relationships methods, for predefined ones, look in DBIx::Class::Relationship.
__PACKAGE__->add_relationship('relname', 'Foreign::Class', $cond, $attrs);
The condition needs to be an SQL::Abstract-style representation of the
join between the tables. When resolving the condition for use in a JOIN,
keys using the pseudo-table foreign are resolved to mean "the Table on the
other side of the relationship", and values using the pseudo-table self
are resolved to mean "the Table this class is representing". Other
restrictions, such as by value, sub-select and other tables, may also be
used. Please check your database for JOIN parameter support.
For example, if you're creating a relationship from Author to Book, where
the Book table has a column author_id containing the ID of the Author
row:
{ 'foreign.author_id' => 'self.id' }
will result in the JOIN clause
author me JOIN book book ON book.author_id = me.id
For multi-column foreign keys, you will need to specify a foreign-to-self
mapping for each column in the key. For example, if you're creating a
relationship from Book to Edition, where the Edition table refers to a
publisher and a type (e.g. "paperback"):
{
'foreign.publisher_id' => 'self.publisher_id',
'foreign.type_id' => 'self.type_id',
}
This will result in the JOIN clause:
book me JOIN edition edition ON edition.publisher_id = me.publisher_id
AND edition.type_id = me.type_id
Each key-value pair provided in a hashref will be used as ANDed conditions.
To add an ORed condition, use an arrayref of hashrefs. See the
SQL::Abstract documentation for more details.
In addition to the standard ResultSet attributes, the following attributes are also valid:
Explicitly specifies the type of join to use in the relationship. Any SQL
join type is valid, e.g. LEFT or RIGHT. It will be placed in the SQL
command immediately before JOIN.
An arrayref containing a list of accessors in the foreign class to create in the main class. If, for example, you do the following:
MyDB::Schema::CD->might_have(liner_notes => 'MyDB::Schema::LinerNotes',
undef, {
proxy => [ qw/notes/ ],
});
Then, assuming MyDB::Schema::LinerNotes has an accessor named notes, you can do:
my $cd = MyDB::Schema::CD->find(1);
$cd->notes('Notes go here'); # set notes -- LinerNotes object is
# created if it doesn't exist
Specifies the type of accessor that should be created for the relationship.
Valid values are single (for when there is only a single related object),
multi (when there can be many), and filter (for when there is a single
related object, but you also want the relationship accessor to double as
a column accessor). For multi accessors, an add_to_* method is also
created, which calls create_related for the relationship.
If you are using SQL::Translator to create SQL for you and you find that it is creating constraints where it shouldn't, or not creating them where it should, set this attribute to a true or false value to override the detection of when to create constraints.
If you are using SQL::Translator to create SQL for you, you can use these
attributes to explicitly set the desired ON DELETE or ON UPDATE constraint
type. If not supplied the SQLT parser will attempt to infer the constraint type by
interrogating the attributes of the opposite relationship. For any 'multi'
relationship with cascade_delete => 1, the corresponding belongs_to
relationship will be created with an ON DELETE CASCADE constraint. For any
relationship bearing cascade_copy => 1 the resulting belongs_to constraint
will be ON UPDATE CASCADE. If you wish to disable this autodetection, and just
use the RDBMS' default constraint type, pass on_delete => undef or
on_delete => '', and the same for on_update respectively.
Tells SQL::Translator that the foreign key constraint it creates should be deferrable. In other words, the user may request that the constraint be ignored until the end of the transaction. Currently, only the PostgreSQL producer actually supports this.
Tells SQL::Translator to add an index for this constraint. Can also be specified globally in the args to deploy in DBIx::Class::Schema or create_ddl_dir in DBIx::Class::Schema. Default is on, set to 0 to disable.
Registers a relationship on the class. This is called internally by DBIx::Class::ResultSourceProxy to set up Accessors and Proxies.
Currently only available for has_many, many-to-many and 'multi' type
relationships.
my $role = $schema->resultset('Role')->find(1);
$actor->add_to_roles($role);
# creates a My::DBIC::Schema::ActorRoles linking table row object
$actor->add_to_roles({ name => 'lead' }, { salary => 15_000_000 });
# creates a new My::DBIC::Schema::Role row object and the linking table
# object with an extra column in the link
Adds a linking table object for $obj or $foreign_vals. If the first
argument is a hash reference, the related object is created first with the
column values in the hash. If an object reference is given, just the linking
table object is created. In either case, any additional column values for the
linking table object can be specified in $link_vals.
Currently only available for many-to-many relationships.
my $actor = $schema->resultset('Actor')->find(1);
my @roles = $schema->resultset('Role')->search({ role =>
{ '-in' => ['Fred', 'Barney'] } } );
$actor->set_roles(\@roles);
# Replaces all of $actor's previous roles with the two named
$actor->set_roles(\@roles, { salary => 15_000_000 });
# Sets a column in the link table for all roles
Replace all the related objects with the given reference to a list of
objects. This does a delete on the link table resultset to remove the
association between the current object and all related objects, then calls
add_to_$rel repeatedly to link all the new objects.
Note that this means that this method will not delete any objects in the table on the right side of the relation, merely that it will delete the link between them.
Due to a mistake in the original implementation of this method, it will also accept a list of objects or hash references. This is deprecated and will be removed in a future version.
Currently only available for many-to-many relationships.
my $role = $schema->resultset('Role')->find(1);
$actor->remove_from_roles($role);
# removes $role's My::DBIC::Schema::ActorRoles linking table row object
Removes the link between the current object and the related object. Note that the related object itself won't be deleted unless you call ->delete() on it. This method just removes the link between the two objects.
Matt S. Trout <mst@shadowcatsystems.co.uk>
You may distribute this code under the same terms as Perl itself.
| DBIx-Class documentation | view source | Contained in the DBIx-Class distribution. |