我可以在 Symfony 中保存单个实体,而不刷新所有更改吗?例如。
$em = $this->getDoctrine();
$entity1 = $em->find('\SomeEntity', 1);
$entity2 = $em->find('\SomeEntity', 2);
$entity1->setFoo(1);
$entity1->persist();
$entity2->setFoo(2);
$this->saveRightNow($entity2); // entity2 is written to the DB at this point
$em->flush(); // entity1 is written to the DB at this point
查看源代码,似乎我可以使用Doctrine\ORM\UnitOfWork::commit
:
function saveRightNow($entity) {
$em = $this->getDoctrine();
$uow = $em->getUnitOfWork();
$uow->commit($entity);
}
但我找不到任何关于使用commit
这种方式的文档(也没有太多关于使用它的信息,即使它没有被标记为内部函数)。这是一个好主意吗?它有什么危险吗?