Re: [CDBI] order by a foreign key

[prev] [thread] [next] [Date index for 2005/08/16]

From: Marcello
Subject: Re: [CDBI] order by a foreign key
Date: 16:44 on 16 Aug 2005
Randal L. Schwartz ha scritto:
>>>>>>"Marcello" == Marcello  <m.romani@xxxxxxxx.xx> writes:
> 
> 
> Marcello> I have two tables, 'cd' and 'artist', whose model classes are
> Marcello> MyApp::Cd and MyApp::Artist.
> 
> Marcello> MyApp::Artist is stringified as 'name'
> 
> Marcello> The relationship is:
> Marcello> MyApp:Cd->has_a( artist => MyApp::Artist );
> 
> Marcello> MyApp::Cd uses Class::DBI::Plugin::RetrieveAll, so I can say:
> Marcello> my @cds = MyApp::Cd->retrieve_all_sorted_by( $order ); # e.g. title ASC
> 
> Marcello> And now to the point:
> Marcello> if $order == 'artist' the @cds list gets ordered by the artist *id*,
> Marcello> not by the artist name.
> 
> Marcello> What I would like to obtain is a list ordered by the foreign class's
> Marcello> 'stringify' column.
> 
> Dunno how to do it with that plugin, but using this little trick
> I came across one day...
> 
> my @ordered_cds = map $_->[0],
>   sort { $a->[1] cmp $b->[1] }
>   map [$_, $_->artist->stringify], MyApp::Cd->retrive_all;
> 

Nice!

It remainds me of a very good article of yours about sorting data in Perl...

But the problem with this technique is that we do in the application 
what is better done by the database: sorting records...

Nonetheless, seeing a problem from different perspectives is a Good Thing :)

Marcello

Marcello

_______________________________________________
ClassDBI mailing list
ClassDBI@xxxxx.xxxxxxxxxxxxxxxx.xxx
http://lists.digitalcraftsmen.net/mailman/listinfo/classdbi

[CDBI] order by a foreign key
Marcello 15:34 on 16 Aug 2005

Re: [CDBI] order by a foreign key
merlyn (Randal L. Schwartz) 15:48 on 16 Aug 2005

Re: [CDBI] order by a foreign key
Marcello 16:44 on 16 Aug 2005

Re: [CDBI] order by a foreign key
Hartmaier Alexander 16:00 on 16 Aug 2005

Re: [CDBI] order by a foreign key
Matt S Trout 16:08 on 16 Aug 2005

Re: [CDBI] order by a foreign key
Marcello 16:47 on 16 Aug 2005

Re: [CDBI] order by a foreign key
Matt S Trout 17:04 on 16 Aug 2005

Re: [CDBI] order by a foreign key
Marcello 07:33 on 17 Aug 2005

Generated at 13:17 on 19 Aug 2005 by mariachi v0.52