变量填充对象突然为空,发生了什么



我在symfony 2.0中构建这个表单,由于某种原因,当我从数据库中检索对象并将其放入对象中时,当我想保存它时,它就不见了,所以我得到了以下错误:

Catchable Fatal Error: Argument 1 passed to MelvinLoosCMSCoreBundleEntityPage::setParent()
must be an instance of MelvinLoosCMSCoreBundleEntityPage, null given, 
called in vendorsymfonysrcSymfonyComponentFormUtilPropertyPath.php on line 347
and defined in srcMelvinLoosCMSCoreBundleEntityPage.php line 233

我的代码:

public function popupChildAction($parentid)
{
    $entity  = new Page();
    $entity->setWebsite($this->getWebsite());
    $parent = $this->getDoctrine()
    ->getRepository('MelvinLoosCMSCoreBundle:Page')
    ->findOneById($parentid);
    if (!$parent)
    {
        throw $this->createNotFoundException('No parent found with given id: "' . $parentid . '"');
    }
    $entity->setParent($parent);
    $entity->setCreatedBy($this->getUser());
    //$entity->setPageType();
    $form   = $this->createForm(new PageChildType(), $entity);
    $request = $this->getRequest();
    $form->bindRequest($request);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($entity);
        $em->flush();
        return $this->redirect($this->generateUrl('page_show', array('id' => $entity->getId())));
    }
    return $this->render('MelvinLoosCMSCoreBundle:Page:new_popup.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
            'parent' => $parent
    ));
}

正如你所看到的,我放入了一个if语句来检查$parent是否已填充,我还尝试了一个var_dump来进行双重检查,它充满了一个对象。但由于某种原因,当我调用实体对象的setParent()函数时,它会用null填充它。

Melvin您有权检查表单是否已提交,并从表单中获取实体。
你能这样试试吗?

    $form   = $this->createForm(new PageChildType(), $entity);
    $request = $this->getRequest();
    if ($request->getMethod() == 'POST') {
        $form->bindRequest($request);
        if ($form->isValid()) {
            $entity = $form->getData();
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();
            return $this->redirect($this->generateUrl('page_show', array('id' => $entity->getId())));
        }
    }

完全忘记了我还有这个问题。。。不管怎样,我解决了我的问题,仍然不知道为什么我会出错,但我用一个相当简单的解决方案解决了它。

我没有为子对象设置父对象,而是相反,为父对象设置子对象。我的代码如下(其中还有一些新代码无关紧要,因为我前段时间修复了它)。

public function popupChildAction($parentid)
{
    $parent = $this->getDoctrine()
    ->getRepository('MelvinLoosCMSCoreBundle:Page')
    ->findOneById($parentid);
    if (!$parent)
    {
        throw $this->createNotFoundException('No parent found with given id: "' . $parentid . '"');
    }
    $entity  = new Page();
    $entity->setWebsite($this->getWebsite());
    $entity->setCreatedBy($this->getUser());
    $form   = $this->createForm(new PageChildType(), $entity);
    $request = $this->getRequest();
    $form->bindRequest($request);
    if ($form->isValid()) {
        $em = $this->getDoctrine()->getEntityManager();
        $parent->setChildren($entity);
        $em->persist($entity);
        $em->flush();
        return $this->redirect($this->generateUrl('page_show', array('id' => $entity->getId())));
    }
    return $this->render('MelvinLoosCMSCoreBundle:Page:new_popup.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
            'parent' => $parent
    ));
}

就像我说的,有一些更新的代码是无关紧要的,但它的全部内容是$parent->setChildren($entity);,现在它工作。。。希望这能帮助到别人!感谢所有提供意见的人!

相关内容

最新更新