3

Using DBIx::Class I am trying to manipulate the data of a column whenever it is being updated or retrieved. For instance, before it goes into the database I would like to encrypt it, and whenever it is being accessed I would like to decrypt it. I am following this example in the DBIx::Class::Manual::Cookbook, however I can't seem to get it to work. I have placed the following in my User schema. For testing I am just using the name column, I know it doesn't make sense:

__PACKAGE__->add_columns("name" => { accessor => '_name' });

sub name {
    my $self = shift;

    # If there is an update to the column, we'll let the original accessor
    # deal with it.
    if(@_) {
        return $self->_name('test 1');
    }

    # Fetch the column value.
    my $name = $self->_name;
    $name = 'test 2';
    return $name;
}

I can't see what I'm doing any different than what the cookbook says. Can't anyone help me understand what I'm doing wrong? Thanks!

srchulo
  • 5,143
  • 4
  • 43
  • 72
  • It just does functions as if my name function is not there. – srchulo Jul 17 '12 at 23:22
  • Can you show us a snippet of the code that isn't working, what you expect it to return, and what it's actually returning? – RickF Jul 24 '12 at 15:43
  • I've tested that code in my environment and it worked as expected, so I think it's likely the problem's somewhere else. If you post up the code you're using to test this, and perhaps the whole package where `name()` is defined we should be able to help. – edb Jul 31 '12 at 06:51

1 Answers1

1

DBIx::Class has a component for that called FilterColumn.

There are various modules on CPAN using that component like DBIx::Class::EncodedColumn and PassphraseColumn.

If you tell us what you use case is we might give you more/better suggestions.

Alexander Hartmaier
  • 2,178
  • 12
  • 21
  • But will FilterColumn affect the column on a general insert or select? Like if I insert a rows with multiple column values, will my FilterColumn still be affected? – srchulo Aug 12 '12 at 20:47