Re: [CDBI] How to sort "has_many" relation after I've declared it?
David McMath <mcdave-FGKo4X94FMn2fBVCVOL8/[email protected]> Tue, 21 Aug 2012 10:35:29 -0500
| Newsgroups | gmane.comp.lang.perl.modules.class-dbi |
|---|---|
| Message-ID | <[email protected]> |
Thanks for your response.
On 08/20/2012 06:40 PM, Daniel Williams wrote:
> Maybe take a lookg at
> http://search.cpan.org/~phred/Class-DBI-Sweet-0.11/lib/Class/DBI/Sweet.pm
On 08/20/2012 07:30 PM, Edward J. Sabol wrote:
>
> Based on my reading of the current code, I think it would be worth trying the
> following:
>
> my @s = $d->schools({ { order_by => 'name' } });
>
> I.e., pass the order_by in a hash of a hash instead of a single-level hash.
The latter ended up complaining that "HASH(0x13aa9ae0) is not a column
of School".
I looked at Class::DBI::Sweet as well. I don't have the guts to change
my base class today but it looks a bit more promising
I think, however, the real issue for me is the HasMany class and the way
it sets up the arguments to "search". So a proper extension would be to
make a new "HasManyWithOptions" or something. I didn't particularly
think it through because I hacked together the following pragmatic
solution in my "District" class:
> __PACKAGE__->has_many( schools => 'School' ) ;
>
> sub schools_orderby {
> my $self = shift ;
> my @orderColumns = grep { ( my $o = $_) =~ s/\s*(DESC|ASC)// ; CMS::School->find_column( $o ) } @_ ;
> ## "order by" nothing is just the list.
> return $self->schools unless @orderColumns ;
> ## We know there's some list of fields.
> my $orderClause = join( ',', @orderColumns ) ;
> return School->retrieve_from_sql( <<SQL, $self ) ;
> WHERE district_id=?
> ORDER BY $orderClause
> SQL
> }
Thanks for the advice,
dave