[CDBI] Preloading objects when using a view

Bill Moseley moseley at hank.org
Wed Mar 29 01:10:00 BST 2006


Looking to see if anyone has already implemented this (other than mst
in CDBI::Sweet).

I have a view that joins CD and Artist.  One of the columns in
the view is an artist id.  So, as is typical, I have:

    __PACKAGE__->has_a( artist => 'Music::Artist' );

I can then do $view_obj->artist->name, but it will hit the database
again to lookup the artist name.

Now, say, the view also returns the artist name.  What I'd like to do
is have CDBI populate the "name" column of the artist object it
created as a result of the search on the view class.  That way I'd
avoid a second trip to the database.

It would be nice to do something like:

    __PACKAGE__->has_a( artist_name => ['Music::Artist', 'name' ] );

Anyone doing something like this?


Why not just have an "artist_name" column as part of the view class?

    $view_obj->artist_name;

Because I want to pass $view_obj->artist to existing code that expects
to be able to do $artist->name.


Now I'm off to dig through CDBI::Sweet....



-- 
Bill Moseley
moseley at hank.org





More information about the ClassDBI mailing list