I have an Order entity and an Address entity, and in my Schema::Result::Order
module I have a simple belongs to relationship:
__PACKAGE__->belongs_to( "address", 'Schema::Result::Address',
{ addressid => 'addressid' });
I run this code with DBIC_TRACE=1
:
my $order = $schema->resulset('Order')->find($id);
my $add1 = $order->address;
my $add2 = $order->address;
and I only see one SELECT ... FROM ADDRESS ...
query, so apparently the second $order->address
method is not hitting the database.
So this might be a simple question, but where is the address object getting cached? (in the $order
object?)
Secondly, is this caching configurable (i.e. can I configure DBIC to not cache these relationships)?
I think the way you have your order to address relationship you will only have one address to the order:
__PACKAGE__->belongs_to( "address", 'Schema::Result::Address',
{ addressid => 'addressid' });
If your order will have many addresses you will want:
__PACKAGE__->has_many( "address", 'Schema::Result::Address',
{ addressid => 'addressid' });
Then you can retrieve the addresses a number of ways:
my $address_rs = $order->search_related('address',{});
while(my $row = $address_rs->next) {
#$row has an address record
}
I am not sure how the caching works in this situation
my $order = $schema->resulset('Order')->find($id);
my $add1 = $order->address;
my $add2 = $order->address;
But if you access your address record like this:
my $address_rs = $order->search_related('address',{});
you can control it with your query attributes: