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 =
          object_class => $object_class,
          query        => $query);
        return wantarray ? ($objects->[0], 0) : undef;
      my $object = $object_class->new(@$query);
      return wantarray ? ($object, 1) : $object;

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


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
Rose-db-object mailing list

Reply via email to