[Dbix-class] How to find updated fields
RA Jones
ra.jones at dpw.clara.co.uk
Thu Mar 29 13:50:32 GMT 2007
RA Jones wrote:
> So I created a very simple overloaded insert() method as described in
> DBIx::Class::Manual::Component, in a new module called DBIx::Class::CRUD
>
> package DBIx::Class::CRUD;
> use base qw/DBIx::Class/;
>
> sub update {
> my $self = shift;
>
> my %dirty_cols = $self->get_dirty_columns;
> use Data::Dumper; # open $fh here
> print $fh Dumper %dirty_cols;
> return $self->next::method( @_ );
> }
>
> And in MyApp::Schema::MyClass:
> __PACKAGE__->load_components(qw/PK::Auto CRUD Core/);
>
> But %dirty_cols is empty. The DBIC::CRUD::insert method *is* being
> called as a) I can dump $self to output, where there is no entry for
> anything resembling get_dirty_columns, and b) entry confirmed with
> $c->log->info( join "\n" => Class::C3::calculateMRO('Schema::MyClass') ):
Having completely failed to find a way to make get_dirty_columns work, I
implemented the following alternate method of logging updated fields:
In MyApp::Controller::Locations :-
sub do_edit : Private {
my ( $self, $c ) = @_;
my $fields = $form->field;
my $old_vals = { map { $_ => $location->$_ } $location->columns };
# do the table update, then:
$c->controller('Root')->record_changes($c, $old_vals, $fields);
}
In Root::Controller :-
sub record_changes : Private {
my ( $self, $c, $old, $new ) = @_;
map {
my $vals = {
old_value => $old->{$_},
new_value => $new->{$_},
dbid => $old->{id},
field => $_,
};
$c->model('Schema::Log')->create($vals);
} grep $old->{$_} ne $new->{$_}, keys %{ $new };
It does everything I wanted (so far). Comments welcome.
--
Richard Jones
Leeds, UK
ra.jones(at)dpw.clara.co.uk
More information about the Dbix-class
mailing list