1 package DBIx
::Class
::ResultSet
::RecursiveUpdate
;
3 use version
; $VERSION = qv
('0.001');
8 use Scalar
::Util
qw( blessed );
10 use base
qw(DBIx::Class::ResultSet);
12 sub recursive_update
{
13 my ( $self, $updates, $fixed_fields ) = @_;
15 # warn 'entering: ' . $self->result_source->from();
16 if ( blessed
($updates) && $updates->isa('DBIx::Class::Row') ) {
20 carp
if !( ref($fixed_fields) eq 'HASH' );
21 $updates = { %$updates, %$fixed_fields };
24 # direct column accessors
27 # relations that that should be done before the row is inserted into the database
31 # relations that that should be done after the row is inserted into the database
32 # like has_many and might_have
34 my %columns_by_accessor = $self->_get_columns_by_accessor;
36 # warn 'columns_by_accessor: ' . Dumper( \%columns_by_accessor ); use Data::Dumper;
37 for my $name ( keys %$updates ) {
38 my $source = $self->result_source;
39 if ( $columns_by_accessor{$name}
40 && !( $source->has_relationship($name) && ref( $updates->{$name} ) )
43 $columns{$name} = $updates->{$name};
46 next if !$source->has_relationship($name);
47 my $info = $source->relationship_info($name);
49 _master_relation_cond
(
50 $source, $info->{cond
}, $self->_get_pk_for_related($name)
54 $pre_updates{$name} = $updates->{$name};
57 $post_updates{$name} = $updates->{$name};
61 # warn 'columns: ' . Dumper( \%columns ); use Data::Dumper;
65 grep { !exists $columns{$_} } $self->result_source->primary_columns;
66 if ( !scalar @missing ) {
67 $object = $self->find( \
%columns, { key
=> 'primary' } );
69 $object ||= $self->new( {} );
71 # first update columns and other accessors - so that later related records can be found
72 for my $name ( keys %columns ) {
73 $object->$name( $updates->{$name} );
75 for my $name ( keys %pre_updates ) {
76 my $info = $object->result_source->relationship_info($name);
77 $self->_update_relation( $name, $updates, $object, $info );
79 $self->_delete_empty_auto_increment($object);
81 # don't allow insert to recurse to related objects - we do the recursion ourselves
82 # $object->{_rel_in_storage} = 1;
83 $object->update_or_insert;
85 # updating many_to_many
86 for my $name ( keys %$updates ) {
87 next if exists $columns{$name};
88 my $value = $updates->{$name};
91 if ( $self->is_m2m($name) ) {
92 my ($pk) = $self->_get_pk_for_related($name);
94 my $result_source = $object->$name->result_source;
95 for my $elem ( @{ $updates->{$name} } ) {
97 push @rows, $result_source->resultset->find($elem);
101 $result_source->resultset->find( { $pk => $elem } );
104 my $set_meth = 'set_' . $name;
105 $object->$set_meth( \
@rows );
108 for my $name ( keys %post_updates ) {
109 my $info = $object->result_source->relationship_info($name);
110 $self->_update_relation( $name, $updates, $object, $info );
115 sub _get_columns_by_accessor
{
117 my $source = $self->result_source;
119 for my $name ( $source->columns ) {
120 my $info = $source->column_info($name);
121 $info->{name
} = $name;
122 $columns{ $info->{accessor
} || $name } = $info;
127 sub _update_relation
{
128 my ( $self, $name, $updates, $object, $info ) = @_;
131 $self->related_resultset($name)->result_source->resultset;
133 $self->result_source->resolve_condition( $info->{cond
}, $name, $object );
135 # warn 'resolved: ' . Dumper( $resolved ); use Data::Dumper;
137 if $DBIx::Class
::ResultSource
::UNRESOLVABLE_CONDITION
== $resolved;
138 if ( ref $updates->{$name} eq 'ARRAY' ) {
139 for my $sub_updates ( @{ $updates->{$name} } ) {
141 $related_result->recursive_update( $sub_updates, $resolved );
146 $related_result->recursive_update( $updates->{$name}, $resolved );
147 $object->set_from_related( $name, $sub_object );
152 my ( $self, $relation ) = @_;
153 my $rclass = $self->result_class;
155 # DBIx::Class::IntrospectableM2M
156 if ( $rclass->can('_m2m_metadata') ) {
157 return $rclass->_m2m_metadata->{$relation};
159 my $object = $self->new( {} );
160 if ( $object->can($relation)
161 and !$self->result_source->has_relationship($relation)
162 and $object->can( 'set_' . $relation ) )
170 my ( $self, $relation ) = @_;
171 my $rclass = $self->result_class;
173 # DBIx::Class::IntrospectableM2M
174 if ( $rclass->can('_m2m_metadata') ) {
175 return $self->result_source->related_source(
176 $rclass->_m2m_metadata->{$relation}{relation
} )
178 $rclass->_m2m_metadata->{$relation}{foreign_relation
} );
180 my $object = $self->new( {} );
181 my $r = $object->$relation;
182 return $r->result_source;
185 sub _delete_empty_auto_increment
{
186 my ( $self, $object ) = @_;
187 for my $col ( keys %{ $object->{_column_data
} } ) {
189 $object->result_source->column_info($col)->{is_auto_increment
}
190 and ( !defined $object->{_column_data
}{$col}
191 or $object->{_column_data
}{$col} eq '' )
194 delete $object->{_column_data
}{$col};
199 sub _get_pk_for_related
{
200 my ( $self, $relation ) = @_;
202 if ( $self->result_source->has_relationship($relation) ) {
203 $result_source = $self->result_source->related_source($relation);
207 if ( $self->is_m2m($relation) ) {
208 $result_source = $self->get_m2m_source($relation);
210 return $result_source->primary_columns;
213 sub _master_relation_cond
{
214 my ( $source, $cond, @foreign_ids ) = @_;
215 my $foreign_ids_re = join '|', @foreign_ids;
216 if ( ref $cond eq 'HASH' ) {
217 for my $f_key ( keys %{$cond} ) {
219 # might_have is not master
220 my $col = $cond->{$f_key};
222 if ( $source->column_info($col)->{is_auto_increment
} ) {
225 if ( $f_key =~ /^foreign\.$foreign_ids_re/ ) {
230 elsif ( ref $cond eq 'ARRAY' ) {
231 for my $new_cond (@$cond) {
233 if _master_relation_cond
( $source, $new_cond, @foreign_ids );
239 1; # Magic true value required at end of module
244 DBIx::Class::ResultSet::RecursiveUpdate - like update_or_create - but recursive
249 This document describes DBIx::Class::ResultSet::RecursiveUpdate version 0.001
254 __PACKAGE__->load_namespaces( default_resultset_class => '+DBIx::Class::ResultSet::RecursiveUpdate' );
256 in the Schema file (see t/lib/DBSchema.pm). Or appriopriate 'use base' in the ResultSet classes.
260 my $user = $user_rs->recursive_update( {
265 title => 'One Flew Over the Cuckoo's Nest'
274 You can feed the ->create method with a recursive datastructure and have the related records
275 created. Unfortunately you cannot do a similar thing with update_or_create - this module
276 tries to fill that void.
278 It is a base class for ResultSets providing just one method: recursive_update
279 which works just like update_or_create but can recursively update or create
280 data objects composed of multiple rows. All rows need to be identified by primary keys
281 - so you need to provide them in the update structure (unless they can be deduced from
282 the parent row - for example when you have a belongs_to relationship).
283 When creating new rows in a table with auto_increment primary keys you need to
284 put 'undef' for the key value - this is then removed
285 and a correct INSERT statement is generated.
287 For a many_to_many (pseudo) relation you can supply a list of primary keys
288 from the other table - and it will link the record at hand to those and
289 only those records identified by them. This is convenient for handling web
290 forms with check boxes (or a SELECT box with multiple choice) that let you
291 update such (pseudo) relations.
293 For a description how to set up base classes for ResultSets see load_namespaces
294 in DBIx::Class::Schema.
296 =head1 DESIGN CHOICES
298 =head2 Treatment of many to many pseudo relations
300 Matt Trout expressed following criticism of the support for many to many in
301 RecursiveUpdate and since this is an extension of his DBIx::Class I feel obliged to
302 reply to it. It is about two points leading in his opinion to 'fragile and
303 implicitely broken code'.
305 1. That I rely on the fact that
307 if($object->can($name) and
308 !$object->result_source->has_relationship($name) and
309 $object->can( 'set_' . $name )
312 then $name must be a many to many pseudo relation. And that in a
313 similarly ugly was I find out what is the ResultSource of objects from
314 that many to many pseudo relation.
316 2. That I treat uniformly relations and many to many (which are
317 different from relations because they require traversal of the bridge
320 To answer 1) I've refactored that 'dirty' code into is_m2m and get_m2m_source so
321 that it can be easily overridden. I agree that this code is not too nice - but
322 currenlty it is the only way to do what I need - and I'll replace it as soon as
323 there is a more clean way. I don't think it is extremely brittle - sure it will
324 break if many to many (pseudo) relations don't get 'set_*' methods anymore - but
325 I would say it is rather justified for this kind of change in underlying library
329 Ad 2) - first this is not strictly true - RecursiveUpdate does have
330 different code to cope with m2m and other cases (see the point above for
331 example) - but it let's the user to treat m2m and 'normal' relations in a
332 uniform way. I consider this a form of abstraction - it is the work that
333 RecursiveUpdate does for the programmer.
340 =head2 recursive_update
342 The method that does the work here.
346 $self->is_m2m( 'name ' ) - answers the question if 'name' is a many to many
347 (pseudo) relation on $self.
349 =head2 get_m2m_source
351 $self->get_m2m_source( 'name' ) - returns the ResultSource linked to by the many
352 to many (pseudo) relation 'name' from $self.
358 =head1 CONFIGURATION AND ENVIRONMENT
360 DBIx::Class::RecursiveUpdate requires no configuration files or environment variables.
366 =head1 INCOMPATIBILITIES
368 =for author to fill in:
373 =head1 BUGS AND LIMITATIONS
375 =for author to fill in:
377 No bugs have been reported.
379 Please report any bugs or feature requests to
380 C<bug-dbix-class-recursiveput@rt.cpan.org>, or through the web interface at
381 L<http://rt.cpan.org>.
386 Zbigniew Lukasiak C<< <zby@cpan.org> >>
387 Influenced by code by Pedro Melo.
389 =head1 LICENCE AND COPYRIGHT
391 Copyright (c) 2008, Zbigniew Lukasiak C<< <zby@cpan.org> >>. All rights reserved.
393 This module is free software; you can redistribute it and/or
394 modify it under the same terms as Perl itself. See L<perlartistic>.
397 =head1 DISCLAIMER OF WARRANTY
399 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
400 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
401 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
402 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
403 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
404 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
405 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
406 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
407 NECESSARY SERVICING, REPAIR, OR CORRECTION.
409 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
410 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
411 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
412 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
413 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
414 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
415 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
416 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
417 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF