【问题标题】:how to remove unable to find entity error?如何删除无法找到实体错误?
【发布时间】:2013-10-29 12:12:03
【问题描述】:

我做了一个控制器的更新功能,但是当我在字段中插入值并提交异常返回时,我做了一个控制器的更新功能,但是当我在字段中插入值并提交异常返回时,

 "Unable to find Sections entity" 

这是我的控制器代码:

 public function updateAction(Request $request, $id)
 {
    if(isset($_SESSION['id'])) {
           $proposalid=$_SESSION['id'];
        }
        $user = $this->get('security.context')->getToken()->getUser();
        $userId = $user->getId();


    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('ProposalsProposalsBundle:Sections')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Sections entity.');
    }

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createForm(new SectionsType(), $entity);
    $editForm->bind($request);
    $sectioncounter = $request->request->get('sectioncounter');
    $date= new \DateTime();
     $query = $em->createQuery("Delete from ProposalsProposalsBundle:Sections s  where s.proposalID='".$proposalid."'");    
         $ids = $query->getResult();
    if($request->isXmlHttpRequest()) {
        $response = new Response();
         for($i=0; $i<$sectioncounter; $i++){
         $sectionname = $_POST['sectionName'.$i];
         $description=$_POST['description'.$i];                                                                 
         $entity->setSectionName($sectionname);
         $entity->setDescription($description);
         $entity->setProposalID($proposalid);
         $entity->setCreatedBy($userId);
         $entity->setUpdatedBy($userId);
         $entity->setCreatedDatetime($date);
         $entity->setUpdatedDatetime($date);
         $em->persist($entity);                        
         $em->flush();
         $em->clear();
     } 
    return $response;
    }
    return $this->render('ProposalsProposalsBundle:Sections:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

如何去除这个异常?

【问题讨论】:

  • 您应该检查您的实体和存储库文件。我认为问题一定存在
  • 你确定你有一个具有该 ID 的部分吗??
  • 是的,我确定部分有那个 id

标签: symfony doctrine-orm entitymanager


【解决方案1】:

Symfony2 有自己的会话处理程序。设置会话变量:

$session = $this->getRequest()->getSession();
$session->set('member', $member);

获取会话变量:

$session = $this->getRequest()->getSession();
$member = $session->get('member');

【讨论】:

    猜你喜欢
    • 2013-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-02
    • 2020-01-26
    • 1970-01-01
    • 2021-12-20
    • 1970-01-01
    相关资源
    最近更新 更多