[CDBI] order by a foreign key

Matt S Trout dbix-class at trout.me.uk
Tue Aug 16 17:08:11 BST 2005


On Tue, Aug 16, 2005 at 05:34:23PM +0200, Marcello wrote:
> Hallo everybody,
>     I'm working with Class-DBI in a catalyst project, and I found a 
> problem that I think is not directly related to Catalyst, but to 
> Class-DBI (or perhaps to Class::DBI::Plugin::RetrieveAll).
> 
> Let me first describe the scenario:
> 
> I have two tables, 'cd' and 'artist', whose model classes are MyApp::Cd 
> and MyApp::Artist.
> 
> MyApp::Artist is stringified as 'name'
> 
> The relationship is:
> MyApp:Cd->has_a( artist => MyApp::Artist );
> 
> MyApp::Cd uses Class::DBI::Plugin::RetrieveAll, so I can say:
> my @cds = MyApp::Cd->retrieve_all_sorted_by( $order ); # e.g. title ASC

Try Class::DBI::Sweet as a base instead of Class::DBI and

MyApp::Cd->search({ }, { prefetch => 'artist', order_by => 'artist.name' });

That should do the trick - and return your $cd->artist objects prepopulated
ala ActiveRecord's :include to boot.

-- 
     Matt S Trout           Website: http://www.shadowcatsystems.co.uk
  Technical Director        E-mail:  mst (at) shadowcatsystems.co.uk
Shadowcat Systems Ltd.

 + Help us build a better perl ORM: http://dbix-class.shadowcatsystems.co.uk/ +




More information about the ClassDBI mailing list