【问题标题】:Preventing DBIx::Class from calling everything related to a new, not-yet-inserted row?防止 DBIx::Class 调用与新的尚未插入的行相关的所有内容?
【发布时间】:2009-11-12 22:42:44
【问题描述】:

我的架构中有父/子关系。我想使用非常相似的代码来修改现有的父级以创建一个新的父级。编辑案例很容易找到孩子:

my $parent = $resultset->find($parent_id);
my @children = $parent->children->all

但是,在新案例中,发生了一些奇怪的事情:

my $parent = $resultset->new_result({});
my @children = $parent->children->all;

我希望@children 为空,但我得到了所有个孩子,不管父母是谁。

我可以这样做(对于每个相关记录访问器,呕吐):

sub children {
    my $self = shift;

    my $res = $self->next::method(@_);
    my $parent_no = $self->get_column('parent_no');
    defined $parent_no ? $res : $res->search({1 => 2});

}

请告诉我这样做的正确方法,因为上面不能是它。

版本: 0.08010,因为这是 Debian Lenny 所拥有的(以及我们的生产服务器正在运行的)

【问题讨论】:

  • 为什么不使用 DBIx::Class::Tree 来维护父子关系?
  • @jrockway: (1) 在生产代码中使用实验模块似乎不是一个好主意; (2) parent 和 child 不是同一类型,它们是完全不同的表。这不是递归的父/子关系。

标签: perl orm dbix-class


【解决方案1】:

您使用的是哪个版本的 DBIx::Class?我正在运行最新版本 0.08112,但我似乎找不到 ResultSet 的 new_record 方法。但是,有一个 new_result 方法看起来与您正在使用的 new_record 方法具有相同的效果。我尝试了以下代码并得到了一个空数组,正如预期的那样:

my $parent = $resultset->new_result({});
my @children = $parent->children();

另外,根据documentation for a has_many 关系,创建的访问器方法将返回列表上下文中的对象,因此您不需要调用所有。不过,我确实按照您的方式尝试过,但@children 仍然是空的。

【讨论】:

  • 我使用的是 0.08010,因为这是 Debian Lenny(我们的生产服务器正在运行)中的内容。
  • 好吧,我做了一个快速的解决方法,直到我可以升级。我在更改日志中找到了该修复程序。
猜你喜欢
  • 2018-07-03
  • 2014-09-01
  • 1970-01-01
  • 2012-08-10
  • 1970-01-01
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多