[Dbix-class] Is this the canonical way to do this?

Mark Blythe list at markblythe.com
Thu Jun 8 05:05:08 CEST 2006


John, have you tried just passing the key value to find() as a scalar
rather than passing a hash ref?  Here's a  piece of sample code which
works just fine for me:

($c->stash->{thing} = $c->model('Main::Thing')->find($thing_id)) ||
     die "Thing $thing_id not found";

This code does indeed die if no matching record is found and moves on
just fine when it is found.

Maybe it's broken in a specific version?  I'm running DBIx::Class .06002

On 6/6/06, John Napiorkowski <jjn1056 at yahoo.com> wrote:
> Hi,
>
> Quick question.  I have a lot of places where I check
> to see if a particular unique key exists in the
> database.  It's not clear to me what the ->find method
> returns when it doesn't find the specified unique key.
>  I assumed it would return something undefined, but
> the following code doesn't do what I think it would:
>
> {Catalyst example}
>
> if( $c->model("db:video")->find({id=?id}) )
> {
>   ## Do something if the $id exists in the database
> }
> else
> {
>   ## Do something else if it doesn't
> }
>
> For some reason this always (in my code) seem to
> evaluate to boolean true regardless if the row exists
> in the database or not.
>
> I tried:
>
> if( $c->model("db:video")->find({id=?id})->count )
>
> But that just gives me "not defined" errors.  So I
> looked in the documentaion for the row object and
> found a method called ->in_storage but that also just
> gives me a 'not defined' error.
>
> I also tried:
>
> if( defined($c->model("db:video")->find({id=?id})) )
>
> but that doesn't do it.
>
> I can still do:
>
> if( $c->model("db:video")->search({id=?id})->count )
>
> and this works but seems rather cludgy.  Have any of
> you found a cleaner and more correct way to do this
> type of check?
>
> --john
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com
>
> _______________________________________________
> List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> Wiki: http://dbix-class.shadowcatsystems.co.uk/
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/
>



More information about the Dbix-class mailing list