Re: Constructing objects in multiple CDBI classes w. single SELECT
[prev]
[thread]
[next]
[Date index for 2005/03/03]
> > My question is: could this feature be added to
> > Class::DBI::Relationship::HasMany, or should I make a new kind of
> > relationship? Or is there a better way to accomplish this?
>
> What you're talking about is caching, and it's fairly tricky to get
> right for multi-process environments. I would say it's safe enough to
> cache them for the length of a single web request, but caching them for
> the life of the object may not work for people using Class::DBI in a
> long-running script, or with volatile data. I think anything like this
> should be optional.
Yeah, the caching issue (in general) is a biggie. But I'm not proposing
that HasMany cache its results - only that it use the pre-constructed
results if they are available.
You can already do:
my $cd = Music::CD->construct({
cdid => 7,
artist => 3,
title => 'Mule Variations',
year => '1999',
});
I'm suggesting that you also be able to do:
my $cd = Music::CD->construct({
cdid => 7,
artist => 3,
title => 'Mule Variations',
year => '1999',
tracks => \@list_of_tracks;
});
One challenge here would be to discard the cached value of 'tracks'
whenever the $cd object reloads the rest of its columns.
> > If I'm going to create a generalized query mechanism, how can I figure
> > out how the classes are related to each other? For instance, how can I
> > know that the relationship between Music::Artist and Music::CD is
> > HasMany?
>
> There is class data that will tell you this. Look at the
> Class::DBI::Relationship::* source code.
Ah - thanks! It looks like I can do this:
use Data::Dumper;
# HasMany
print "Artist __hasa_list: \n", Dumper(Music::Artist->__hasa_list), "\n";
print "CD __hasa_list: \n", Dumper(Music::CD->__hasa_list), "\n";
print "Track __hasa_list: \n", Dumper(Music::Track->__hasa_list), "\n";
# HasA
print "Artist __hasa_rels: \n", Dumper(Music::Artist->__hasa_rels), "\n";
print "CD __hasa_rels: \n", Dumper(Music::CD->__hasa_rels), "\n";
print "Track __hasa_rels: \n", Dumper(Music::Track->__hasa_rels), "\n";
Thanks for the feedback!
Michael
--
Michael Graham <magog@xxxxxxxx.xxx>