【发布时间】: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