【问题标题】:DBIx::Class Wrapping/overloading a column accessorDBIx::Class 包装/重载列访问器
【发布时间】:2012-07-16 15:21:43
【问题描述】:

使用 DBIx::Class 我试图在更新或检索列的数据时对其进行操作。例如,在它进入数据库之前,我想对其进行加密,而每当它被访问时,我想对其进行解密。我在DBIx::Class::Manual::Cookbook 中关注这个例子,但是我似乎无法让它工作。我在我的用户模式中放置了以下内容。对于测试我只是使用名称列,我知道这没有意义:

__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;
}

我看不出我在做什么与食谱上说的有什么不同。谁能帮助我理解我做错了什么?谢谢!

【问题讨论】:

  • 它的功能就像我的名字功能不存在一样。
  • 您能否向我们展示一下无法正常工作的代码的 sn-p、您期望它返回什么以及它实际返回什么?
  • 我已经在我的环境中测试了该代码并且它按预期工作,所以我认为问题可能出在其他地方。如果您发布您用来测试的代码,或许还有定义name() 的整个包,我们应该能够提供帮助。

标签: perl accessor catalyst dbix-class


【解决方案1】:

DBIx::Class 有一个名为 FilterColumn 的组件。

CPAN 上有各种模块使用该组件,例如 DBIx::Class::EncodedColumnPassphraseColumn

如果您告诉我们您的用例是什么,我们可能会给您更多/更好的建议。

【讨论】:

  • 但是FilterColumn会影响一般插入或选择上的列吗?就像我插入具有多个列值的行一样,我的 FilterColumn 还会受到影响吗?
猜你喜欢
  • 1970-01-01
  • 2012-07-06
  • 1970-01-01
  • 2011-07-09
  • 2011-07-09
  • 2016-08-25
  • 2013-01-20
  • 1970-01-01
  • 2013-01-21
相关资源
最近更新 更多