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)){
$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
}
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 );
return;
}
-# Module implementation here
-
1; # Magic true value required at end of module
__END__
},
);
__PACKAGE__->set_primary_key('id');
-__PACKAGE__->belongs_to('owner', 'User', { id => 'owner' });
-__PACKAGE__->belongs_to('current_borrower', 'User', { id => 'current_borrower' });
+__PACKAGE__->belongs_to('owner', 'DBSchema::Result::User', { id => 'owner' });
+__PACKAGE__->belongs_to('current_borrower', 'DBSchema::Result::User', { id => 'current_borrower' });
__PACKAGE__->has_many('dvdtags', 'Dvdtag', { 'foreign.dvd' => 'self.id' });
+__PACKAGE__->has_many('viewings', 'Viewing', { 'foreign.dvd_id' => 'self.id' });
__PACKAGE__->many_to_many('tags', 'dvdtags' => 'tag');
__PACKAGE__->might_have(
liner_notes => 'DBSchema::Result::LinerNotes', undef,
{ proxy => [ qw/notes/ ] },
);
+__PACKAGE__->add_relationship('like_has_many', 'DBSchema::Result::Twokeys', { 'foreign.dvd_name' => 'self.name' }, { accessor_name => 'like_has_many' } );
1;
use strict;
use Test::More;
-
sub run_tests{
my $schema = shift;
my $dvd_rs = $schema->resultset( 'Dvd' );
- my $owner = $schema->resultset( 'User' )->first;
- my $initial_user_count = $schema->resultset( 'User' )->count;
+ my $user_rs = $schema->resultset( 'User' );
+
+ my $owner = $user_rs->next;
+ my $another_owner = $user_rs->next;
+ my $initial_user_count = $user_rs->count;
+
+ # creating new record linked to some old record
+
+ my $updates;
+ $updates = {
+ id => undef,
+ name => 'Test name 2',
+ viewings => [ { user_id => $owner->id } ],
+ owner => { id => $owner->id },
+ };
+
+ my $new_dvd = $dvd_rs->recursive_update( $updates );
+# my $new_dvd = $dvd_rs->create( $updates );
+ is ( $schema->resultset( 'User' )->count, $initial_user_count, "No new user created" );
+ is ( $new_dvd->name, 'Test name 2', 'Dvd name set' );
+ is ( $new_dvd->owner->id, $owner->id, 'Owner set' );
+# is ( $new_dvd->viewing->user_id, $owner->id, 'Viewing created' );
+;
# creating new records
my $updates = {
aaaa => undef,
tags => [ '2', { id => '3' } ],
name => 'Test name',
- # 'creation_date.year' => 2002,
- # 'creation_date.month' => 1,
- # 'creation_date.day' => 3,
- # 'creation_date.hour' => 4,
- # 'creation_date.minute' => 33,
- # 'creation_date.pm' => 1,
- owner => $owner->id,
+ owner => $owner,
current_borrower => {
name => 'temp name',
username => 'temp name',
password => 'temp name',
},
liner_notes => {
-
notes => 'test note',
- }
+ },
+ like_has_many => [
+ { key2 => 1 }
+ ],
};
my $dvd = $dvd_rs->recursive_update( $updates );
-
+;
is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "One new user created" );
is ( $dvd->name, 'Test name', 'Dvd name set' );
is_deeply ( [ map {$_->id} $dvd->tags ], [ '2', '3' ], 'Tags set' );
- #my $value = $dvd->creation_date;
- #is( "$value", '2002-01-03T16:33:00', 'Date set');
is ( $dvd->owner->id, $owner->id, 'Owner set' );
is ( $dvd->current_borrower->name, 'temp name', 'Related record created' );
is ( $dvd->liner_notes->notes, 'test note', 'might_have record created' );
-
+ ok ( $schema->resultset( 'Twokeys' )->find( { dvd_name => 'Test name', key2 => 1 } ), 'Twokeys created' );
+
# changing existing records
+ my $num_of_users = $user_rs->count;
$updates = {
id => $dvd->id,
aaaa => undef,
name => 'Test name',
tags => [ ],
- 'owner' => $owner->id,
+ 'owner' => $another_owner->id,
current_borrower => {
username => 'new name a',
name => 'new name a',
is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" );
is ( $dvd->name, 'Test name', 'Dvd name set' );
- is ( $dvd->owner->id, $owner->id, 'Owner set' );
+ is ( $dvd->owner->id, $another_owner->id, 'Owner updated' );
is ( $dvd->current_borrower->name, 'new name a', 'Related record modified' );
is ( $dvd->tags->count, 0, 'Tags deleted' );
]
};
- my $user_rs = $schema->resultset( 'User' );
my $user = $user_rs->recursive_update( $updates );
my %owned_dvds = map { $_->name => $_ } $user->owned_dvds;
is( scalar keys %owned_dvds, 2, 'Has many relations created' );
my @tags = $owned_dvds{'temp name 1'}->tags;
is( scalar @tags, 2, 'Tags in has_many related record saved' );
ok( $owned_dvds{'temp name 2'}, 'Second name in a has_many related record saved' );
+
}