4 package DBIx
::Class
::ResultSet
::RecursiveUpdate
;
6 # ABSTRACT: like update_or_create - but recursive
8 use base
qw(DBIx::Class::ResultSet);
10 sub recursive_update
{
11 my ( $self, $updates, $attrs ) = @_;
14 my $unknown_params_ok;
17 if ( defined $attrs && ref $attrs eq 'HASH' ) {
18 $fixed_fields = $attrs->{fixed_fields
};
19 $unknown_params_ok = $attrs->{unknown_params_ok
};
23 elsif ( defined $attrs && ref $attrs eq 'ARRAY' ) {
24 $fixed_fields = $attrs;
28 DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
::recursive_update
(
31 fixed_fields
=> $fixed_fields,
32 unknown_params_ok
=> $unknown_params_ok,
36 package DBIx
::Class
::ResultSet
::RecursiveUpdate
::Functions
;
37 use Carp
::Clan qw
/^DBIx::Class|^HTML::FormHandler|^Try::Tiny/;
38 use Scalar
::Util
qw( blessed );
39 use List
::MoreUtils qw
/ any /;
42 sub recursive_update
{
44 my ( $self, $updates, $fixed_fields, $object, $resolved,
45 $if_not_submitted, $unknown_params_ok )
47 qw
/resultset updates fixed_fields object resolved if_not_submitted unknown_params_ok/
51 my $source = $self->result_source;
53 croak
"first parameter needs to be defined"
54 unless defined $updates;
56 croak
"first parameter needs to be a hashref"
57 unless ref($updates) eq 'HASH';
59 # warn 'entering: ' . $source->from();
60 croak
'fixed fields needs to be an arrayref'
61 if defined $fixed_fields && ref $fixed_fields ne 'ARRAY';
63 # always warn about additional parameters if storage debugging is enabled
64 $unknown_params_ok = 0
65 if $source->storage->debug;
67 if ( blessed
($updates) && $updates->isa('DBIx::Class::Row') ) {
71 if ( !defined $object && exists $updates->{id
} ) {
73 # warn "finding object by id " . $updates->{id} . "\n";
74 $object = $self->find( $updates->{id
}, { key
=> 'primary' } );
76 # warn "object not found by id\n"
77 # unless defined $object;
80 my %fixed_fields = map { $_ => 1 } @$fixed_fields
82 # the updates hashref might contain the pk columns
83 # but with an undefined value
86 ( !exists $updates->{$_}
87 || ( exists $updates->{$_} && !defined $updates->{$_} ) )
88 && !exists $fixed_fields{$_}
89 } $source->primary_columns;
91 # warn "MISSING: " . join(', ', @missing) . "\n";
92 if ( !defined $object && scalar @missing == 0 ) {
94 # warn 'finding by: ' . Dumper( $updates ); use Data::Dumper;
95 $object = $self->find( $updates, { key
=> 'primary' } );
98 # add the resolved columns to the updates hashref
99 $updates = { %$updates, %$resolved };
100 # the resolved hashref might contain the pk columns
101 # but with an undefined value
103 !exists $resolved->{$_}
104 || ( exists $resolved->{$_} && !defined $resolved->{$_} )
107 #warn "MISSING2: " . join( ', ', @missing ) . "\n";
108 if ( !defined $object && scalar @missing == 0 ) {
110 # warn 'finding by +resolved: ' . Dumper( $updates ); use Data::Dumper;
111 $object = $self->find( $updates, { key
=> 'primary' } );
114 $object = $self->new( {} )
115 unless defined $object;
117 # warn Dumper( $updates ); use Data::Dumper;
118 # direct column accessors
121 # relations that that should be done before the row is inserted into the
122 # database like belongs_to
125 # relations that that should be done after the row is inserted into the
126 # database like has_many, might_have and has_one
130 my %columns_by_accessor = _get_columns_by_accessor
($self);
132 # warn 'resolved: ' . Dumper( $resolved );
133 # warn 'updates: ' . Dumper( $updates ); use Data::Dumper;
134 # warn 'columns: ' . Dumper( \%columns_by_accessor );
135 for my $name ( keys %$updates ) {
138 if ( exists $columns_by_accessor{$name}
139 && !( $source->has_relationship($name)
140 && ref( $updates->{$name} ) ) )
143 #warn "$name is a column\n";
144 $columns{$name} = $updates->{$name};
149 if ( $source->has_relationship($name) ) {
150 if ( _master_relation_cond
( $self, $name ) ) {
152 #warn "$name is a pre-update rel\n";
153 $pre_updates{$name} = $updates->{$name};
158 #warn "$name is a post-update rel\n";
159 $post_updates{$name} = $updates->{$name};
164 # many-to-many helper accessors
165 if ( is_m2m
( $self, $name ) ) {
167 #warn "$name is a many-to-many helper accessor\n";
168 $m2m_accessors{$name} = $updates->{$name};
173 if ( $object->can($name) && not $source->has_relationship($name) ) {
175 #warn "$name is an accessor";
176 $other_methods{$name} = $updates->{$name};
182 # don't throw a warning instead of an exception to give users
183 # time to adapt to the new API
185 "No such column, relationship, many-to-many helper accessor or generic accessor '$name'"
186 ) unless $unknown_params_ok;
188 #$self->throw_exception(
189 # "No such column, relationship, many-to-many helper accessor or generic accessor '$name'"
193 # warn 'other: ' . Dumper( \%other_methods ); use Data::Dumper;
195 # first update columns and other accessors
196 # so that later related records can be found
197 for my $name ( keys %columns ) {
199 #warn "update col $name\n";
200 $object->$name( $columns{$name} );
202 for my $name ( keys %other_methods ) {
204 #warn "update other $name\n";
205 $object->$name( $other_methods{$name} );
207 for my $name ( keys %pre_updates ) {
209 #warn "pre_update $name\n";
210 _update_relation
( $self, $name, $pre_updates{$name}, $object,
214 # $self->_delete_empty_auto_increment($object);
215 # don't allow insert to recurse to related objects
216 # do the recursion ourselves
217 # $object->{_rel_in_storage} = 1;
218 #warn "CHANGED: " . $object->is_changed . "\n";
219 #warn "IN STOR: " . $object->in_storage . "\n";
220 $object->update_or_insert if $object->is_changed;
221 $object->discard_changes;
223 # updating many_to_many
224 for my $name ( keys %m2m_accessors ) {
225 my $value = $m2m_accessors{$name};
227 #warn "update m2m $name\n";
228 # TODO: only first pk col is used
229 my ($pk) = _get_pk_for_related
( $self, $name );
231 my $result_source = $object->$name->result_source;
233 if ( defined $value && ref $value eq 'ARRAY' ) {
234 @updates = @{$value};
236 elsif ( defined $value && !ref $value ) {
239 elsif ( defined $value ) {
241 "value of many-to-many rel '$name' must be an arrayref or scalar: $value";
243 for my $elem (@updates) {
244 if ( blessed
($elem) && $elem->isa('DBIx::Class::Row') ) {
247 elsif ( ref $elem eq 'HASH' ) {
250 resultset
=> $result_source->resultset,
256 $result_source->resultset->find( { $pk => $elem } );
259 my $set_meth = 'set_' . $name;
260 $object->$set_meth( \
@rows );
262 for my $name ( keys %post_updates ) {
264 #warn "post_update $name\n";
265 _update_relation
( $self, $name, $post_updates{$name}, $object,
271 # returns DBIx::Class::ResultSource::column_info as a hash indexed by column accessor || name
272 sub _get_columns_by_accessor
{
274 my $source = $self->result_source;
276 for my $name ( $source->columns ) {
277 my $info = $source->column_info($name);
278 $info->{name
} = $name;
279 $columns{ $info->{accessor
} || $name } = $info;
284 # Arguments: $rs, $name, $updates, $row, $if_not_submitted
285 sub _update_relation
{
286 my ( $self, $name, $updates, $object, $if_not_submitted ) = @_;
288 # this should never happen because we're checking the paramters passed to
289 # recursive_update, but just to be sure...
290 $object->throw_exception("No such relationship '$name'")
291 unless $object->has_relationship($name);
293 #warn "_update_relation $name: OBJ: " . ref($object) . "\n";
295 my $info = $object->result_source->relationship_info($name);
297 # get a related resultset without a condition
298 my $related_resultset =
299 $self->related_resultset($name)->result_source->resultset;
301 if ( $self->result_source->can('_resolve_condition') ) {
303 $self->result_source->_resolve_condition( $info->{cond
}, $name,
307 $self->throw_exception(
308 "result_source must support _resolve_condition");
311 # warn "$name resolved: " . Dumper( $resolved ); use Data::Dumper;
313 if defined $DBIx::Class
::ResultSource
::UNRESOLVABLE_CONDITION
314 && $DBIx::Class
::ResultSource
::UNRESOLVABLE_CONDITION
317 #warn "RESOLVED: " . Dumper($resolved); use Data::Dumper;
319 my @rel_cols = keys %{ $info->{cond
} };
320 map {s/^foreign\.//} @rel_cols;
322 #warn "REL_COLS: " . Dumper(@rel_cols); use Data::Dumper;
323 #my $rel_col_cnt = scalar @rel_cols;
325 # find out if all related columns are nullable
326 my $all_fks_nullable = 1;
327 for my $rel_col (@rel_cols) {
328 $all_fks_nullable = 0
329 unless $related_resultset->result_source->column_info($rel_col)
333 $if_not_submitted = $all_fks_nullable ? 'nullify' : 'delete'
334 unless defined $if_not_submitted;
336 #warn "\tNULLABLE: $all_fks_nullable ACTION: $if_not_submitted\n";
338 #warn "RELINFO for $name: " . Dumper($info); use Data::Dumper;
340 # the only valid datatype for a has_many rels is an arrayref
341 if ( $info->{attrs
}{accessor
} eq 'multi' ) {
343 # handle undef like empty arrayref
345 unless defined $updates;
346 $self->throw_exception(
347 "data for has_many relationship '$name' must be an arrayref")
348 unless ref $updates eq 'ARRAY';
352 #warn "\tupdating has_many rel '$name' ($rel_col_cnt columns cols)\n";
353 for my $sub_updates ( @{$updates} ) {
354 my $sub_object = recursive_update
(
355 resultset
=> $related_resultset,
356 updates
=> $sub_updates,
357 resolved
=> $resolved
360 push @updated_objs, $sub_object;
363 #warn "\tcreated and updated related rows\n";
365 my @related_pks = $related_resultset->result_source->primary_columns;
367 my $rs_rel_delist = $object->$name;
369 # foreign table has a single pk column
370 if ( scalar @related_pks == 1 ) {
371 $rs_rel_delist = $rs_rel_delist->search_rs(
373 { -not_in
=> [ map ( $_->id, @updated_objs ) ] }
378 # foreign table has multiple pk columns
381 for my $obj (@updated_objs) {
383 for my $col (@related_pks) {
384 $cond_for_obj{$col} = $obj->get_column($col);
386 push @cond, \
%cond_for_obj;
389 # only limit resultset if there are related rows left
390 if ( scalar @cond ) {
392 $rs_rel_delist->search_rs( { -not => [@cond] } );
396 #warn "\tCOND: " . Dumper(\%cond);
397 #my $rel_delist_cnt = $rs_rel_delist->count;
398 if ( $if_not_submitted eq 'delete' ) {
400 #warn "\tdeleting related rows: $rel_delist_cnt\n";
401 $rs_rel_delist->delete;
403 elsif ( $if_not_submitted eq 'set_to_null' ) {
405 #warn "\tnullifying related rows: $rel_delist_cnt\n";
406 my %update = map { $_ => undef } @rel_cols;
407 $rs_rel_delist->update( \
%update );
410 elsif ($info->{attrs
}{accessor
} eq 'single'
411 || $info->{attrs
}{accessor
} eq 'filter' )
414 #warn "\tupdating rel '$name': $if_not_submitted\n";
416 if ( ref $updates ) {
417 if ( blessed
($updates) && $updates->isa('DBIx::Class::Row') ) {
418 $sub_object = $updates;
421 # for might_have relationship
422 elsif ( $info->{attrs
}{accessor
} eq 'single'
423 && defined $object->$name )
425 $sub_object = recursive_update
(
426 resultset
=> $related_resultset,
428 object
=> $object->$name
432 $sub_object = recursive_update
(
433 resultset
=> $related_resultset,
435 resolved
=> $resolved
440 $sub_object = $related_resultset->find($updates)
443 && ( exists $info->{attrs
}{join_type
}
444 && $info->{attrs
}{join_type
} eq 'LEFT' )
447 $object->set_from_related( $name, $sub_object )
451 && ( exists $info->{attrs
}{join_type
}
452 && $info->{attrs
}{join_type
} eq 'LEFT' )
456 $self->throw_exception(
457 "recursive_update doesn't now how to handle relationship '$name' with accessor "
458 . $info->{attrs
}{accessor
} );
463 my ( $self, $relation ) = @_;
464 my $rclass = $self->result_class;
466 # DBIx::Class::IntrospectableM2M
467 if ( $rclass->can('_m2m_metadata') ) {
468 return $rclass->_m2m_metadata->{$relation};
470 my $object = $self->new( {} );
471 if ( $object->can($relation)
472 and !$self->result_source->has_relationship($relation)
473 and $object->can( 'set_' . $relation ) )
481 my ( $self, $relation ) = @_;
482 my $rclass = $self->result_class;
484 # DBIx::Class::IntrospectableM2M
485 if ( $rclass->can('_m2m_metadata') ) {
486 return $self->result_source->related_source(
487 $rclass->_m2m_metadata->{$relation}{relation
} )
489 $rclass->_m2m_metadata->{$relation}{foreign_relation
} );
491 my $object = $self->new( {} );
492 my $r = $object->$relation;
493 return $r->result_source;
496 sub _delete_empty_auto_increment
{
497 my ( $self, $object ) = @_;
498 for my $col ( keys %{ $object->{_column_data
} } ) {
499 if ($object->result_source->column_info($col)->{is_auto_increment
}
500 and ( !defined $object->{_column_data
}{$col}
501 or $object->{_column_data
}{$col} eq '' )
504 delete $object->{_column_data
}{$col};
509 sub _get_pk_for_related
{
510 my ( $self, $relation ) = @_;
512 if ( $self->result_source->has_relationship($relation) ) {
513 $result_source = $self->result_source->related_source($relation);
517 if ( is_m2m
( $self, $relation ) ) {
518 $result_source = get_m2m_source
( $self, $relation );
520 return $result_source->primary_columns;
523 # This function determines wheter a relationship should be done before or
524 # after the row is inserted into the database
525 # relationships before: belongs_to
526 # relationships after: has_many, might_have and has_one
527 # true means before, false after
528 sub _master_relation_cond
{
529 my ( $self, $name ) = @_;
531 my $source = $self->result_source;
532 my $info = $source->relationship_info($name);
534 #warn "INFO: " . Dumper($info) . "\n";
536 # has_many rels are always after
538 if $info->{attrs
}->{accessor
} eq 'multi';
540 my @foreign_ids = _get_pk_for_related
( $self, $name );
542 #warn "IDS: " . join(', ', @foreign_ids) . "\n";
544 my $cond = $info->{cond
};
547 my ( $source, $cond, @foreign_ids ) = @_;
549 while ( my ( $f_key, $col ) = each %{$cond} ) {
551 # might_have is not master
553 $f_key =~ s/^foreign\.//;
554 if ( $source->column_info($col)->{is_auto_increment
} ) {
557 if ( any
{ $_ eq $f_key } @foreign_ids ) {
564 if ( ref $cond eq 'HASH' ) {
565 return _inner
( $source, $cond, @foreign_ids );
568 # arrayref of hashrefs
569 elsif ( ref $cond eq 'ARRAY' ) {
570 for my $new_cond (@$cond) {
571 return _inner
( $source, $new_cond, @foreign_ids );
575 $source->throw_exception(
576 "unhandled relation condition " . ref($cond) );
581 1; # Magic true value required at end of module
586 # The functional interface:
588 my $schema = MyDB::Schema->connect();
589 my $new_item = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
590 resultset => $schema->resultset('User'),
595 title => "One Flew Over the Cuckoo's Nest"
599 unknown_params_ok => 1,
603 # As ResultSet subclass:
605 __PACKAGE__->load_namespaces( default_resultset_class => '+DBIx::Class::ResultSet::RecursiveUpdate' );
607 # in the Schema file (see t/lib/DBSchema.pm). Or appropriate 'use base' in the ResultSet classes.
609 my $user = $schema->resultset('User')->recursive_update({
613 title => "One Flew Over the Cuckoo's Nest"
617 unknown_params_ok => 1,
620 # You'll get a warning if you pass non-result specific data to
621 # recursive_update. See L</"Additional data in the updates hashref">
622 # for more information how to prevent this.
626 This is still experimental.
628 You can feed the ->create method of DBIx::Class with a recursive datastructure
629 and have the related records created. Unfortunately you cannot do a similar
630 thing with update_or_create. This module tries to fill that void until
631 L<DBIx::Class> has an api itself.
633 The functional interface can be used without modifications of the model,
634 for example by form processors like L<HTML::FormHandler::Model::DBIC>.
636 It is a base class for L<DBIx::Class::ResultSet>s providing the method
637 recursive_update which works just like update_or_create but can recursively
638 update or create result objects composed of multiple rows. All rows need to be
639 identified by primary keys so you need to provide them in the update structure
640 (unless they can be deduced from the parent row. For example a related row of
641 a belongs_to relationship). If any of the primary key columns are missing,
642 a new row will be created, with the expectation that the missing columns will
643 be filled by it (as in the case of auto_increment primary keys).
645 If the resultset itself stores an assignment for the primary key,
648 my $restricted_rs = $user_rs->search( { id => 1 } );
650 you need to inform recursive_update about the additional predicate with the fixed_fields attribute:
652 my $user = $restricted_rs->recursive_update( {
655 title => 'One Flew Over the Cuckoo's Nest'
660 fixed_fields => [ 'id' ],
664 For a many_to_many (pseudo) relation you can supply a list of primary keys
665 from the other table and it will link the record at hand to those and
666 only those records identified by them. This is convenient for handling web
667 forms with check boxes (or a select field with multiple choice) that lets you
668 update such (pseudo) relations.
670 For a description how to set up base classes for ResultSets see
671 L<DBIx::Class::Schema/load_namespaces>.
673 =head2 Additional data in the updates hashref
675 If you pass additional data to recursive_update which doesn't match a column
676 name, column accessor, relationship or many-to-many helper accessor, it will
677 throw a warning by default. To disable this behaviour you can set the
678 unknown_params_ok attribute to a true value.
680 The warning thrown is:
681 "No such column, relationship, many-to-many helper accessor or generic accessor '$key'"
683 When used by L<HTML::FormHandler::Model::DBIC> this can happen if you have
684 additional form fields that aren't relevant to the database but don't have the
685 noupdate attribute set to a true value.
687 NOTE: in a future version this behaviour will change and throw an exception
688 instead of a warning!
691 =head1 DESIGN CHOICES
693 Columns and relationships which are excluded from the updates hashref aren't
696 =head2 Treatment of belongs_to relations
698 In case the relationship is included but undefined in the updates hashref,
699 all columns forming the relationship will be set to null.
700 If not all of them are nullable, DBIx::Class will throw an error.
702 Updating the relationship:
704 my $dvd = $dvd_rs->recursive_update( {
709 Clearing the relationship (only works if cols are nullable!):
711 my $dvd = $dvd_rs->recursive_update( {
716 =head2 Treatment of might_have relationships
718 In case the relationship is included but undefined in the updates hashref,
719 all columns forming the relationship will be set to null.
721 Updating the relationship:
723 my $user = $user_rs->recursive_update( {
726 street => "101 Main Street",
732 Clearing the relationship:
734 my $user = $user_rs->recursive_update( {
739 =head2 Treatment of has_many relations
741 If a relationship key is included in the data structure with a value of undef
742 or an empty array, all existing related rows will be deleted, or their foreign
743 key columns will be set to null.
745 The exact behaviour depends on the nullability of the foreign key columns and
746 the value of the "if_not_submitted" parameter. The parameter defaults to
747 undefined which neither nullifies nor deletes.
749 When the array contains elements they are updated if they exist, created when
750 not and deleted if not included.
752 =head3 All foreign table columns are nullable
754 In this case recursive_update defaults to nullifying the foreign columns.
756 =head3 Not all foreign table columns are nullable
758 In this case recursive_update deletes the foreign rows.
760 Updating the relationship:
764 my $user = $user_rs->recursive_update( {
766 owned_dvds => [1, 2],
771 my $user = $user_rs->recursive_update( {
775 name => 'temp name 1',
778 name => 'temp name 2',
785 my $user = $user_rs->recursive_update( {
787 owned_dvds => [ $dvd1, $dvd2 ],
790 You can even mix them:
792 my $user = $user_rs->recursive_update( {
794 owned_dvds => [ 1, { id => 2 } ],
797 Clearing the relationship:
799 my $user = $user_rs->recursive_update( {
804 This is the same as passing an empty array:
806 my $user = $user_rs->recursive_update( {
811 =head2 Treatment of many-to-many pseudo relations
813 If a many-to-many accessor key is included in the data structure with a value
814 of undef or an empty array, all existing related rows are unlinked.
816 When the array contains elements they are updated if they exist, created when
817 not and deleted if not included.
819 See L</is_m2m> for many-to-many pseudo relationship detection.
821 Updating the relationship:
825 my $dvd = $dvd_rs->recursive_update( {
832 my $dvd = $dvd_rs->recursive_update( {
848 my $dvd = $dvd_rs->recursive_update( {
850 tags => [ $tag1, $tag2 ],
853 You can even mix them:
855 my $dvd = $dvd_rs->recursive_update( {
857 tags => [ 2, { id => 3 } ],
860 Clearing the relationship:
862 my $dvd = $dvd_rs->recursive_update( {
867 This is the same as passing an empty array:
869 my $dvd = $dvd_rs->recursive_update( {
879 =head2 recursive_update
881 The method that does the work here.
887 =item Arguments: $name
889 =item Return Value: true, if $name is a many to many pseudo-relationship
893 The function gets the information about m2m relations from
894 L<DBIx::Class::IntrospectableM2M>. If it isn't loaded in the ResultSource
895 class, the code relies on the fact:
897 if($object->can($name) and
898 !$object->result_source->has_relationship($name) and
899 $object->can( 'set_' . $name )
902 to identify a many to many pseudo relationship. In a similar ugly way the
903 ResultSource of that many to many pseudo relationship is detected.
905 So if you need many to many pseudo relationship support, it's strongly
906 recommended to load L<DBIx::Class::IntrospectableM2M> in your ResultSource
909 =head2 get_m2m_source
913 =item Arguments: $name
915 =item Return Value: $result_source
919 =head1 CONFIGURATION AND ENVIRONMENT
921 DBIx::Class::RecursiveUpdate requires no configuration files or environment variables.
927 optional but recommended:
928 DBIx::Class::IntrospectableM2M
930 =head1 INCOMPATIBILITIES
935 =head1 BUGS AND LIMITATIONS
937 The list of reported bugs can be viewed at L<http://rt.cpan.org/Public/Dist/Display.html?Name=DBIx-Class-ResultSet-RecursiveUpdate>.
939 Please report any bugs or feature requests to
940 C<bug-dbix-class-recursiveput@rt.cpan.org>, or through the web interface at
941 L<http://rt.cpan.org>.