[Dbix-class] Serialize::Storable and dclone
stephen joseph butler
stephen.butler at gmail.com
Thu Apr 12 18:21:03 GMT 2007
THE PROBLEM:
I've been using Storable::dclone to make a clone of a Row before
updating it so that I can later find the differences (I'm using
DBIx::Class::HTMLWidget, so get_dirty_columns isn't available to me).
Here's what my code looks like (using Catalyst):
sub save {
my ( $self, $c, $id, $widget_result ) = @_;
my $new_obj = $c->model( 'DB::Object' )->find( $id );
my $old_obj = dclone( $new_obj );
$new_obj->populate_from_widget( $widget_result );
my %diff;
foreach ($old_obj->result_source->columns) {
next if $old_obj->get_column( $_ ) eq $new_obj->get_column( $_ );
$diff{ $_ } = {
old => $old_obj->$_,
new => $new_obj->$_
};
}
# render %diff with TT
}
But I keep getting this error whenever I change a column with a
foreign key relationship:
[error] Caught exception in DB::Controller::Object->save "Can't call
method "select_single" on an undefined value at
/usr/lib/perl5/site_perl/5.8.8/DBIx/Class/ResultSet.pm line 537."
Yes, my schema for DB::Object has this at the top:
__PACKAGE__->load_components("HTMLWidget", "Serialize::Storable", "Core");
Still, suspecting that result_source isn't getting set properly on a
thaw, I changed my code to:
sub save {
my ( $self, $c, $id, $widget_result ) = @_;
my $new_obj = $c->model( 'DB::Object' )->find( $id );
my $old_obj = dclone( $new_obj );
$old_obj->result_source( $new_obj->result_source );
# rest...
}
Am I doing something wrong? Is this work-around going to bite me in
the ass later?
WHAT I'M TRYING TO DO:
Each DB::Object has a DB::ObjectComment with it. Each time the user
submits edits I want to automatically generate a comment with what's
changed. Before, I was using get_columns before and after the
populate_from_widget() and this worked. But all my foreign keys showed
up as the actual value instead of the fetched row. I'd rather have the
row since I took the time to write pretty overloaded stringification
operators.
More information about the Dbix-class
mailing list