Symfony2 Doctrine2, is there a function "REPLACE INTO"?

I did some research that Doctrine does not seem to support REPLACE INTO, so I was wondering if there is an equivalent? Is there a way for Doctrine to simply do INSERT INTOif the primary key does not exist?

Sort of:

$em = $this->getDoctrine()->getManager();
$em->replace($entity);
$em->flush();

Or do I need to write this condition? If the entity exists, update, otherwise insert?

+4
source share
1 answer

If the primary key does not exist, you can do FindOneBy

$entity = $entity->getRepository('Entiy\YOurEntiry')
->findOneBy(array('field1'=>$value,...));

and than

$entity->setField($field);
$entity->flush($entity);
0
source

All Articles