DBIx-Class
view release on metacpan or search on metacpan
lib/DBIx/Class/Relationship/Base.pm view on Meta::CPAN
return shift->search_related(shift)->find(@_);
}
=head2 find_or_new_related
=over 4
=item Arguments: $rel_name, \%col_data, { key => $unique_constraint, L<%attrs|DBIx::Class::ResultSet/ATTRIBUTES> }?
=item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
=back
Find a result object of a related class. See L<DBIx::Class::ResultSet/find_or_new>
for details.
=cut
sub find_or_new_related {
my $self = shift;
my $obj = $self->find_related(@_);
return defined $obj ? $obj : $self->new_related(@_);
}
=head2 find_or_create_related
=over 4
=item Arguments: $rel_name, \%col_data, { key => $unique_constraint, L<%attrs|DBIx::Class::ResultSet/ATTRIBUTES> }?
=item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
=back
Find or create a result object of a related class. See
L<DBIx::Class::ResultSet/find_or_create> for details.
=cut
sub find_or_create_related {
my $self = shift;
my $obj = $self->find_related(@_);
return (defined($obj) ? $obj : $self->create_related(@_));
}
=head2 update_or_create_related
=over 4
=item Arguments: $rel_name, \%col_data, { key => $unique_constraint, L<%attrs|DBIx::Class::ResultSet/ATTRIBUTES> }?
=item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
=back
Update or create a result object of a related class. See
L<DBIx::Class::ResultSet/update_or_create> for details.
=cut
sub update_or_create_related {
#my ($self, $rel, @args) = @_;
shift->related_resultset(shift)->update_or_create(@_);
}
=head2 set_from_related
=over 4
=item Arguments: $rel_name, L<$result|DBIx::Class::Manual::ResultClass>
=item Return Value: not defined
=back
$book->set_from_related('author', $author_obj);
$book->author($author_obj); ## same thing
Set column values on the current object, using related values from the given
related object. This is used to associate previously separate objects, for
example, to set the correct author for a book, find the Author object, then
call set_from_related on the book.
This is called internally when you pass existing objects as values to
L<DBIx::Class::ResultSet/create>, or pass an object to a belongs_to accessor.
The columns are only set in the local copy of the object, call
L<update|DBIx::Class::Row/update> to update them in the storage.
=cut
sub set_from_related {
my ($self, $rel, $f_obj) = @_;
$self->set_columns( $self->result_source->_resolve_relationship_condition (
infer_values_based_on => {},
rel_name => $rel,
foreign_values => $f_obj,
foreign_alias => $rel,
self_alias => 'me',
)->{inferred_values} );
return 1;
}
=head2 update_from_related
=over 4
=item Arguments: $rel_name, L<$result|DBIx::Class::Manual::ResultClass>
=item Return Value: not defined
=back
$book->update_from_related('author', $author_obj);
The same as L</"set_from_related">, but the changes are immediately updated
in storage.
=cut
sub update_from_related {
my $self = shift;
$self->set_from_related(@_);
$self->update;
}
=head2 delete_related
=over 4
=item Arguments: $rel_name, $cond?, L<\%attrs?|DBIx::Class::ResultSet/ATTRIBUTES>
=item Return Value: $underlying_storage_rv
=back
Delete any related row, subject to the given conditions. Internally, this
calls:
$self->search_related(@_)->delete
And returns the result of that.
=cut
sub delete_related {
my $self = shift;
my $obj = $self->search_related(@_)->delete;
delete $self->{related_resultsets}->{$_[0]};
return $obj;
}
=head2 add_to_$rel
B<Currently only available for C<has_many>, C<many_to_many> and 'multi' type
relationships.>
=head3 has_many / multi
=over 4
=item Arguments: \%col_data
=item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
=back
Creates/inserts a new result object. Internally, this calls:
$self->create_related($rel, @_)
And returns the result of that.
=head3 many_to_many
=over 4
=item Arguments: (\%col_data | L<$result|DBIx::Class::Manual::ResultClass>), \%link_col_data?
=item Return Value: L<$result|DBIx::Class::Manual::ResultClass>
( run in 0.526 second using v1.01-cache-2.11-cpan-39bf76dae61 )