3 package DBIx
::Class
::ResultSet
::RecursiveUpdate
;
5 our $VERSION = '0.012';
7 use base
qw(DBIx::Class::ResultSet);
10 my ( $self, $updates, $fixed_fields ) = @_;
11 return DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
14 fixed_fields
=> $fixed_fields
18 package DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
;
20 use Scalar
::Util
qw( blessed );
23 sub recursive_update
{
25 my ( $self, $updates, $fixed_fields, $object, $resolved, $if_not_submitted ) = @params{ qw
/resultset updates fixed_fields object resolved if_not_submitted/ };
27 # warn 'entering: ' . $self->result_source->from();
28 carp
'fixed fields needs to be an array ref' if $fixed_fields && ref($fixed_fields) ne 'ARRAY';
30 %fixed_fields = map { $_ => 1 } @$fixed_fields if $fixed_fields;
31 if ( blessed
($updates) && $updates->isa('DBIx::Class::Row') ) {
34 if ( $updates->{id
} ){
35 $object = $self->find( $updates->{id
}, { key
=> 'primary' } );
38 grep { !exists $updates->{$_} && !exists $fixed_fields{$_} } $self->result_source->primary_columns;
39 if ( !$object && !scalar @missing ) {
40 # warn 'finding by: ' . Dumper( $updates ); use Data::Dumper;
41 $object = $self->find( $updates, { key
=> 'primary' } );
43 $updates = { %$updates, %$resolved };
45 grep { !exists $resolved->{$_} } @missing;
46 if ( !$object && !scalar @missing ) {
47 # warn 'finding by +resolved: ' . Dumper( $updates ); use Data::Dumper;
48 $object = $self->find( $updates, { key
=> 'primary' } );
50 $object ||= $self->new( {} );
51 # warn Dumper( $updates ); use Data::Dumper;
52 # direct column accessors
55 # relations that that should be done before the row is inserted into the database
59 # relations that that should be done after the row is inserted into the database
60 # like has_many and might_have
63 my %columns_by_accessor = _get_columns_by_accessor
( $self );
64 # warn 'resolved: ' . Dumper( $resolved );
65 # warn 'updates: ' . Dumper( $updates ); use Data::Dumper;
66 # warn 'columns: ' . Dumper( \%columns_by_accessor );
67 for my $name ( keys %$updates ) {
68 my $source = $self->result_source;
69 if ( $columns_by_accessor{$name}
70 && !( $source->has_relationship($name) && ref( $updates->{$name} ) )
73 $columns{$name} = $updates->{$name};
76 if( !( $source->has_relationship($name) ) ){
77 $other_methods{$name} = $updates->{$name};
80 my $info = $source->relationship_info($name);
82 _master_relation_cond
(
83 $source, $info->{cond
}, _get_pk_for_related
( $self, $name)
87 $pre_updates{$name} = $updates->{$name};
90 $post_updates{$name} = $updates->{$name};
93 # warn 'other: ' . Dumper( \%other_methods ); use Data::Dumper;
95 # first update columns and other accessors - so that later related records can be found
96 for my $name ( keys %columns ) {
97 $object->$name( $columns{$name} );
99 for my $name ( keys %other_methods) {
100 $object->$name( $updates->{$name} ) if $object->can( $name );
102 for my $name ( keys %pre_updates ) {
103 my $info = $object->result_source->relationship_info($name);
104 _update_relation
( $self, $name, $updates, $object, $info, $if_not_submitted );
106 # $self->_delete_empty_auto_increment($object);
107 # don't allow insert to recurse to related objects - we do the recursion ourselves
108 # $object->{_rel_in_storage} = 1;
110 $object->update_or_insert;
113 # updating many_to_many
114 for my $name ( keys %$updates ) {
115 next if exists $columns{$name};
116 my $value = $updates->{$name};
118 if ( is_m2m
( $self, $name) ) {
119 my ($pk) = _get_pk_for_related
( $self, $name);
121 my $result_source = $object->$name->result_source;
123 if( ! defined $value ){
127 @updates = @{ $value };
130 @updates = ( $value );
132 for my $elem ( @updates ) {
134 push @rows, recursive_update
( resultset
=> $result_source->resultset, updates
=> $elem );
138 $result_source->resultset->find( { $pk => $elem } );
141 my $set_meth = 'set_' . $name;
142 $object->$set_meth( \
@rows );
145 for my $name ( keys %post_updates ) {
146 my $info = $object->result_source->relationship_info($name);
147 _update_relation
( $self, $name, $updates, $object, $info, $if_not_submitted );
152 sub _get_columns_by_accessor
{
154 my $source = $self->result_source;
156 for my $name ( $source->columns ) {
157 my $info = $source->column_info($name);
158 $info->{name
} = $name;
159 $columns{ $info->{accessor
} || $name } = $info;
164 sub _update_relation
{
165 my ( $self, $name, $updates, $object, $info, $if_not_submitted ) = @_;
167 $self->related_resultset($name)->result_source->resultset;
169 if( $self->result_source->can( '_resolve_condition' ) ){
170 $resolved = $self->result_source->_resolve_condition( $info->{cond
}, $name, $object );
173 $resolved = $self->result_source->resolve_condition( $info->{cond
}, $name, $object );
176 # warn 'resolved: ' . Dumper( $resolved ); use Data::Dumper;
178 if defined $DBIx::Class
::ResultSource
::UNRESOLVABLE_CONDITION
&& $DBIx::Class
::ResultSource
::UNRESOLVABLE_CONDITION
== $resolved;
179 if ( ref $updates->{$name} eq 'ARRAY' ) {
181 for my $sub_updates ( @{ $updates->{$name} } ) {
183 recursive_update
( resultset
=> $related_result, updates
=> $sub_updates, resolved
=> $resolved );
184 push @updated_ids, $sub_object->id;
186 my @related_pks = $related_result->result_source->primary_columns;
187 if( defined $if_not_submitted && $if_not_submitted eq 'delete' ){
188 if ( 1 == scalar @related_pks ){
189 $object->$name->search( { $related_pks[0] => { -not_in
=> \
@updated_ids } } )->delete;
192 elsif( defined $if_not_submitted && $if_not_submitted eq 'set_to_null' ){
193 if ( 1 == scalar @related_pks ){
194 my @fk = keys %$resolved;
195 $object->$name->search( { $related_pks[0] => { -not_in
=> \
@updated_ids } } )->update( { $fk[0] => undef } );
200 my $sub_updates = $updates->{$name};
202 if( ref $sub_updates ){
203 # for might_have relationship
204 if( $info->{attrs
}{accessor
} eq 'single' && defined $object->$name ){
205 $sub_object = recursive_update
(
206 resultset
=> $related_result,
207 updates
=> $sub_updates,
208 object
=> $object->$name
213 recursive_update
( resultset
=> $related_result, updates
=> $sub_updates, resolved
=> $resolved );
216 elsif( ! ref $sub_updates ){
217 $sub_object = $related_result->find( $sub_updates )
218 unless (!$sub_updates && ($info->{attrs
}{join_type
} eq 'LEFT'));
220 $object->set_from_related( $name, $sub_object )
221 unless (!$sub_object && !$sub_updates && ($info->{attrs
}{join_type
} eq 'LEFT'));
226 my ( $self, $relation ) = @_;
227 my $rclass = $self->result_class;
229 # DBIx::Class::IntrospectableM2M
230 if ( $rclass->can('_m2m_metadata') ) {
231 return $rclass->_m2m_metadata->{$relation};
233 my $object = $self->new( {} );
234 if ( $object->can($relation)
235 and !$self->result_source->has_relationship($relation)
236 and $object->can( 'set_' . $relation ) )
244 my ( $self, $relation ) = @_;
245 my $rclass = $self->result_class;
247 # DBIx::Class::IntrospectableM2M
248 if ( $rclass->can('_m2m_metadata') ) {
249 return $self->result_source->related_source(
250 $rclass->_m2m_metadata->{$relation}{relation
} )
252 $rclass->_m2m_metadata->{$relation}{foreign_relation
} );
254 my $object = $self->new( {} );
255 my $r = $object->$relation;
256 return $r->result_source;
259 sub _delete_empty_auto_increment
{
260 my ( $self, $object ) = @_;
261 for my $col ( keys %{ $object->{_column_data
} } ) {
263 $object->result_source->column_info($col)->{is_auto_increment
}
264 and ( !defined $object->{_column_data
}{$col}
265 or $object->{_column_data
}{$col} eq '' )
268 delete $object->{_column_data
}{$col};
273 sub _get_pk_for_related
{
274 my ( $self, $relation ) = @_;
276 if ( $self->result_source->has_relationship($relation) ) {
277 $result_source = $self->result_source->related_source($relation);
281 if ( is_m2m
($self, $relation) ) {
282 $result_source = get_m2m_source
($self, $relation);
284 return $result_source->primary_columns;
287 sub _master_relation_cond
{
288 my ( $source, $cond, @foreign_ids ) = @_;
289 my $foreign_ids_re = join '|', @foreign_ids;
290 if ( ref $cond eq 'HASH' ) {
291 for my $f_key ( keys %{$cond} ) {
293 # might_have is not master
294 my $col = $cond->{$f_key};
296 if ( $source->column_info($col)->{is_auto_increment
} ) {
299 if ( $f_key =~ /^foreign\.$foreign_ids_re/ ) {
304 elsif ( ref $cond eq 'ARRAY' ) {
305 for my $new_cond (@$cond) {
307 if _master_relation_cond
( $source, $new_cond, @foreign_ids );
313 1; # Magic true value required at end of module
318 DBIx::Class::ResultSet::RecursiveUpdate - like update_or_create - but recursive
323 This document describes DBIx::Class::ResultSet::RecursiveUpdate version 0.006
328 The functional interface:
330 my $new_item = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update({
331 resultset => $schema->resultset( 'Dvd' ),
336 title => 'One Flew Over the Cuckoo's Nest'
343 As ResultSet subclass:
345 __PACKAGE__->load_namespaces( default_resultset_class => '+DBIx::Class::ResultSet::RecursiveUpdate' );
347 in the Schema file (see t/lib/DBSchema.pm). Or appriopriate 'use base' in the ResultSet classes.
351 my $user = $user_rs->recursive_update( {
355 title => 'One Flew Over the Cuckoo's Nest'
363 This is still experimental. I've added a functional interface so that it can be used
364 in Form Processors and not require modification of the model.
366 You can feed the ->create method with a recursive datastructure and have the related records
367 created. Unfortunately you cannot do a similar thing with update_or_create - this module
368 tries to fill that void.
370 It is a base class for ResultSets providing just one method: recursive_update
371 which works just like update_or_create but can recursively update or create
372 data objects composed of multiple rows. All rows need to be identified by primary keys
373 - so you need to provide them in the update structure (unless they can be deduced from
374 the parent row - for example when you have a belongs_to relationship).
375 If not all colums comprising the primary key are specified - then a new row will be created,
376 with the expectation that the missing columns will be filled by it (as in the case of auto_increment
380 If the resultset itself stores an assignement for the primary key,
383 my $restricted_rs = $user_rs->search( { id => 1 } );
385 then you need to inform recursive_update about additional predicate with a second argument:
387 my $user = $restricted_rs->recursive_update( {
390 title => 'One Flew Over the Cuckoo's Nest'
397 This will work with a new DBIC release.
399 For a many_to_many (pseudo) relation you can supply a list of primary keys
400 from the other table - and it will link the record at hand to those and
401 only those records identified by them. This is convenient for handling web
402 forms with check boxes (or a SELECT box with multiple choice) that let you
403 update such (pseudo) relations.
405 For a description how to set up base classes for ResultSets see load_namespaces
406 in DBIx::Class::Schema.
408 =head1 DESIGN CHOICES
410 =head2 Treatment of many to many pseudo relations
412 The function gets the information about m2m relations from DBIx::Class::IntrospectableM2M.
413 If it is not loaded in the ResultSource classes - then the code relies on the fact that:
414 if($object->can($name) and
415 !$object->result_source->has_relationship($name) and
416 $object->can( 'set_' . $name )
419 then $name must be a many to many pseudo relation. And that in a
420 similarly ugly was I find out what is the ResultSource of objects from
421 that many to many pseudo relation.
428 =head2 recursive_update
430 The method that does the work here.
434 $self->is_m2m( 'name ' ) - answers the question if 'name' is a many to many
435 (pseudo) relation on $self.
437 =head2 get_m2m_source
439 $self->get_m2m_source( 'name' ) - returns the ResultSource linked to by the many
440 to many (pseudo) relation 'name' from $self.
446 =head1 CONFIGURATION AND ENVIRONMENT
448 DBIx::Class::RecursiveUpdate requires no configuration files or environment variables.
454 =head1 INCOMPATIBILITIES
456 =for author to fill in:
461 =head1 BUGS AND LIMITATIONS
463 =for author to fill in:
465 No bugs have been reported.
467 Please report any bugs or feature requests to
468 C<bug-dbix-class-recursiveput@rt.cpan.org>, or through the web interface at
469 L<http://rt.cpan.org>.
474 Zbigniew Lukasiak C<< <zby@cpan.org> >>
475 Influenced by code by Pedro Melo.
477 =head1 LICENCE AND COPYRIGHT
479 Copyright (c) 2008, Zbigniew Lukasiak C<< <zby@cpan.org> >>. All rights reserved.
481 This module is free software; you can redistribute it and/or
482 modify it under the same terms as Perl itself. See L<perlartistic>.
485 =head1 DISCLAIMER OF WARRANTY
487 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
488 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
489 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
490 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
491 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
492 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
493 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
494 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
495 NECESSARY SERVICING, REPAIR, OR CORRECTION.
497 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
498 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
499 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
500 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
501 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
502 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
503 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
504 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
505 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF