From: Michael G Schwern Date: Wed, 13 Feb 2008 07:41:26 +0000 (-0800) Subject: Fix create() in the same way as update() so it throws out the new values and X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1d7e89b8623b1ae271aac80651d16dc7c655b15a;p=dbsrgits%2FDBIx-Class-Historic.git Fix create() in the same way as update() so it throws out the new values and reloads them from the database on demand. --- diff --git a/lib/DBIx/Class/CDBICompat/LazyLoading.pm b/lib/DBIx/Class/CDBICompat/LazyLoading.pm index 0b6691b..e8ffbcc 100644 --- a/lib/DBIx/Class/CDBICompat/LazyLoading.pm +++ b/lib/DBIx/Class/CDBICompat/LazyLoading.pm @@ -20,14 +20,38 @@ sub update { my @dirty_columns = keys %{$self->{_dirty_columns}}; my $ret = $self->next::method(@_); - - delete $self->{_column_data}{$_} for @dirty_columns; - delete $self->{_inflated_column}{$_} for @dirty_columns; + $self->_clear_column_data(@dirty_columns); return $ret; } +# And again for create +sub create { + my $class = shift; + my($data) = @_; + + my @columns = keys %$data; + + my $obj = $class->next::method(@_); + return $obj unless defined $obj; + + my %primary_cols = map { $_ => 1 } $class->primary_columns; + my @data_cols = grep !$primary_cols{$_}, @columns; + $obj->_clear_column_data(@data_cols); + + return $obj; +} + + +sub _clear_column_data { + my $self = shift; + + delete $self->{_column_data}{$_} for @_; + delete $self->{_inflated_column}{$_} for @_; +} + + sub get_column { my ($self, $col) = @_; if ((ref $self) && (!exists $self->{'_column_data'}{$col}) diff --git a/t/cdbi-t/04-lazy.t b/t/cdbi-t/04-lazy.t index 1ca413c..39d3efd 100644 --- a/t/cdbi-t/04-lazy.t +++ b/t/cdbi-t/04-lazy.t @@ -13,7 +13,7 @@ BEGIN { next; } eval "use DBD::SQLite"; - plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 30); + plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 34); } INIT { @@ -80,7 +80,7 @@ eval { # Multiple false columns ok($@, $@); -# Test that update() throws out columns that changed +# Test that create() and update() throws out columns that changed { my $l = Lazy->create({ this => 99, @@ -88,7 +88,15 @@ ok($@, $@); oop => 3, opop => 4, }); - + + ok $l->db_Main->do(qq{ + UPDATE @{[ $l->table ]} + SET oop = ? + WHERE this = ? + }, undef, 87, $l->this); + + is $l->oop, 87; + $l->oop(32); $l->update; @@ -117,7 +125,15 @@ ok($@, $@); that => 2, orp => 1998, }); + + ok $l->db_Main->do(qq{ + UPDATE @{[ $l->table ]} + SET orp = ? + WHERE this = ? + }, undef, 1987, $l->this); + is $l->orp, '1987-01-01'; + $l->orp(2007); is $l->orp, '2007-01-01'; # make sure it's inflated $l->update;