how to check if entity already exists in database before inserting in doctrine?

Posted by fayer on Stack Overflow See other posts from Stack Overflow or by fayer
Published on 2010-05-08T15:47:38Z Indexed on 2010/05/08 15:58 UTC
Read the original article Hit count: 144

Filed under:

whenever i insert a entity that already exists in database (i have a unique constraint on email) i get an error message on the screen.

so i want to check if it already exists, if not i insert it.

at the moment it looks like this:

$q = Doctrine_Query::create()
    ->from('User u')
    ->where('u.email = ?', $email);

$object = $q->fetchOne();

if( ! is_object($object)) {
          $user = new User();
          $user-email = $email;
          $user->save();
    }

i wonder, if there an easier way to do this?

thanks

© Stack Overflow or respective owner

Related posts about doctrine