[Dbix-class] How to find updated fields
Scott Thomson
smoothhound at gmail.com
Fri Mar 23 15:43:22 GMT 2007
I've done something very similar before and this is what I ended up up
with in my component...
sub update {
my $self = shift;
my %dirty_cols = $self->get_dirty_columns();
my $result = $self->next::method( @_ );
$self->_audit(\%dirty_cols);
return $result;
}
sub insert {
my $self = shift;
my $result = $self->next::method( @_ );
$self->_audit({$self->get_columns()});
return $result;
}
sub _audit {
my $self = shift;
my $dirty_cols = shift;
my $rs = $self->result_source();
while (my ($col, $val) = each %$dirty_cols) {
if ($rs->column_info($col)->{'audit'}) {
# create audit trail entry (snipped)
}
}
}
delete is exactly the same as insert. Control of whether a column is
audited is in the class definition e.g.
__PACKAGE__->add_columns('foo' => {'audit' => 1});
And yes, writing a component is *very* easy and IMHO it's one of the
features that makes DBIx::Class great :-)
HTH
Scott.
On 3/23/07, Guillermo Roditi <groditi at gmail.com> wrote:
> yeah, that's my distribution. and really, its not that hard. just make
> the file and overload the appropriate subs, load the component and
> ta-da
>
> On 3/23/07, RA Jones <ra.jones at dpw.clara.co.uk> wrote:
> > Guillermo Roditi wrote:
> > > That's the code I would recommend using for a DBIx::Class component,
> > > which you would have to build, then load. this is a lot simpler than
> > > it seems. see DBIx::Class::DigestColumns on the 'pan for an example of
> > > how to build a DBIC component.
> > This is a module you assisted in development? Are you suggesting I have
> > to write my own module to be able to use the get_dirty_columns method?
> >
> > > I can't really go through this exercise with you, but I assure you
> > > it's not ThatHard. it just takes some diving in.
> > I'm quite prepared to 'dive in' if that is what is necessary. But I feel
> > sure it *will* be ThatHard!
> >
> > > On 3/23/07, RA Jones <ra.jones at dpw.clara.co.uk> wrote:
> > >> Guillermo Roditi wrote:
> > >> > I think you could possibly implement this as a component that wraps
> > >> > update, delete, or insert and the use get_dirty_columns et al.
> > >> >
> > >> > eg
> > >> >
> > >> > sub update{
> > >> > my ( $self, $upd, @rest ) = @_;
> > >> > my %cols = $obj->get_dirty_columns;
> > >> > %cols = (%cols, %$upd) if( ref $upd );
> > >> > warn( "Updated: ". join(", ", keys %cols) );
> > >> > $self->next::method($upd, @rest);
> > >> > }
> > >>
> > >> Well I cannot get this to work. I'm using catalyst and formbuilder, and
> > >> doing the update like:
> > >>
> > >> $form->update($fields);
> > >>
> > >> That works OK, but if I try to capture the result of the action:
> > >>
> > >> my $r = $form->update($fields);
> > >>
> > >> and dump $r to file, get_dirty_columns does not exist, the closest is
> > >> _dirty_columns which is an undefined hashref:
> > >>
> > >> $VAR1 = bless( {
> > >> 'related_resultsets' => {},
> > >> '_orig_ident' => undef,
> > >> '_dirty_columns' => {},
> > >> etc,
> > >>
> > >> Am I missing something obvious here?
> > >>
> > >> > On 3/22/07, RA Jones <ra.jones at dpw.clara.co.uk> wrote:
> > >> >> Rob Kinyon wrote:
> > >> >> > On 3/22/07, RA Jones <ra.jones at dpw.clara.co.uk> wrote:
> > >> >> >> In my applications I frequently record in a log file which fields
> > >> >> >> were updated by which users. Is there an easy way in DBIC to find
> > >> >> >> out which fields got updated during an update() or
> > >> >> >> update_or_insert() action? Thanks.
> > >> >> >
> > >> >> > Look for the get_dirty_columns() method on the Row object.
> > >> >> Thanks, I had just scanned the DBIC::Row manual, and managed to miss
> > >> >> that one - is_changed() looked promising, but it appears to be only
> > >> for
> > >> >> uncommitted changes.
> > >> >> --
> > >> --
> > >> Richard Jones
> > >> Leeds, UK
> > >> ra.jones(at)dpw.clara.co.uk
> > >>
> > >> _______________________________________________
> > >> List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> > >> Wiki: http://dbix-class.shadowcatsystems.co.uk/
> > >> IRC: irc.perl.org#dbix-class
> > >> SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
> > >> Searchable Archive:
> > >> http://www.mail-archive.com/dbix-class@lists.rawmode.org/
> > >>
> > >
> > > _______________________________________________
> > > List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> > > Wiki: http://dbix-class.shadowcatsystems.co.uk/
> > > IRC: irc.perl.org#dbix-class
> > > SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
> > > Searchable Archive:
> > > http://www.mail-archive.com/dbix-class@lists.rawmode.org/
> > >
> > >
> > >
> >
> >
> > --
> > Richard Jones
> > Leeds, UK
> > mailto:ra.jones at dpw.clara.co.uk
> >
> > _______________________________________________
> > List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> > Wiki: http://dbix-class.shadowcatsystems.co.uk/
> > IRC: irc.perl.org#dbix-class
> > SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
> > Searchable Archive: http://www.mail-archive.com/dbix-class@lists.rawmode.org/
> >
>
> _______________________________________________
> List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> Wiki: http://dbix-class.shadowcatsystems.co.uk/
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
> Searchable Archive: http://www.mail-archive.com/dbix-class@lists.rawmode.org/
>
More information about the Dbix-class
mailing list