Preventing DBIx::Class from calling everything related to a new, not-yet-inserted row?
I have a parent/child relationship in my schema. I'd like to use very similar code to modify an existing parent as to create a new one. The edit case is easy to find the children:
my $parent = $resultset->find($parent_id);
my @children = $parent->children->all
However, in the new case, something weird happens:
my $parent = $resultset->new_result({});
my @children = 开发者_运维知识库$parent->children->all;
I expected to have @children
to be empty, but instead I got back all children, regardless of parent.
I can do something like this (for each related-record accessor, vomit):
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});
}
Please tell me the right way to do this, as the above mustn't be it.
version: 0.08010, because that's what Debian Lenny has (and what our production servers are running)
What version of DBIx::Class are you using? I'm running the most recent version, 0.08112, and I can't seem to find a new_record method for ResultSet. There is, however, a new_result method that looks like it would have the same effect as the new_record method you are using. I tried the following code and got an empty array, as expected:
my $parent = $resultset->new_result({});
my @children = $parent->children();
Also, according to the documentation for a has_many Relationship, the created accessor method will return objects in list context, so you don't need to call all. I did try it the way you have it though, and @children was still empty.
精彩评论