我不確定我是否正確理解你,但如果我是,也許你想查看prefetch
功能來自動準備好調用那些其他相關的行對象。
例如,在Galileo
中,當列出所有頁面(文章)時,我使用此機制爲每個頁面對象(see here)獲取相關作者對象。
好吧,如果要將一個對象存儲在另一個對象中,您可能需要向對象中注入一些額外的數據。
UNTESTED:
## some initial checks (run these only once)
# check that method name is available
die "Cannot use method 'extra_data'" if $book->can('extra_data');
# check that the reftype is a hash
require Scalar::Util;
die "Incorrect underlying type" unless Scalar::Util::reftype($book) eq 'HASH';
# check that the key is available
die "Key unavailable" if exists $book->{'my_extra_data'};
{
no strict 'refs';
# create a simple accessor for a hash stored in an object
*{ ref($book) . '::extra_data' } = sub {
my $self = shift;
#return all extra data if called without args
return $self->{my_extra_data} unless @_;
my $key = shift;
if (@_) {
$self->{my_extra_data}{$key} = shift;
}
return $self->{my_extra_data}{$key};
};
}
$book->extra_data(author => $author);
#then later
my $stored_author = $book->extra_data('author');
這是一個很好的問題。您可能想在DBIx :: Class郵件列表上詢問它,開發者在這裏列出。在我看來,創作之後緩存作者對象在書的範圍內應該是可能的。但是,DBIC可能總是偏向於檢索相關記錄以確保新鮮度。 – 2012-08-15 20:04:39
發佈到DBIx :: Class郵件列表:http://lists.scsys.co.uk/pipermail/dbix-class/2012-August/010741.html – 2012-08-16 08:33:50
今天早上看到了。 – 2012-08-16 14:35:20