X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fp5-DBIx-Class-ResultSet-RecursiveUpdate;a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSet%2FRecursiveUpdate.pm;h=a152dc02c21530f4d7d9fc306341ea6e4eb85bbf;hp=2dc40cba4f7ba698cddb0b5befa90d60a1420249;hb=264a28fa4c2fdab1244cdba5c9e606ce8c411c0a;hpb=7947dd6ee9711102d5fb3fc579316d830d610da1 diff --git a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm index 2dc40cb..a152dc0 100644 --- a/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm +++ b/lib/DBIx/Class/ResultSet/RecursiveUpdate.pm @@ -5,13 +5,30 @@ use version; $VERSION = qv('0.001'); use warnings; use strict; use Carp; +use Scalar::Util qw( blessed ); use base qw(DBIx::Class::ResultSet); sub recursive_update { - my( $self, $updates ) = @_; + my( $self, $updates, $fixed_fields ) = @_; + if( blessed( $updates ) && $updates->isa( 'DBIx::Class::Row' ) ){ + return $updates; + } my $object; - $object = $self->find( $updates, { key => 'primary' } ) || $self->new( {} ); +# warn 'cond: ' . Dumper( $self->{cond} ); use Data::Dumper; +# warn 'where: ' . Dumper( $self->{attrs}{where} ); use Data::Dumper; + my @missing = grep { !exists $updates->{$_} && !exists $fixed_fields->{$_} } $self->result_source->primary_columns; + if( defined $self->{cond} && $DBIx::Class::ResultSource::UNRESOLVABLE_CONDITION == $self->{cond} ){ + $self->{cond} = undef; + $self->{attrs}{where} = undef; + if( ! scalar @missing ){ + $object = $self->find( $updates, { key => 'primary' } ); + } + } + else{ + $object = $self->find( $updates, { key => 'primary' } ); + } + $object ||= $self->new( {} ); for my $name ( keys %$updates ){ if($object->can($name)){ @@ -46,6 +63,7 @@ sub recursive_update { $self->_delete_empty_auto_increment($object); # don't allow insert to recurse to related objects - we do the recursion ourselves $object->{_rel_in_storage} = 1; +# warn Dumper( $object->{_column_data} ); $object->update_or_insert; # updating relations that can be done only after the row is inserted into the database @@ -70,7 +88,7 @@ sub recursive_update { } elsif( $object->result_source->has_relationship($name) ){ my $info = $object->result_source->relationship_info( $name ); - # has many case + # has many case (and similar) if( ref $updates->{$name} eq 'ARRAY' ){ for my $sub_updates ( @{$updates->{$name}} ) { my $sub_object = $object->search_related( $name )->recursive_update( $sub_updates ); @@ -88,9 +106,14 @@ sub recursive_update { sub is_m2m { my( $self, $relation ) = @_; + my $rclass = $self->result_class; + # DBIx::Class::IntrospectableM2M + if( $rclass->can( '_m2m_metadata' ) ){ + return $rclass->_m2m_metadata->{$relation}; + } my $object = $self->new({}); if ( $object->can($relation) and - !$object->result_source->has_relationship($relation) and + !$self->result_source->has_relationship($relation) and $object->can( 'set_' . $relation) ){ return 1; @@ -100,6 +123,17 @@ sub is_m2m { sub get_m2m_source { my( $self, $relation ) = @_; + my $rclass = $self->result_class; + # DBIx::Class::IntrospectableM2M + if( $rclass->can( '_m2m_metadata' ) ){ + return $self->result_source + ->related_source( + $rclass->_m2m_metadata->{$relation}{relation} + ) + ->related_source( + $rclass->_m2m_metadata->{$relation}{foreign_relation} + ); + } my $object = $self->new({}); my $r = $object->$relation; return $r->result_source; @@ -155,8 +189,6 @@ sub _master_relation_cond { return; } -# Module implementation here - 1; # Magic true value required at end of module __END__