Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从会话中持久化和刷新对象_Php_Symfony_Serialization_Doctrine Orm_Flush - Fatal编程技术网

Php 从会话中持久化和刷新对象

Php 从会话中持久化和刷新对象,php,symfony,serialization,doctrine-orm,flush,Php,Symfony,Serialization,Doctrine Orm,Flush,我有两个控制器,一个记录修改后的实体并将它们放入会话: public function update_accountAction(Request $request) { Try { $code = $request->request->get('code'); $name = $request->request->get('name'); $em = $this->getDoctrine()

我有两个控制器,一个记录修改后的实体并将它们放入会话:

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:注意:未定义索引:00000000 7A60B041000000007AE6AFD8 in/home/eagle1/www/Symfony24/vendor/doctrine/orm/lib/doctrine/orm/UnitOfWork.php第2852行

我不明白为什么,因为我似乎得到了一个完全功能化的对象(我可以执行它的函数,访问它的属性,而且持久化似乎也在工作


有人知道答案吗?

当您从会话中提取对象时,它不受条令管理。您必须将其合并回来,以通知实体管理器

if($account->getId())
{                     
    $account = $em->merge($account);
    $em->persist($account); 
    echo $account->getId();
 }

条令手册第2852行中有什么内容?有什么具体原因吗?您不只是在会话中保存实体id以及两个字符串(名称和代码),而是尝试存储完整的实体(以及所有条令代理内容)?您可以在保存操作中轻松地从存储库中获取实体。对于这种情况,您是对的,我可以按照您的说法执行。但是,当我要更改父级时,我必须拥有该实体。除非我可以自己设置父级(不使用文档中建议的方法addchild),否则手册页面已移动: