my %columns_by_accessor = _get_columns_by_accessor( $self );
for my $name ( keys %$updates ) {
my $source = $self->result_source;
+ if( $name eq 'id'
+# && scalar @{$source->primary_columns} == 1
+ && !$source->has_column( 'id' )
+ ){
+ my @ids = ( $updates->{id} );
+ if( ref $updates->{id} ){
+ @ids = @{ $updates->{id} };
+ }
+ my $i = 0;
+ for my $key ( $source->primary_columns ){
+ $columns{ $key } = $ids[ $i++ ];
+ }
+ next;
+ }
if ( $columns_by_accessor{$name}
&& !( $source->has_relationship($name) && ref( $updates->{$name} ) )
)
$object = $self->find( \%columns, { key => 'primary' } );
}
$object ||= $self->new( {} );
-
# first update columns and other accessors - so that later related records can be found
for my $name ( keys %columns ) {
- $object->$name( $updates->{$name} );
+ $object->$name( $columns{$name} );
}
for my $name ( keys %other_methods) {
$object->$name( $updates->{$name} ) if $object->can( $name );
my ($pk) = _get_pk_for_related( $self, $name);
my @rows;
my $result_source = $object->$name->result_source;
- for my $elem ( @{ $updates->{$name} } ) {
+ my @updates;
+ if( ref $updates->{$name} ){
+ @updates = @{ $updates->{$name} };
+ }
+ else{
+ @updates = ( $updates->{$name} );
+ }
+ for my $elem ( @updates ) {
if ( ref $elem ) {
push @rows, $result_source->resultset->find($elem);
}
use base 'DBIx::Class';
use overload '""' => sub {$_[0]->name}, fallback => 1;
-use lib '../../DBIx-Class-HTML-FormFu/lib/';
__PACKAGE__->load_components(qw/IntrospectableM2M Core/);
__PACKAGE__->table('dvd');
__PACKAGE__->add_columns(
- 'id' => {
+ 'dvd_id' => {
data_type => 'integer',
is_auto_increment => 1
},
is_nullable => 1,
},
);
-__PACKAGE__->set_primary_key('id');
+__PACKAGE__->set_primary_key('dvd_id');
__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', 'DBSchema::Result::Viewing', { 'foreign.dvd_id' => 'self.id' });
+__PACKAGE__->has_many('dvdtags', 'Dvdtag', { 'foreign.dvd' => 'self.dvd_id' });
+__PACKAGE__->has_many('viewings', 'DBSchema::Result::Viewing', { 'foreign.dvd_id' => 'self.dvd_id' });
__PACKAGE__->many_to_many('tags', 'dvdtags' => 'tag');
__PACKAGE__->might_have(
liner_notes => 'DBSchema::Result::LinerNotes', undef,
"tag" => { data_type => 'integer' },
);
__PACKAGE__->set_primary_key("dvd", "tag");
-__PACKAGE__->belongs_to("dvd", "DBSchema::Result::Dvd", { id => "dvd" });
+__PACKAGE__->belongs_to("dvd", "DBSchema::Result::Dvd", { dvd_id => "dvd" });
__PACKAGE__->belongs_to("tag", "DBSchema::Result::Tag", { id => "tag" });
1;
__PACKAGE__->belongs_to(
dvd => 'DBSchema::Result::Dvd',
- {'foreign.id'=>'self.dvd_id'},
+ {'foreign.dvd_id'=>'self.dvd_id'},
);
;
sub run_tests{
my $schema = shift;
- plan tests => 29;
+ plan tests => 30;
my $dvd_rs = $schema->resultset( 'Dvd' );
my $user_rs = $schema->resultset( 'User' );
my $initial_user_count = $user_rs->count;
my $initial_dvd_count = $dvd_rs->count;
my $updates;
-
+
# creating new record linked to some old record
$updates = {
name => 'Test name 2',
my $num_of_users = $user_rs->count;
$updates = {
- id => $dvd->id,
+ id => $dvd->dvd_id, # id instead of dvd_id
aaaa => undef,
name => undef,
tags => [ ],
},
};
- $dvd = $dvd_rs->recursive_update( $updates );
-
+ my $dvd_updated = $dvd_rs->recursive_update( $updates );
+
+ is ( $dvd_updated->dvd_id, $dvd->dvd_id, 'Pk from "id"' );
is ( $schema->resultset( 'User' )->count, $initial_user_count + 1, "No new user created" );
- is ( $dvd->name, undef, 'Dvd name deleted' );
- 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' );
- is ( $dvd->liner_notes->notes, 'test note changed', 'might_have record changed' );
+ is ( $dvd_updated->name, undef, 'Dvd name deleted' );
+ is ( $dvd_updated->owner->id, $another_owner->id, 'Owner updated' );
+ is ( $dvd_updated->current_borrower->name, 'new name a', 'Related record modified' );
+ is ( $dvd_updated->tags->count, 0, 'Tags deleted' );
+ is ( $dvd_updated->liner_notes->notes, 'test note changed', 'might_have record changed' );
# repeatable