On Thu, 1 Feb 2007, John Siracusa wrote:

> I question the wisdom of not supplying a sort_by paramater, however.

You're right, in the general case, sort_by is essential, but often
times, the application knows that there's either exactly one matching
record in the database or none.

It's something that I'm running into quite frequently:
I want to find out whether a matching record exists or not, and
load it if so. I guess it comes from using Class::DBI previously,
which allowed calls like

    my $cd = Music::CD->find_or_create({ artist => 'U2', title => 'Boy' });

Just speculating: Could this be added to the new find() method?

And thanks for suggesting the get_objects() method, that was helpful.

-- Mike

Mike Schilli
[EMAIL PROTECTED]

> On 2/1/07 1:14 PM, Mike Schilli wrote
> > On Tue, 30 Jan 2007, [ISO-8859-1] Ask Bj?rn Hansen wrote:
> >> (although I sometimes get tripped up by forgetting by how ->new->load
> >> needs unique keys).
> >
> > Me too. Does new->load to be restricted to unique keys?
>
> Yes, because each object is linked to a single, uniquely identified row.
>
> > Couldn't there be a way to provide a query and call the manager under the 
> > hood
> > to find, say, the first matching record?
>
> Sure, but that's something very different than load() since the "first
> matching record" depends on the sort order (if specified) or vagaries of the
> database (if not).  As you've shown, it's not a hard method to write.
> You've made it a bit more difficult than you have to, however :)
>
> The MyWhatever::Manager class methods are really just thin wrappers around
> the generic Rose::DB::Object::Manager methods, with the object class fixed.
> IOW, this:
>
>     $objects = MyWhatever::Manager->get_whatevers(...);
>
> is equivalent to this:
>
>     $objects =
>       Rose::DB::Object::Manager->get_objects(object_class => 'MyWhatever',
>                                              ...);
>
> So you don't need to muck around with the convention manager or anything
> like that.  Here's a cleaned up version of your method (untested, but it
> should be right...famous last words :)
>
>     sub db_find_or_create
>     {
>       my($self, $class, $query) = @_;
>
>       my $object_class = __PACKAGE__ . '::' . $class;
>
>       my $objects =
>         Rose::DB::Object::Manager->get_objects(
>           object_class => $object_class,
>           query        => $query);
>
>       if(@$objects)
>       {
>         return wantarray ? ($objects->[0], 0) : undef;
>       }
>
>       my $object = $object_class->new(@$query);
>       return wantarray ? ($object, 1) : $object;
>     }
>

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Rose-db-object mailing list
Rose-db-object@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rose-db-object

Reply via email to