-sub recursive_update {
- my( $self, $updates ) = @_;
- my $object;
- $object = $self->find( $updates, { key => 'primary' } ) || $self->new( {} );
-
- for my $name ( keys %$updates ){
- if($object->can($name)){
- my $value = $updates->{$name};
-
- # updating relations that that should be done before the row is inserted into the database
- # like belongs_to
- if( $object->result_source->has_relationship($name)
- and
- ref $value
- ){
- my $info = $object->result_source->relationship_info( $name );
- if( $info and not $info->{attrs}{accessor} eq 'multi'
- and
- _master_relation_cond( $object, $info->{cond}, _get_pk_for_related( $object, $name ) )
- ){
- my $related_result = $object->related_resultset( $name );
- my $sub_object = $related_result->recursive_update( $value );
- $object->set_from_related( $name, $sub_object );
- }
+sub recursive_update {
+ my ( $self, $updates, $attrs ) = @_;
+
+ my $fixed_fields;
+ my $unknown_params_ok;
+
+ # 0.21+ api
+ if ( defined $attrs && ref $attrs eq 'HASH' ) {
+ $fixed_fields = $attrs->{fixed_fields};
+ $unknown_params_ok = $attrs->{unknown_params_ok};
+ }
+
+ # pre 0.21 api
+ elsif ( defined $attrs && ref $attrs eq 'ARRAY' ) {
+ $fixed_fields = $attrs;
+ }
+
+ return
+ DBIx::Class::ResultSet::RecursiveUpdate::Functions::recursive_update(
+ resultset => $self,
+ updates => $updates,
+ fixed_fields => $fixed_fields,
+ unknown_params_ok => $unknown_params_ok,
+ );
+}
+
+package DBIx::Class::ResultSet::RecursiveUpdate::Functions;
+use Carp::Clan qw/^DBIx::Class|^HTML::FormHandler|^Try::Tiny/;
+use Scalar::Util qw( blessed );
+use List::MoreUtils qw/ any /;
+
+sub recursive_update {
+ my %params = @_;
+ my ( $self, $updates, $fixed_fields, $object, $resolved,
+ $if_not_submitted, $unknown_params_ok )
+ = @params{
+ qw/resultset updates fixed_fields object resolved if_not_submitted unknown_params_ok/
+ };
+ $resolved ||= {};
+
+ my $source = $self->result_source;
+
+ # warn 'entering: ' . $source->from();
+ carp 'fixed fields needs to be an array ref'
+ if defined $fixed_fields && ref $fixed_fields ne 'ARRAY';
+
+ if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) {
+ return $updates;
+ }
+ if ( exists $updates->{id} ) {
+ # warn "finding object by id " . $updates->{id} . "\n";
+ $object = $self->find( $updates->{id}, { key => 'primary' } );
+ # warn "object not found by id\n"
+ # unless defined $object;
+ }
+
+ my %fixed_fields = map { $_ => 1 } @$fixed_fields
+ if $fixed_fields;
+ my @missing =
+ grep { !exists $updates->{$_} && !exists $fixed_fields{$_} }
+ $source->primary_columns;
+ if ( !$object && !scalar @missing ) {
+
+ # warn 'finding by: ' . Dumper( $updates ); use Data::Dumper;
+ $object = $self->find( $updates, { key => 'primary' } );
+ }
+ $updates = { %$updates, %$resolved };
+ @missing =
+ grep { !exists $resolved->{$_} } @missing;
+ if ( !$object && !scalar @missing ) {
+
+ # warn 'finding by +resolved: ' . Dumper( $updates ); use Data::Dumper;
+ $object = $self->find( $updates, { key => 'primary' } );
+ }
+
+ $object = $self->new( {} )
+ unless defined $object;
+
+ # warn Dumper( $updates ); use Data::Dumper;
+ # direct column accessors
+ my %columns;
+
+ # relations that that should be done before the row is inserted into the
+ # database like belongs_to
+ my %pre_updates;
+
+ # relations that that should be done after the row is inserted into the
+ # database like has_many, might_have and has_one
+ my %post_updates;
+ my %other_methods;
+ my %columns_by_accessor = _get_columns_by_accessor($self);
+
+ # warn 'resolved: ' . Dumper( $resolved );
+ # warn 'updates: ' . Dumper( $updates ); use Data::Dumper;
+ # warn 'columns: ' . Dumper( \%columns_by_accessor );
+ for my $name ( keys %$updates ) {
+ # columns
+ if ( exists $columns_by_accessor{$name}
+ && !( $source->has_relationship($name)
+ && ref( $updates->{$name} ) ) )
+ {
+
+ #warn "$name is a column\n";
+ $columns{$name} = $updates->{$name};
+ next;
+ }
+
+ # relationships
+ if ( $source->has_relationship($name) ) {
+ if ( _master_relation_cond( $self, $name ) ) {
+
+ #warn "$name is a pre-update rel\n";
+ $pre_updates{$name} = $updates->{$name};
+ next;