Whenever I insert an entity that already exists in the database, I get an error because there is a unique constraint on one of the fields (email).
So I want to check if it already exists; if not, I insert it.
My code 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();
}
Is there an easier way to do this?
Put the code you have into a method in your UserTable class eg
insertIfNotExists():Now you can call the method, and the object returned will be the existing record (if there is one), or the one you’ve just created.