Catalyst 中 DBIx::Class::Schema 模型的 Moose 方法修饰符

发布于 2024-10-06 14:27:43 字数 1161 浏览 4 评论 0原文

对于任何给定的结果类 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 技术交流群。

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

发布评论

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

评论(1

酒与心事 2024-10-13 14:27:43

我明白了,您正在使用 MooseX::NonMoose。

鉴于此,我猜测您需要使用 FOREIGNBUILDARGS

around 'FOREGINBUILDARGS' => sub {
   my $orig = shift;
   my $class = shift;
   delete $_[0]->{not_a_real_column};
   return $class->$orig(@_);
};

“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

around 'FOREGINBUILDARGS' => sub {
   my $orig = shift;
   my $class = shift;
   delete $_[0]->{not_a_real_column};
   return $class->$orig(@_);
};

"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!

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