[Dbix-class] Is this the canonical way to do this?
John Napiorkowski
jjn1056 at yahoo.com
Thu Jun 8 11:39:35 CEST 2006
Looks like I was having some sort of mental block the
other night. I got the following to work properly:
my $video_rs = $self->find({video_id=>$video_id});
return ( defined($video_rs) ? 1 : 0 );
and it did what I wanted.
I have to specify the key name in find since my table
has multiple primary keys. I've also found that since
->find works for any column marked as unique it has
really improved my thinking about my table designs,
since I am more careful to normalize properly to get
more truly unique columns. So in this way I am sure
DBIx has improved my table designs.
Thanks for the help from al the responders. Sometimes
having someone say, "It should work" restores my
confidence :) --john
--- Mark Blythe <list at markblythe.com> wrote:
> 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/
> >
>
> _______________________________________________
> 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/
>
__________________________________________________
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