[Dbix-class] Prefetch, DBIC, Relationships
Amiri Barksdale
amiri at metalabel.com
Tue Jun 23 21:05:44 GMT 2009
Hi folks.
I was chatting with ribasushi on irc today about the way to get
relationships with
$schema->source($source)->relationships/relationship_info, so I can
create a series of statements like this:
list_prefetch_allows => [
'cds', { cds => [qw/cd_to_producer tags tracks/] },
],
or like this:
list_prefetch_allows => [
'cd_to_producer', { cd_to_producer => [qw//] },
'tags', { tags => [qw//] },
'tracks', { tracks => [qw//] },
],
For a Catalyst::Helper::Controller I am writing. Now, I am not sure how
to implement ribasushi's advice, so I am posting the code here to
solicit feedback. It seems clumsy to me, and like ribasushi said,
I am sure I am doing it wrong, but I don't know how else to do it.
In a loop through $schema->sources, I do this:
for my $source ($schema->sources) {
my @list_prefetch_allows =
_return_has_many_list($schema->source($source)->_relationships);
@list_prefetch_allows = map {
my $ref = $_;
qq|'$ref->[0]', { |
. $ref->[0]
. qq| => [qw/|
. join (' ', map { $_->[0] }
_return_has_many_list($schema->source($ref->[1])->_relationships))
. qq|/] },\n\t\t|;
} @list_prefetch_allows;
The subroutine _return_has_many_list is this:
sub _return_has_many_list {
my ($relationships) = @_;
return grep { $relationships->{$_->[0]}->{attrs}->{accessor} =~
/multi/ } map { [$_, $relationships->{$_}->{source} ] } sort keys
%$relationships;
}
All feedback welcome!
Amiri
More information about the DBIx-Class
mailing list