3
votes

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!

1
It just does functions as if my name function is not there. - srchulo
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
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

1 Answers

1
votes

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.