0
我在Symfony 2.6.1項目中使用了Doctrine 2.4.6。問題在於preUpdate回調中對實體所做的更改未保存在數據庫中。代碼如下:學說preUpdate實際上並沒有改變實體
class MyListener {
public function preUpdate(PreUpdateEventArgs $args) {
$entity = $args->getEntity();
$args->setNewValue('name', 'test');
// echo $args->getNewValue('name'); --> prints 'test'
}
}
class DefaultController extends Controller {
/**
* @Route("/commit", name="commit")
*/
public function commitAction(Request $request) {
$content = $request->getContent();
$serializer = $this->get('jms_serializer');
/* @var $serializer \JMS\Serializer\Serializer */
$em = $this->getDoctrine()->getManager();
/* @var $em \Doctrine\Orm\EntityManagerInterface */
$persons = $serializer->deserialize($content, 'ArrayCollection<AppBundle\Entity\Person>', 'json');
/* @var $persons \AppBundle\Entity\Person[] */
foreach($persons as $person) {
$em->merge($person);
}
$em->flush();
return new JsonResponse($serializer->serialize($persons, 'json'));
// Person name is NOT 'test' here.
}
}
仔細閱讀印刷精美/en/latest/reference/events.html#preupdate這是混淆的常見來源。 preUpdate事件有很多限制。 – Cerad