Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DBIx::Class Wrapping/overloading a column accessor

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!

like image 404
srchulo Avatar asked Nov 25 '25 02:11

srchulo


1 Answers

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.

like image 71
Alexander Hartmaier Avatar answered Nov 26 '25 16:11

Alexander Hartmaier