f18323ad68078ed74f97117621c8e2f29a0ca1f1
[chaz/p5-DBIx-Class-ResultSet-RecursiveUpdate] / lib / DBIx / Class / ResultSet / RecursiveUpdate.pm
1 use strict;
2 use warnings;
3
4 package DBIx::Class::ResultSet::RecursiveUpdate;
5
6 # ABSTRACT: like update_or_create - but recursive
7
8 use base qw(DBIx::Class::ResultSet);
9
10 sub recursive_update {
11 my ( $self, $updates, $attrs ) = @_;
12
13 my $fixed_fields;
14 my $unknown_params_ok;
15
16 # 0.21+ api
17 if ( defined $attrs && ref $attrs eq 'HASH' ) {
18 $fixed_fields = $attrs->{fixed_fields};
19 $unknown_params_ok = $attrs->{unknown_params_ok};
20 }
21
22 # pre 0.21 api
23 elsif ( defined $attrs && ref $attrs eq 'ARRAY' ) {
24 $fixed_fields = $attrs;
25 }
26
27 return
28 DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
29 resultset => $self,
30 updates => $updates,
31 fixed_fields => $fixed_fields,
32 unknown_params_ok => $unknown_params_ok,
33 );
34 }
35
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 /;
40
41 sub recursive_update {
42 my %params = @_;
43 my ( $self, $updates, $fixed_fields, $object, $resolved,
44 $if_not_submitted, $unknown_params_ok )
45 = @params{
46 qw/resultset updates fixed_fields object resolved if_not_submitted unknown_params_ok/
47 };
48 $resolved ||= {};
49
50 my $source = $self->result_source;
51
52 croak "first parameter needs to be defined"
53 unless defined $updates;
54
55 croak "first parameter needs to be a hashref"
56 unless ref($updates) eq 'HASH';
57
58 # warn 'entering: ' . $source->from();
59 croak 'fixed fields needs to be an arrayref'
60 if defined $fixed_fields && ref $fixed_fields ne 'ARRAY';
61
62 # always warn about additional parameters if storage debugging is enabled
63 $unknown_params_ok = 0
64 if $source->storage->debug;
65
66 if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) {
67 return $updates;
68 }
69 if ( exists $updates->{id} ) {
70
71 # warn "finding object by id " . $updates->{id} . "\n";
72 $object = $self->find( $updates->{id}, { key => 'primary' } );
73
74 # warn "object not found by id\n"
75 # unless defined $object;
76 }
77
78 my %fixed_fields = map { $_ => 1 } @$fixed_fields
79 if $fixed_fields;
80 my @missing =
81 grep { !exists $updates->{$_} && !exists $fixed_fields{$_} }
82 $source->primary_columns;
83 if ( !$object && !scalar @missing ) {
84
85 # warn 'finding by: ' . Dumper( $updates ); use Data::Dumper;
86 $object = $self->find( $updates, { key => 'primary' } );
87 }
88 $updates = { %$updates, %$resolved };
89 @missing =
90 grep { !exists $resolved->{$_} } @missing;
91 if ( !$object && !scalar @missing ) {
92
93 # warn 'finding by +resolved: ' . Dumper( $updates ); use Data::Dumper;
94 $object = $self->find( $updates, { key => 'primary' } );
95 }
96
97 $object = $self->new( {} )
98 unless defined $object;
99
100 # warn Dumper( $updates ); use Data::Dumper;
101 # direct column accessors
102 my %columns;
103
104 # relations that that should be done before the row is inserted into the
105 # database like belongs_to
106 my %pre_updates;
107
108 # relations that that should be done after the row is inserted into the
109 # database like has_many, might_have and has_one
110 my %post_updates;
111 my %other_methods;
112 my %m2m_accessors;
113 my %columns_by_accessor = _get_columns_by_accessor($self);
114
115 # warn 'resolved: ' . Dumper( $resolved );
116 # warn 'updates: ' . Dumper( $updates ); use Data::Dumper;
117 # warn 'columns: ' . Dumper( \%columns_by_accessor );
118 for my $name ( keys %$updates ) {
119
120 # columns
121 if ( exists $columns_by_accessor{$name}
122 && !( $source->has_relationship($name)
123 && ref( $updates->{$name} ) ) )
124 {
125
126 #warn "$name is a column\n";
127 $columns{$name} = $updates->{$name};
128 next;
129 }
130
131 # relationships
132 if ( $source->has_relationship($name) ) {
133 if ( _master_relation_cond( $self, $name ) ) {
134
135 #warn "$name is a pre-update rel\n";
136 $pre_updates{$name} = $updates->{$name};
137 next;
138 }
139 else {
140
141 #warn "$name is a post-update rel\n";
142 $post_updates{$name} = $updates->{$name};
143 next;
144 }
145 }
146
147 # many-to-many helper accessors
148 if ( is_m2m( $self, $name ) ) {
149
150 #warn "$name is a many-to-many helper accessor\n";
151 $m2m_accessors{$name} = $updates->{$name};
152 next;
153 }
154
155 # accessors
156 if ( $object->can($name) && not $source->has_relationship($name) ) {
157
158 #warn "$name is an accessor";
159 $other_methods{$name} = $updates->{$name};
160 next;
161 }
162
163 # unknown
164
165 # don't throw a warning instead of an exception to give users
166 # time to adapt to the new API
167 carp(
168 "No such column, relationship, many-to-many helper accessor or generic accessor '$name'"
169 ) unless $unknown_params_ok;
170
171 #$self->throw_exception(
172 # "No such column, relationship, many-to-many helper accessor or generic accessor '$name'"
173 #);
174 }
175
176 # warn 'other: ' . Dumper( \%other_methods ); use Data::Dumper;
177
178 # first update columns and other accessors
179 # so that later related records can be found
180 for my $name ( keys %columns ) {
181
182 #warn "update col $name\n";
183 $object->$name( $columns{$name} );
184 }
185 for my $name ( keys %other_methods ) {
186
187 #warn "update other $name\n";
188 $object->$name( $other_methods{$name} );
189 }
190 for my $name ( keys %pre_updates ) {
191
192 #warn "pre_update $name\n";
193 _update_relation( $self, $name, $pre_updates{$name}, $object,
194 $if_not_submitted );
195 }
196
197 # $self->_delete_empty_auto_increment($object);
198 # don't allow insert to recurse to related objects
199 # do the recursion ourselves
200 # $object->{_rel_in_storage} = 1;
201 #warn "CHANGED: " . $object->is_changed . "\n";
202 #warn "IN STOR: " . $object->in_storage . "\n";
203 $object->update_or_insert if $object->is_changed;
204 $object->discard_changes;
205
206 # updating many_to_many
207 for my $name ( keys %m2m_accessors ) {
208 my $value = $m2m_accessors{$name};
209
210 #warn "update m2m $name\n";
211 # TODO: only first pk col is used
212 my ($pk) = _get_pk_for_related( $self, $name );
213 my @rows = ();
214 my $result_source = $object->$name->result_source;
215 my @updates;
216 if ( defined $value && ref $value eq 'ARRAY' ) {
217 @updates = @{$value};
218 }
219 elsif ( defined $value && !ref $value ) {
220 @updates = ($value);
221 }
222 else {
223 carp "value of many-to-many rel '$name' must be an arrayref or scalar";
224 }
225 for my $elem (@updates) {
226 if ( blessed($elem) && $elem->isa('DBIx::Class::Row') ) {
227 push @rows, $elem;
228 }
229 elsif ( ref $elem eq 'HASH' ) {
230 push @rows,
231 recursive_update(
232 resultset => $result_source->resultset,
233 updates => $elem
234 );
235 }
236 else {
237 push @rows,
238 $result_source->resultset->find( { $pk => $elem } );
239 }
240 }
241 my $set_meth = 'set_' . $name;
242 $object->$set_meth( \@rows );
243 }
244 for my $name ( keys %post_updates ) {
245
246 #warn "post_update $name\n";
247 _update_relation( $self, $name, $post_updates{$name}, $object,
248 $if_not_submitted );
249 }
250 return $object;
251 }
252
253 # returns DBIx::Class::ResultSource::column_info as a hash indexed by column accessor || name
254 sub _get_columns_by_accessor {
255 my $self = shift;
256 my $source = $self->result_source;
257 my %columns;
258 for my $name ( $source->columns ) {
259 my $info = $source->column_info($name);
260 $info->{name} = $name;
261 $columns{ $info->{accessor} || $name } = $info;
262 }
263 return %columns;
264 }
265
266 # Arguments: $rs, $name, $updates, $row, $if_not_submitted
267 sub _update_relation {
268 my ( $self, $name, $updates, $object, $if_not_submitted ) = @_;
269
270 # this should never happen because we're checking the paramters passed to
271 # recursive_update, but just to be sure...
272 $object->throw_exception("No such relationship '$name'")
273 unless $object->has_relationship($name);
274
275 #warn "_update_relation $name: OBJ: " . ref($object) . "\n";
276
277 my $info = $object->result_source->relationship_info($name);
278
279 # get a related resultset without a condition
280 my $related_resultset =
281 $self->related_resultset($name)->result_source->resultset;
282 my $resolved;
283 if ( $self->result_source->can('_resolve_condition') ) {
284 $resolved =
285 $self->result_source->_resolve_condition( $info->{cond}, $name,
286 $object );
287 }
288 else {
289 $self->throw_exception(
290 "result_source must support _resolve_condition");
291 }
292
293 # warn "$name resolved: " . Dumper( $resolved ); use Data::Dumper;
294 $resolved = {}
295 if defined $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION
296 && $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION
297 == $resolved;
298
299 my @rel_cols = keys %{ $info->{cond} };
300 map {s/^foreign\.//} @rel_cols;
301
302 #warn "REL_COLS: " . Dumper(@rel_cols); use Data::Dumper;
303 #my $rel_col_cnt = scalar @rel_cols;
304
305 # find out if all related columns are nullable
306 my $all_fks_nullable = 1;
307 for my $rel_col (@rel_cols) {
308 $all_fks_nullable = 0
309 unless $related_resultset->result_source->column_info($rel_col)
310 ->{is_nullable};
311 }
312
313 $if_not_submitted = $all_fks_nullable ? 'nullify' : 'delete'
314 unless defined $if_not_submitted;
315
316 #warn "\tNULLABLE: $all_fks_nullable ACTION: $if_not_submitted\n";
317
318 #warn "RELINFO for $name: " . Dumper($info); use Data::Dumper;
319
320 # the only valid datatype for a has_many rels is an arrayref
321 if ( $info->{attrs}{accessor} eq 'multi' ) {
322
323 # handle undef like empty arrayref
324 $updates = []
325 unless defined $updates;
326 $self->throw_exception(
327 "data for has_many relationship '$name' must be an arrayref")
328 unless ref $updates eq 'ARRAY';
329
330 my @updated_objs;
331
332 #warn "\tupdating has_many rel '$name' ($rel_col_cnt columns cols)\n";
333 for my $sub_updates ( @{$updates} ) {
334 my $sub_object = recursive_update(
335 resultset => $related_resultset,
336 updates => $sub_updates,
337 resolved => $resolved
338 );
339
340 push @updated_objs, $sub_object;
341 }
342
343 #warn "\tcreated and updated related rows\n";
344
345 my @related_pks = $related_resultset->result_source->primary_columns;
346
347 my $rs_rel_delist = $object->$name;
348
349 # foreign table has a single pk column
350 if ( scalar @related_pks == 1 ) {
351 $rs_rel_delist = $rs_rel_delist->search_rs(
352 { $related_pks[0] =>
353 { -not_in => [ map ( $_->id, @updated_objs ) ] }
354 }
355 );
356 }
357
358 # foreign table has multiple pk columns
359 else {
360 my @cond;
361 for my $obj (@updated_objs) {
362 my %cond_for_obj;
363 for my $col (@related_pks) {
364 $cond_for_obj{$col} = $obj->get_column($col);
365 }
366 push @cond, \%cond_for_obj;
367 }
368
369 # only limit resultset if there are related rows left
370 if ( scalar @cond ) {
371 $rs_rel_delist =
372 $rs_rel_delist->search_rs( { -not => [@cond] } );
373 }
374 }
375
376 #warn "\tCOND: " . Dumper(\%cond);
377 #my $rel_delist_cnt = $rs_rel_delist->count;
378 if ( $if_not_submitted eq 'delete' ) {
379
380 #warn "\tdeleting related rows: $rel_delist_cnt\n";
381 $rs_rel_delist->delete;
382 }
383 elsif ( $if_not_submitted eq 'set_to_null' ) {
384
385 #warn "\tnullifying related rows: $rel_delist_cnt\n";
386 my %update = map { $_ => undef } @rel_cols;
387 $rs_rel_delist->update( \%update );
388 }
389 }
390 elsif ($info->{attrs}{accessor} eq 'single'
391 || $info->{attrs}{accessor} eq 'filter' )
392 {
393
394 #warn "\tupdating rel '$name': $if_not_submitted\n";
395 my $sub_object;
396 if ( ref $updates ) {
397 if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) {
398 $sub_object = $updates;
399 }
400
401 # for might_have relationship
402 elsif ( $info->{attrs}{accessor} eq 'single'
403 && defined $object->$name )
404 {
405 $sub_object = recursive_update(
406 resultset => $related_resultset,
407 updates => $updates,
408 object => $object->$name
409 );
410 }
411 else {
412 $sub_object = recursive_update(
413 resultset => $related_resultset,
414 updates => $updates,
415 resolved => $resolved
416 );
417 }
418 }
419 else {
420 $sub_object = $related_resultset->find($updates)
421 unless (
422 !$updates
423 && ( exists $info->{attrs}{join_type}
424 && $info->{attrs}{join_type} eq 'LEFT' )
425 );
426 }
427 $object->set_from_related( $name, $sub_object )
428 unless (
429 !$sub_object
430 && !$updates
431 && ( exists $info->{attrs}{join_type}
432 && $info->{attrs}{join_type} eq 'LEFT' )
433 );
434 }
435 else {
436 $self->throw_exception(
437 "recursive_update doesn't now how to handle relationship '$name' with accessor "
438 . $info->{attrs}{accessor} );
439 }
440 }
441
442 sub is_m2m {
443 my ( $self, $relation ) = @_;
444 my $rclass = $self->result_class;
445
446 # DBIx::Class::IntrospectableM2M
447 if ( $rclass->can('_m2m_metadata') ) {
448 return $rclass->_m2m_metadata->{$relation};
449 }
450 my $object = $self->new( {} );
451 if ( $object->can($relation)
452 and !$self->result_source->has_relationship($relation)
453 and $object->can( 'set_' . $relation ) )
454 {
455 return 1;
456 }
457 return;
458 }
459
460 sub get_m2m_source {
461 my ( $self, $relation ) = @_;
462 my $rclass = $self->result_class;
463
464 # DBIx::Class::IntrospectableM2M
465 if ( $rclass->can('_m2m_metadata') ) {
466 return $self->result_source->related_source(
467 $rclass->_m2m_metadata->{$relation}{relation} )
468 ->related_source(
469 $rclass->_m2m_metadata->{$relation}{foreign_relation} );
470 }
471 my $object = $self->new( {} );
472 my $r = $object->$relation;
473 return $r->result_source;
474 }
475
476 sub _delete_empty_auto_increment {
477 my ( $self, $object ) = @_;
478 for my $col ( keys %{ $object->{_column_data} } ) {
479 if ($object->result_source->column_info($col)->{is_auto_increment}
480 and ( !defined $object->{_column_data}{$col}
481 or $object->{_column_data}{$col} eq '' )
482 )
483 {
484 delete $object->{_column_data}{$col};
485 }
486 }
487 }
488
489 sub _get_pk_for_related {
490 my ( $self, $relation ) = @_;
491 my $result_source;
492 if ( $self->result_source->has_relationship($relation) ) {
493 $result_source = $self->result_source->related_source($relation);
494 }
495
496 # many to many case
497 if ( is_m2m( $self, $relation ) ) {
498 $result_source = get_m2m_source( $self, $relation );
499 }
500 return $result_source->primary_columns;
501 }
502
503 # This function determines wheter a relationship should be done before or
504 # after the row is inserted into the database
505 # relationships before: belongs_to
506 # relationships after: has_many, might_have and has_one
507 # true means before, false after
508 sub _master_relation_cond {
509 my ( $self, $name ) = @_;
510
511 my $source = $self->result_source;
512 my $info = $source->relationship_info($name);
513
514 #warn "INFO: " . Dumper($info) . "\n";
515
516 # has_many rels are always after
517 return 0
518 if $info->{attrs}->{accessor} eq 'multi';
519
520 my @foreign_ids = _get_pk_for_related( $self, $name );
521
522 #warn "IDS: " . join(', ', @foreign_ids) . "\n";
523
524 my $cond = $info->{cond};
525
526 sub _inner {
527 my ( $source, $cond, @foreign_ids ) = @_;
528
529 while ( my ( $f_key, $col ) = each %{$cond} ) {
530
531 # might_have is not master
532 $col =~ s/^self\.//;
533 $f_key =~ s/^foreign\.//;
534 if ( $source->column_info($col)->{is_auto_increment} ) {
535 return 0;
536 }
537 if ( any { $_ eq $f_key } @foreign_ids ) {
538 return 1;
539 }
540 }
541 return 0;
542 }
543
544 if ( ref $cond eq 'HASH' ) {
545 return _inner( $source, $cond, @foreign_ids );
546 }
547
548 # arrayref of hashrefs
549 elsif ( ref $cond eq 'ARRAY' ) {
550 for my $new_cond (@$cond) {
551 return _inner( $source, $new_cond, @foreign_ids );
552 }
553 }
554 else {
555 $source->throw_exception(
556 "unhandled relation condition " . ref($cond) );
557 }
558 return;
559 }
560
561 1; # Magic true value required at end of module
562 __END__
563
564 =head1 SYNOPSIS
565
566 # The functional interface:
567
568 my $schema = MyDB::Schema->connect();
569 my $new_item = DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
570 resultset => $schema->resultset('User'),
571 updates => {
572 id => 1,
573 owned_dvds => [
574 {
575 title => "One Flew Over the Cuckoo's Nest"
576 }
577 ]
578 },
579 unknown_params_ok => 1,
580 );
581
582
583 # As ResultSet subclass:
584
585 __PACKAGE__->load_namespaces( default_resultset_class => '+DBIx::Class::ResultSet::RecursiveUpdate' );
586
587 # in the Schema file (see t/lib/DBSchema.pm). Or appropriate 'use base' in the ResultSet classes.
588
589 my $user = $schema->resultset('User')->recursive_update({
590 id => 1,
591 owned_dvds => [
592 {
593 title => "One Flew Over the Cuckoo's Nest"
594 }
595 ]
596 }, {
597 unknown_params_ok => 1,
598 });
599
600 # You'll get a warning if you pass non-result specific data to
601 # recursive_update. See L</"Additional data in the updates hashref">
602 # for more information how to prevent this.
603
604 =head1 DESCRIPTION
605
606 This is still experimental.
607
608 You can feed the ->create method of DBIx::Class with a recursive datastructure
609 and have the related records created. Unfortunately you cannot do a similar
610 thing with update_or_create. This module tries to fill that void until
611 L<DBIx::Class> has an api itself.
612
613 The functional interface can be used without modifications of the model,
614 for example by form processors like L<HTML::FormHandler::Model::DBIC>.
615
616 It is a base class for L<DBIx::Class::ResultSet>s providing the method
617 recursive_update which works just like update_or_create but can recursively
618 update or create result objects composed of multiple rows. All rows need to be
619 identified by primary keys so you need to provide them in the update structure
620 (unless they can be deduced from the parent row. For example a related row of
621 a belongs_to relationship). If any of the primary key columns are missing,
622 a new row will be created, with the expectation that the missing columns will
623 be filled by it (as in the case of auto_increment primary keys).
624
625 If the resultset itself stores an assignment for the primary key,
626 like in the case of:
627
628 my $restricted_rs = $user_rs->search( { id => 1 } );
629
630 you need to inform recursive_update about the additional predicate with the fixed_fields attribute:
631
632 my $user = $restricted_rs->recursive_update( {
633 owned_dvds => [
634 {
635 title => 'One Flew Over the Cuckoo's Nest'
636 }
637 ]
638 },
639 {
640 fixed_fields => [ 'id' ],
641 }
642 );
643
644 For a many_to_many (pseudo) relation you can supply a list of primary keys
645 from the other table and it will link the record at hand to those and
646 only those records identified by them. This is convenient for handling web
647 forms with check boxes (or a select field with multiple choice) that lets you
648 update such (pseudo) relations.
649
650 For a description how to set up base classes for ResultSets see
651 L<DBIx::Class::Schema/load_namespaces>.
652
653 =head2 Additional data in the updates hashref
654
655 If you pass additional data to recursive_update which doesn't match a column
656 name, column accessor, relationship or many-to-many helper accessor, it will
657 throw a warning by default. To disable this behaviour you can set the
658 unknown_params_ok attribute to a true value.
659
660 The warning thrown is:
661 "No such column, relationship, many-to-many helper accessor or generic accessor '$key'"
662
663 When used by L<HTML::FormHandler::Model::DBIC> this can happen if you have
664 additional form fields that aren't relevant to the database but don't have the
665 noupdate attribute set to a true value.
666
667 NOTE: in a future version this behaviour will change and throw an exception
668 instead of a warning!
669
670
671 =head1 DESIGN CHOICES
672
673 Columns and relationships which are excluded from the updates hashref aren't
674 touched at all.
675
676 =head2 Treatment of belongs_to relations
677
678 In case the relationship is included but undefined in the updates hashref,
679 all columns forming the relationship will be set to null.
680 If not all of them are nullable, DBIx::Class will throw an error.
681
682 Updating the relationship:
683
684 my $dvd = $dvd_rs->recursive_update( {
685 id => 1,
686 owner => $user->id,
687 });
688
689 Clearing the relationship (only works if cols are nullable!):
690
691 my $dvd = $dvd_rs->recursive_update( {
692 id => 1,
693 owner => undef,
694 });
695
696 =head2 Treatment of might_have relationships
697
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
701 Updating the relationship:
702
703 my $user = $user_rs->recursive_update( {
704 id => 1,
705 address => {
706 street => "101 Main Street",
707 city => "Podunk",
708 state => "New York",
709 }
710 });
711
712 Clearing the relationship:
713
714 my $user = $user_rs->recursive_update( {
715 id => 1,
716 address => undef,
717 });
718
719 =head2 Treatment of has_many relations
720
721 If a relationship key is included in the data structure with a value of undef
722 or an empty array, all existing related rows will be deleted, or their foreign
723 key columns will be set to null.
724
725 The exact behaviour depends on the nullability of the foreign key columns and
726 the value of the "if_not_submitted" parameter. The parameter defaults to
727 undefined which neither nullifies nor deletes.
728
729 When the array contains elements they are updated if they exist, created when
730 not and deleted if not included.
731
732 =head3 All foreign table columns are nullable
733
734 In this case recursive_update defaults to nullifying the foreign columns.
735
736 =head3 Not all foreign table columns are nullable
737
738 In this case recursive_update deletes the foreign rows.
739
740 Updating the relationship:
741
742 Passing ids:
743
744 my $user = $user_rs->recursive_update( {
745 id => 1,
746 owned_dvds => [1, 2],
747 });
748
749 Passing hashrefs:
750
751 my $user = $user_rs->recursive_update( {
752 id => 1,
753 owned_dvds => [
754 {
755 name => 'temp name 1',
756 },
757 {
758 name => 'temp name 2',
759 },
760 ],
761 });
762
763 Passing objects:
764
765 my $user = $user_rs->recursive_update( {
766 id => 1,
767 owned_dvds => [ $dvd1, $dvd2 ],
768 });
769
770 You can even mix them:
771
772 my $user = $user_rs->recursive_update( {
773 id => 1,
774 owned_dvds => [ 1, { id => 2 } ],
775 });
776
777 Clearing the relationship:
778
779 my $user = $user_rs->recursive_update( {
780 id => 1,
781 owned_dvds => undef,
782 });
783
784 This is the same as passing an empty array:
785
786 my $user = $user_rs->recursive_update( {
787 id => 1,
788 owned_dvds => [],
789 });
790
791 =head2 Treatment of many-to-many pseudo relations
792
793 If a many-to-many accessor key is included in the data structure with a value
794 of undef or an empty array, all existing related rows are unlinked.
795
796 When the array contains elements they are updated if they exist, created when
797 not and deleted if not included.
798
799 See L</is_m2m> for many-to-many pseudo relationship detection.
800
801 Updating the relationship:
802
803 Passing ids:
804
805 my $dvd = $dvd_rs->recursive_update( {
806 id => 1,
807 tags => [1, 2],
808 });
809
810 Passing hashrefs:
811
812 my $dvd = $dvd_rs->recursive_update( {
813 id => 1,
814 tags => [
815 {
816 id => 1,
817 file => 'file0'
818 },
819 {
820 id => 2,
821 file => 'file1',
822 },
823 ],
824 });
825
826 Passing objects:
827
828 my $dvd = $dvd_rs->recursive_update( {
829 id => 1,
830 tags => [ $tag1, $tag2 ],
831 });
832
833 You can even mix them:
834
835 my $dvd = $dvd_rs->recursive_update( {
836 id => 1,
837 tags => [ 2, { id => 3 } ],
838 });
839
840 Clearing the relationship:
841
842 my $dvd = $dvd_rs->recursive_update( {
843 id => 1,
844 tags => undef,
845 });
846
847 This is the same as passing an empty array:
848
849 my $dvd = $dvd_rs->recursive_update( {
850 id => 1,
851 tags => [],
852 });
853
854
855 =head1 INTERFACE
856
857 =head1 METHODS
858
859 =head2 recursive_update
860
861 The method that does the work here.
862
863 =head2 is_m2m
864
865 =over 4
866
867 =item Arguments: $name
868
869 =item Return Value: true, if $name is a many to many pseudo-relationship
870
871 =back
872
873 The function gets the information about m2m relations from
874 L<DBIx::Class::IntrospectableM2M>. If it isn't loaded in the ResultSource
875 class, the code relies on the fact:
876
877 if($object->can($name) and
878 !$object->result_source->has_relationship($name) and
879 $object->can( 'set_' . $name )
880 )
881
882 to identify a many to many pseudo relationship. In a similar ugly way the
883 ResultSource of that many to many pseudo relationship is detected.
884
885 So if you need many to many pseudo relationship support, it's strongly
886 recommended to load L<DBIx::Class::IntrospectableM2M> in your ResultSource
887 class!
888
889 =head2 get_m2m_source
890
891 =over 4
892
893 =item Arguments: $name
894
895 =item Return Value: $result_source
896
897 =back
898
899 =head1 CONFIGURATION AND ENVIRONMENT
900
901 DBIx::Class::RecursiveUpdate requires no configuration files or environment variables.
902
903 =head1 DEPENDENCIES
904
905 DBIx::Class
906
907 optional but recommended:
908 DBIx::Class::IntrospectableM2M
909
910 =head1 INCOMPATIBILITIES
911
912 None reported.
913
914
915 =head1 BUGS AND LIMITATIONS
916
917 The list of reported bugs can be viewed at L<http://rt.cpan.org/Public/Dist/Display.html?Name=DBIx-Class-ResultSet-RecursiveUpdate>.
918
919 Please report any bugs or feature requests to
920 C<bug-dbix-class-recursiveput@rt.cpan.org>, or through the web interface at
921 L<http://rt.cpan.org>.
This page took 0.09172 seconds and 3 git commands to generate.