我有两个控制器,一个记录修改后的实体并将其放入会话中:
public function update_accountAction(Request $request)
{
Try
{
$code = $request->request->get('code');
$name = $request->request->get('name');
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('NRtworksChartOfAccountsBundle:Accounttree');
$to_change = new Accounttree();
$to_change = $repo->findOneByCode($code);
$to_change->setName($name);
$to_change->setCode($code);
$session = $this->getRequest()->getSession();
$entity_to_update = $session->get('entity_to_update');
$counter = $session->get('number_of_changes');
$entity_to_update[] = $to_change;
$counter = $counter +1;
$session->set('number_of_changes',$counter);
$session->set('entity_to_update',serialize($entity_to_update));
$response = array("code" => 100, "success" => true, "modified" => $entity_to_update);
return new Response(json_encode($response));
}
Catch(Exception $e)
{
$response = array("code" => 100, "success" => false, "error" => $e);
return new Response($response);
}
}
和另一个循环结果,如果它实际上是所需的对象之一。终于我冲洗了。
public function save_changesAction()
{
Try
{
$em = $this->getDoctrine()->getManager();
$session = $this->getRequest()->getSession();
$entity_to_update = unserialize($session->get('entity_to_update'));
foreach($entity_to_update as $account)
{
if($account->getId())
{
$em->persist($account);
echo $account->getId();
}
else
{
echo "error";
}
}
$em->flush();
$response = array("code" => 100, "success" => true, "modified" => $account->getId());
return new Response(json_encode($response));
}
Catch(Exception $e)
{
$response = array("code" => 100, "success" => false, "error" => $e);
return new Response($response);
}
}
因此,结果是:contexterRorexception:注意:未定义的索引:0000007A60B04100000000000077AE6AFD8 IN/home/eagle1/www/symfony24/www/symfony24/vendor/doctrine/doctrine/doctrine/doctrine/lib/lib/doctrine/doctrine/doctrine/orm/orm/orm/unitofwork.php line 2852
不明白为什么,因为看来我恢复了一个功能齐全的对象(我可以执行其功能,访问其属性,并且似乎持续存在正常工作...有人知道答案吗?
从会话中拉出对象时,它不会由学说管理。您必须将其合并回通知实体经理。
if($account->getId())
{
$account = $em->merge($account);
$em->persist($account);
echo $account->getId();
}
学说有会话中实体的手册部分