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

John Napiorkowski jjn1056 at yahoo.com
Wed Jun 7 06:12:49 CEST 2006


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 



More information about the Dbix-class mailing list