Several times I came across this scenario while working on the current project. I have to check if the record exists, if it is not needed, I have to add it if it happens, then I need to update. What is the standard way to do this with Doctrine?
I seem to be asking if a record exists using the find * method. Then, if it returns a positive result (object), I use this object to update. Otherwise (record not found) I need to create another object and save ().
For some reason, it just seems ineffective. Is there a better way, or am I just weird? :)
$user = Doctrine_Core::getTable('Model_User')->findOneByEmail(' myemail@email.com '); if (!$user) { $user = new Model_User(); $user->fromArray($values); // $values comes from form or wherever $user->save(); } else { $user->fromArray($values); $user->save(); }
source share