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? Couldn't there
be a way to provide a query and call the manager under the hood to
find, say, the first matching record?

I'm using something like

    ###########################################
    sub db_find_or_create {
    ###########################################
        my($self, $class, $query) = @_;

        my $cmgr = Rose::DB::Object::ConventionManager->new();

        $method = "get_" . $cmgr->class_to_table_plural($class);

        my $files = (__PACKAGE__ . "::${class}::Manager")->$method(
            query => $query
        );

        if($files->[0]) {
            if(wantarray) {
                return($files->[0], 0);
            } else {
                return $files->[0];
            }
        }

        my $rec = (__PACKAGE__ . "::$class")->new(@$query);
        if(wantarray) {
            return($rec, 1);
        } else {
            return $rec;
        }
    }

which I'm calling like

    my($obj, $was_created) = $self->db_find_or_create(
           "Someclass", [field => "value"]);

Is there a smarter way?

-- Mike

Mike Schilli
[EMAIL PROTECTED]


>
> sub fetch {
>    my $self = shift;
>    my $obj = $self->object_class->new(@_);
>    $obj->load(speculative => 1) ? $obj : undef;
> }
>
> sub fetch_or_create {
>    my $self = shift;
>    my $obj = $self->object_class->new(@_);
>    $obj->load(speculative => 1);
>    $obj;
> }
>
> sub create {
>    shift->object_class->new(@_);
> }
>
>
>
> >     # Same as above (hashref as first arg is taken as query)
> >     @low_prices = $p->find_prices({ price => { lt => 10 } });
>
> This should make it easily call-able from Template Toolkit, too (for
> better or worse).
>
>
>   - ask
>
> --
> http://develooper.com/ - http://askask.com/
>
>
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share your
> opinions on IT & business topics through brief surveys - and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> Rose-db-object mailing list
> Rose-db-object@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/rose-db-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