Catalyst 中 DBIx::Class::Schema 模型的 Moose 方法修饰符
对于任何给定的结果类 MySchema::Result::Foo (从生成的默认模式加载器构建) 使用 Moose/MooseX::nonmoose 的语法)
如果我添加 BUILDARGS 方法包装器来清理行的构造函数数据,如下所示:
package MySchema::Result::Foo;
use Moose;
use MooseX::NonMoose;
[etc ..]
around 'BUILDARGS' => sub {
my $orig = shift;
my $class = shift;
delete $_[0]->{not_a_real_column};
return $class->$orig(@_);
};
它在直接使用架构时有效。 例如,以下内容按预期工作:使用 real_column=>'value' 创建新行对象,并在调用 ->new 之前删除 not_a_real_column
use MySchema;
my $s = MySchema->connect('dbi:blahblahblah');
$s->resultset('Foo')->new({ real_column=>'value', not_a_real_column=>'some other thing' }); #win
但是,当通过 Catalyst::Model::DBIC::Schema 使用相同模式时顺序是 不同的。尝试创建新的 Foo 行对象时,以下操作失败,因为 not_a_real_column 无效。换句话说,在调用 ->new 之前,new 的参数不会通过 BUILDARGS 运行。
$c->model('MySchemaModel')->resultset('Foo')->new({ real_column=>'value', not_a_real_column=>'some other thing' }); #fails
有趣的是,如果我把 'new' => 括起来sub{} 而不是围绕 'BUILDARGS' => sub{} 两种情况下的行为是相同的,并且工作正常,但据我了解,穆斯教条规定永远不要搞乱 new。
有人愿意帮助我理解为什么会出现这种情况,或者是否有更好的方法?
For any given result class MySchema::Result::Foo (built from default schema loader generated
syntax which uses Moose/MooseX::nonmoose)
If I add a BUILDARGS method wrapper to sanitize the constructor data for a row like so:
package MySchema::Result::Foo;
use Moose;
use MooseX::NonMoose;
[etc ..]
around 'BUILDARGS' => sub {
my $orig = shift;
my $class = shift;
delete $_[0]->{not_a_real_column};
return $class->$orig(@_);
};
It works when using the schema directly.
For example the following works as expected: A new row object is created with real_column=>'value' and not_a_real_column removed before ->new is called
use MySchema;
my $s = MySchema->connect('dbi:blahblahblah');
$s->resultset('Foo')->new({ real_column=>'value', not_a_real_column=>'some other thing' }); #win
However, when using the same schema via Catalyst::Model::DBIC::Schema the order is
different. The following fails when trying to create a new Foo row object because
not_a_real_column is invalid. In other words the arguments to new are not run through BUILDARGS before ->new is called.
$c->model('MySchemaModel')->resultset('Foo')->new({ real_column=>'value', not_a_real_column=>'some other thing' }); #fails
Interestingly enough, if I wrap around 'new' => sub{} instead of around 'BUILDARGS' => sub{} the behavior is the same in both cases, and works fine, but to my understanding Moose dogma states to never mess with new.
Anyone care to help me understand why this is the case, or if there's a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我明白了,您正在使用 MooseX::NonMoose。
鉴于此,我猜测您需要使用 FOREIGNBUILDARGS
“MooseX::NonMoose 允许您通过定义 FOREIGNBUILLDARGS 方法来操作传递给超类构造函数的参数列表。”
http://metacpan.org/pod/MooseX::NonMoose
我真的希望这能起作用为你!
I see, you are using MooseX::NonMoose.
Given that, I have a guess that you need to use FOREIGNBUILDARGS
"MooseX::NonMoose allows you to manipulate the argument list that gets passed to the superclass constructor by defining a FOREIGNBUILDARGS method."
http://metacpan.org/pod/MooseX::NonMoose
I really hope this works for you!