Skip Menu |

This queue is for tickets about the Class-DBI CPAN distribution.

Report information
The Basics
Id: 16493
Status: open
Priority: 0/
Queue: Class-DBI

People
Owner: Nobody in particular
Requestors: d.rowles [...] outcometechnologies.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: v3.0.9
Fixed in: (no value)



Subject: Bug in Class::DBI::Relationship::HasMany
Hi there, I reference my post to the Class::DBI mailing list, with MessageId <43A0775C.1000904@outcometechnologies.com>. Since upgrading from Class::DBI 0.96 to 3.0.12, I have noticed that when requesting a list/iterator of objects via a "has_many" relationship (for example, my @actors = $film->actors()), passing in an "order_by" argument no longer works. Specifically, calling "my @actors = $film->actors()" will work fine, however calling "my @actors = $film->actors({order_by => 'name'})" will not work, and dies with an error along the lines of "order_by is not a column of Actor". I attach a patch for "t/09-has_many.t", which amends this test to demonstrate the bug. Problem exists on perl 5.8.3.
diff -ur Class-DBI-v3.0.12.old/t/09-has_many.t Class-DBI-v3.0.12.new/t/09-has_many.t --- Class-DBI-v3.0.12.old/t/09-has_many.t 2005-10-23 15:06:37.000000000 +0100 +++ Class-DBI-v3.0.12.new/t/09-has_many.t 2005-12-14 22:14:19.000000000 +0000 @@ -3,7 +3,7 @@ BEGIN { eval "use DBD::SQLite"; - plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 42); + plan $@ ? (skip_all => 'needs DBD::SQLite for testing') : (tests => 45); } use lib 't/testlib'; @@ -79,6 +79,18 @@ is $actors[1]->Name, $pvj->Name, "PVJ first"; } + +# Test that "order_by" can be passed into the actors() method too +# +{ + my @actors = $btaste->actors({order_by => "name DESC"}); + is @actors, 2, " - so now we have 2"; + is $actors[0]->Name, $pvj->Name, "PVJ first due to ordering"; + is $actors[1]->Name, $pj->Name, "PJ second due to ordering"; +} + + + eval { my @actors = $btaste->actors({Name => $pj->Name}); is @actors, 1, "One actor from restricted (sorted) has_many";
Date: Wed, 14 Dec 2005 22:58:13 +0000
From: Tony Bowden <tony [...] tmtm.com>
To: Guest via RT <bug-Class-DBI [...] rt.cpan.org>
Subject: Re: [cpan #16493] Bug in Class::DBI::Relationship::HasMany
RT-Send-Cc:
On Wed, Dec 14, 2005 at 05:20:57PM -0500, Guest via RT wrote: Show quoted text
> I reference my post to the Class::DBI mailing list, with MessageId <43A0775C.1000904@outcometechnologies.com>.
I'm not on the mailing list, so a messageid isn't much use to me. If the message is important, then a URL to the archive would be useful. Show quoted text
> Since upgrading from Class::DBI 0.96 to 3.0.12, I have noticed that > when requesting a list/iterator of objects via a "has_many" relationship > (for example, my @actors = $film->actors()), passing in an "order_by" > argument no longer works.
If this worked previously, then I believe it to have been an accident. It certainly isn't documented to work. Currently this only works as a compile-time option, not a run-time one: Film->has_many(actors => Actor, { order_by => 'name' }); I'll certainly look at patches to add the functionality you describe, but at the minute it's not meant to work. Thanks, Tony
[d.rowles@outcometechnologies.com - Wed Dec 14 19:46:42 2005]: Show quoted text
> I enclose a patch that restores the ability to specify "order_by" > when retrieving data via a has_many relationship. The patch also > includes the additional test cases that I sent you with the original > bug report. > Please let me know if the patch is OK, or if you would like me to > make any other changes.
Thanks for this. To be honest, I'm having a little difficulty following the logic of the patch. It seems like quite a complex set of checks you're adding. If you can talk me through your thinking here I suspect there's probably something much simpler we could do. Thanks, Tony
From: stennie [...] cpan.org
Hrm .. order_by option for has_many relationships may not have been a documented feature, but it seems a sensible parallel to the option in search(), and has also been inadvertently used by other CPAN authors (eg. RT#17317). This definitely worked in previous releases, and the behaviour change looks to have been introduced in Class::DBI 3.0.11 which now converts the order_by hash into an array: http://search.cpan.org/diff?from=Class-DBI-v3.0.10&to=Class-DBI-v3.0.11#lib/Class/DBI/ Relationship/HasMany.pm Also note that the order_by options are only munged if this is the first parameter to a has_many search .. so you can still get away with something like: my @actors = $film->actors(name => 'Bob', {order_by => 'name'}) A simple patch to support order_by follows. Cheers, Stephen --- Class-DBI-v3.0.14/lib/Class/DBI/Relationship/HasMany.pm.dist 2005-10-24 00:06:37.000000000 +1000 +++ Class-DBI-v3.0.14/lib/Class/DBI/Relationship/HasMany.pm 2006-01-28 03:38:02.000000000 +1100 @@ -131,7 +131,7 @@ my ($class, $accessor) = ($rel->class, $rel->accessor); return sub { my ($self, @search_args) = @_; - @search_args = %{ $search_args[0] } if ref $search_args[0] eq "HASH"; + @search_args = %{ $search_args[0] } if (ref $search_args[0] eq "HASH" && !grep(/ ^order_by$/, keys %{$search_args[0]})); my $meta = $class->meta_info($rel->name => $accessor); my ($f_class, $f_key, $args) = ($meta->foreign_class, $meta->args->{foreign_key}, $meta->args);
Subject: Re: [rt.cpan.org #16493] Request for order_by in has_many
Date: Fri, 27 Jan 2006 17:14:57 +0000
To: Guest via RT <bug-Class-DBI [...] rt.cpan.org>
From: Tony Bowden <tony [...] kasei.com>
On Fri, Jan 27, 2006 at 11:55:19AM -0500, Guest via RT wrote: Show quoted text
> A simple patch to support order_by follows.
I'm open to something like this, but I don't really like the grepping on an explicit regex like this. A failing test case would be useful as well, if there isn't one already. Thanks, Tony
Subject: Re: [rt.cpan.org #16493] Request for order_by in has_many
Date: Tue, 31 Jan 2006 20:07:11 +0000
To: bug-Class-DBI [...] rt.cpan.org
From: Dan Rowles <d.rowles [...] outcometechnologies.com>
I think I did include a failing test case in the patch. Thanks, Dan On 27 Jan 2006, at 17:15, Tony Bowden via RT wrote: Show quoted text
> On Fri, Jan 27, 2006 at 11:55:19AM -0500, Guest via RT wrote:
>> A simple patch to support order_by follows.
> > I'm open to something like this, but I don't really like the > grepping on > an explicit regex like this. > > A failing test case would be useful as well, if there isn't one > already. > > Thanks, > > Tony >