Php 变量填充对象突然为空,发生了什么?
我正在symfony 2.0中构建此表单,由于某种原因,当我从db中检索对象并将其放入对象时,当我要保存它时,它将消失,因此出现以下错误:Php 变量填充对象突然为空,发生了什么?,php,forms,symfony,fatal-error,Php,Forms,Symfony,Fatal Error,我正在symfony 2.0中构建此表单,由于某种原因,当我从db中检索对象并将其放入对象时,当我要保存它时,它将消失,因此出现以下错误: Catchable Fatal Error: Argument 1 passed to MelvinLoos\CMS\CoreBundle\Entity\Page::setParent() must be an instance of MelvinLoos\CMS\CoreBundle\Entity\Page, null given, called in
Catchable Fatal Error: Argument 1 passed to MelvinLoos\CMS\CoreBundle\Entity\Page::setParent()
must be an instance of MelvinLoos\CMS\CoreBundle\Entity\Page, null given,
called in vendor\symfony\src\Symfony\Component\Form\Util\PropertyPath.php on line 347
and defined in src\MelvinLoos\CMS\CoreBundle\Entity\Page.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())));
}
}
我完全忘记了这个问题还在我身边飘荡。。。无论如何,我绕过了我的问题,仍然不知道为什么我会出错,但我用一个相当简单的解决方案解决了它 我没有为子对象设置父对象,而是相反地为父对象设置子对象。我的代码如下(其中还有一些新代码是不相关的,因为我在一段时间前修复了它) 正如我所说,有一些新的代码是不相关的,但它的全部内容是与
$parent->setChildren($entity)代码>现在它可以工作了。。。希望这对别人有帮助!
感谢所有提供意见的人 在if(!$parent)之前的var_dump($parent),它说了什么?我们需要从页面classis中查看函数setParent()。如果您查看错误,则给出错误的不是控制器上的调用($entity->setParent($parent);行),而是symfony在\symfony\Component\Form\Util\PropertyPath.php上进行的调用,因此,错误是在代码中的其他某个点产生的。我猜这和你的PageChildType有关。您可以发布相应的代码吗?您的表单类型似乎有一个parent
字段,当您绑定表单时,它会尝试将parent设置为null(因为parent未提交)。引发此错误是因为setParent方法是setParent(Page$parent)
,而不是setParent(Page$parent=null)
,因此它不接受null。我建议从表单类型中删除父字段。
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
));
}