Subject: | Add check to see if relationship has loaded |
Date: | Thu, 18 Jun 2015 16:17:20 -0400 |
To: | bug-DBIx-Class [...] rt.cpan.org |
From: | Jon Rubin <jon.rubin [...] grantstreet.com> |
Hi,
I recently came across a case where I wanted to determine if a relationship
on a row was already loaded before accessing it. Frequently, the relation
would be a circular reference, and there was no reason to hit the database
if this was the case. However, if the relationship *was* already loaded,
then I wanted to return the loaded copy (eg. to preserve prefetch
functionality).
Maybe code will explain better:
sub get_original_foo
{
my ($self) = @_;
if ( $self->get_column('foo_id') ==
$self->get_column('original_foo_id') ) {
# If we've already prefetched the original_foo, return that.
# Otherwise we'll lose out on any benefits gained by prefetching
other
# DB objects through original_foo
# Ideally would check $self->has_relationship_loaded('original_foo')
return $self unless exists $self->{_relationship_data}{original_foo}
|| exists $self->{_inflated_column}{original_foo};
}
return $self->original_foo;
}
I've given the fix a go in the attached patch, written for
DBIx-Class-Row-0.082810
(it includes documentation and test updates).
FWIW, I'm running perl v5.10.1, on both Mac and Linux.
Let me know if there's an existing solution I've missed. Otherwise, if this
makes sense, I can submit the patch in git.
--
Jon Rubin
Grant Street Group
Ph: (412) 391-5555, Ext. 1323
Message body is not shown because sender requested not to inline it.