if ( blessed($updates) && $updates->isa('DBIx::Class::Row') ) {
return $updates;
}
-
-
# direct column accessors
my %columns;
}
$object ||= $self->new( {} );
-# first update columns and other accessors - so that later related records can be found
+ # first update columns and other accessors - so that later related records can be found
for my $name ( keys %columns ) {
$object->$name( $updates->{$name} );
}
_update_relation( $self, $name, $updates, $object, $info );
}
# $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;
$object->update_or_insert;
else {
my $sub_updates = $updates->{$name};
$sub_updates = { %$sub_updates, %$resolved } if $resolved && ref( $sub_updates ) eq 'HASH';
- my $sub_object =
- recursive_update( resultset => $related_result, updates => $sub_updates );
+ my $sub_object;
+ if( $info->{attrs}{accessor} eq 'single' && defined $object->$name ){
+ $sub_object = recursive_update(
+ resultset => $related_result,
+ updates => $sub_updates,
+ object => $object->$name
+ );
+ }
+ else{
+ $sub_object =
+ recursive_update( resultset => $related_result, updates => $sub_updates );
+ }
$object->set_from_related( $name, $sub_object );
}
}
$dsn ||= 'dbi:SQLite:dbname=t/var/dvdzbr.db';
warn "testing $dsn";
my $schema = __PACKAGE__->connect( $dsn, $user, $pass, {} );
- $schema->deploy({ });
+ $schema->deploy({ add_drop_table => 1, });
$schema->populate('User', [
[ qw/username name password / ],
[ 'jgda', 'Jonas Alves', ''],
--- /dev/null
+package DBSchema::Result::Address;
+
+use strict;
+use warnings;
+
+use base 'DBIx::Class';
+
+__PACKAGE__->load_components("Core");
+__PACKAGE__->table("address");
+__PACKAGE__->add_columns(
+ "address_id",
+ { data_type => "INTEGER", is_auto_increment => 1,
+ is_nullable => 0 },
+ "user_id",
+ { data_type => "INTEGER", is_nullable => 0 },
+ "street",
+ { data_type => "VARCHAR", is_nullable => 0, size => 32 },
+ "city",
+ { data_type => "VARCHAR", is_nullable => 0, size => 32 },
+ "state",
+ { data_type => "VARCHAR", is_nullable => 0, size => 32 },
+);
+__PACKAGE__->set_primary_key("address_id");
+
+__PACKAGE__->belongs_to(
+ 'user',
+ 'DBSchema::Result::User',
+ 'user_id',
+);
+
+
+1;
);
__PACKAGE__->many_to_many('roles', 'user_roles' => 'role');
+__PACKAGE__->might_have(
+ "address",
+ "DBSchema::Result::Address",
+ { 'foreign.user_id' => 'self.id' }
+);
+
1;
sub run_tests{
my $schema = shift;
- plan tests => 28;
+ plan tests => 29;
my $dvd_rs = $schema->resultset( 'Dvd' );
my $user_rs = $schema->resultset( 'User' );
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' );
+ $updates = {
+ id => $user->id,
+ address => {
+ street => "101 Main Street",
+ city => "Podunk",
+ state => "New York"
+ }
+ };
+ $user = $user_rs->recursive_update( $updates );
+ $user = $user_rs->recursive_update( $updates );
+ is( $schema->resultset( 'Address' )->search({ user_id => $user->id })->count, 1,
+ 'the right number of addresses' );
+
+
# $updates = {
# name => 'Test name 1',
# };
use RunTests;
use Test::More;
-unlink 't/var/dvdzbr.db';
+#unlink 't/var/dvdzbr.db';
my $schema = DBSchema::get_test_schema();
run_tests( $schema );