使用 DBIX::Class 时 Moose 触发器不触发

发布于 2024-08-23 14:54:53 字数 1474 浏览 11 评论 0原文

我是 Moose 的新手,正在尝试将它与 DBIx::Class 一起使用。基本的 DBIC 查询和更新工作找到了,但是当我修改属性时,我尝试编写的任何触发器都不会被执行。

use Modern::Perl;
use Data::Dumper;

my $schema = My::Schema->connect(<connect str>, <usr>, <psw>) or die $!;
my $rs = $schema->resultset('Isin')->search( sid => 3929 );
my $security_obj = $rs->first;
print $security_obj->isin, "\n";
$security_obj->isin('Test1Foo'); # <- expect to see FOO printed by trigger
print $security_obj->isin, "\n";

我希望看到“isin”打印“FOO”的触发器,但什么也没有发生。如果我从包中删除 DBIx::Class ,触发器将按预期执行。

我怀疑 DBIx::Class 正在以阻止触发器触发的方式设置该值。

不幸的是,我没有找到关于将 DBIx::Class 与 Moose 一起使用的资源。我所写的内容主要基于我在 DBIx::Class 和 Moose 中找到的内容。

我使用 DBIx::Class 和/或 Moose 是否错误?我应该对 Moose 使用不同的 ORM 吗?

带有不会触发的触发器的包:

package My::Schema::Result::Isin;

use DBIx::Class;
use Moose;
use Carp;
extends 'DBIx::Class';

has 'isin'   => ( is => "rw", isa => "Str", trigger => \&_mod_isin);
has 'sid'    => ( is => "ro", isa => "Int");

sub _mod_isin {
    print "FOO\n";
    return;
};

 no Moose;

__PACKAGE__->load_components('Core');

__PACKAGE__->table('isin');

__PACKAGE__->add_columns(
  isin  => { data_type => 'varchar2', size => 12 },
  sid   => { data_type => 'integer',  size => 6 },
);

__PACKAGE__->set_primary_key('isin');

I am new to Moose and am trying to use it with DBIx::Class. Basic DBIC querying and updating work find, but any trigger I attempt to write does not get executed when I modify an attribute.

use Modern::Perl;
use Data::Dumper;

my $schema = My::Schema->connect(<connect str>, <usr>, <psw>) or die $!;
my $rs = $schema->resultset('Isin')->search( sid => 3929 );
my $security_obj = $rs->first;
print $security_obj->isin, "\n";
$security_obj->isin('Test1Foo'); # <- expect to see FOO printed by trigger
print $security_obj->isin, "\n";

I expect to see the trigger for 'isin' print 'FOO', but nothing happens. If I strip out DBIx::Class from the package the trigger is executed as expected.

I suspect that DBIx::Class is setting the value in a way that prevents the trigger from firing.

Unfortunately, I haven't had much luck finding resources about using DBIx::Class with Moose. What I have written is mostly based on what I found at DBIx::Class and Moose.

Am I using DBIx::Class and/or Moose wrong? Is there a different ORM that I should be using with Moose?

The package with the trigger that won't fire:

package My::Schema::Result::Isin;

use DBIx::Class;
use Moose;
use Carp;
extends 'DBIx::Class';

has 'isin'   => ( is => "rw", isa => "Str", trigger => \&_mod_isin);
has 'sid'    => ( is => "ro", isa => "Int");

sub _mod_isin {
    print "FOO\n";
    return;
};

 no Moose;

__PACKAGE__->load_components('Core');

__PACKAGE__->table('isin');

__PACKAGE__->add_columns(
  isin  => { data_type => 'varchar2', size => 12 },
  sid   => { data_type => 'integer',  size => 6 },
);

__PACKAGE__->set_primary_key('isin');

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分開簡單 2024-08-30 14:54:53

首先,您面临着从 Moose 内部扩展非 Moose 类的问题。这是一个问题,因为 DBIx::Class 不是从 Moose::Object 继承的,因此您不会像 does 那样获得标准的 Moose 方法。请参阅 Moose::Cookbook::Basics::Recipe11 来解决这个问题。

其次,你有一个更大的问题,你有两组不同的魔法试图为你创建子例程。您有 Moose,它的魔法创建了 isinsid 子例程,您还有 DBIx::Class,它的魔法还创建了 isinsid 子例程取代了 Moose 创建的子例程。

您可能希望在 Moose 角色中使用 around 修饰符进行组合,如 jrockway 建议的那样。

First, you have the problem of extending a non-Moose class from within Moose. This is a problem because DBIx::Class doesn't inherit from Moose::Object, so you won't get the standard Moose methods like does. See Moose::Cookbook::Basics::Recipe11 for solving this problem.

Second, you have the bigger problem that you have two different sets of magic which are trying to create subroutines for you. You have Moose, whose magic creates isin and sid subroutines, and you have DBIx::Class, whose magic also creates isin and sid subroutines which replace the ones that Moose created.

You might want to compose in a Moose Role with an around modifier, as jrockway suggested.

离去的眼神 2024-08-30 14:54:53

您是否尝试过使用 writer => \&_mod_isin 而是?

Have you tried using writer => \&_mod_isin instead?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文