Symfony2 Doctrine2,是否有“REPLACE INTO”功能?

use*_*305 5 php symfony doctrine-orm

我做了一些研究,显然 Doctrine 不支持,REPLACE INTO所以我想知道是否有等价物?INSERT INTO如果主键不存在,Doctrine 有没有办法只做一个?

就像是:

$em = $this->getDoctrine()->getManager();
$em->replace($entity);
$em->flush();
Run Code Online (Sandbox Code Playgroud)

还是我必须把它写成一个条件?如果实体存在,更新,否则插入?

Waq*_*der 0

如果主键不存在,您可以执行 FindOneBy

$entity = $entity->getRepository('Entiy\YOurEntiry')
->findOneBy(array('field1'=>$value,...));
Run Code Online (Sandbox Code Playgroud)

然后

$entity->setField($field);
$entity->flush($entity);
Run Code Online (Sandbox Code Playgroud)