DBIx::Class Wrapping/overloading a column accessor

邮差的信 提交于 2019-12-12 14:42:02

问题


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:


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.



来源:https://stackoverflow.com/questions/11526999/dbixclass-wrapping-overloading-a-column-accessor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!