[CDBI] Re: has_many relationships and order_by

Dan Rowles d.rowles at outcometechnologies.com
Thu Dec 15 12:55:25 GMT 2005

Have sent Tony a patch for this, so hopefully he'll include it in the 
next release. My solution was that, if you only have a single hash 
passed in, check to see if it contains keys that are not column names.

I'm not sure what you mean by the "doesn't have support for ordering" 
part of your post. You can do "order_by" in searches - just pass in a 
hash-ref as the last argument, for example:-
   Class->search(name => "Bob", age => 12, { order_by => "weight" });

Of course, I might be completely missing the point here!



Matt S Trout wrote:
> On Wed, Dec 14, 2005 at 04:43:49PM -0500, Edward J. Sabol wrote:
>>>I've encountered a problem with has_many relationships in Class::DBI, after
>>>upgrading from 0.96 to 3.0.12. Once upon a time, calling:-
>>>     my $iter = $object->children({order_by => "name"})
>>>where "children" is a "has_many" relationship would work nicely, and you
>>>would get a list/iterator back which would contain your data, all nicely
>>>Now, however, I get an error stating that:- "order_by is not a column of
>>>I'm just wondering if this has been changed on purpose, or whether to
>>>report it as a bug. I can send a test case (using DBD::SQLite2) if
>>I'm guessing it's probably an unintended side effect of this line of code in
>>_hm_run_search() in Class::DBI::Relationship::HasMany:
>>    @search_args = %{ $search_args[0] } if ref $search_args[0] eq "HASH";
>>It's not clear to my why that line was added.
> In order to deal with
> $obj->has_many_method({ foo => 1 });
> instead of
> $obj->has_many_method(foo => 1);
> Class::DBI in and of itself doesn't have support for ordering etc. so it
> doesn't handle search attrs; not being able to pass attributes to has_many
> searches and similar, such as
> $obj->has_many_method({ foo => 1 }, { prefetch => 'some_might_have' });
> has been a substantial driver for people migrating from Class::DBI::Sweet
> to DBIx::Class, where this sort of feature set was designed into the core
> from the start (advocacy though this is, I'm pretty much directly quoting
> a number of my users here).

Dan Rowles
Outcome Technologies


t: +44 (0)207 656 2460
f: +44 (0)709 230 6588
m: +44 (0)798 076 8143
e: d.rowles at outcometechnologies.com
w: http://www.outcometechnologies.com
BUPA House
15-19 Bloomsbury Way
London WC1A 2BA

***This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they are
addressed. If you receive this message in error, please return it to the

More information about the ClassDBI mailing list