好的,我通常不会寻求帮助,但它让我发疯,我必须在这里完成这个项目。如果我遗漏任何可能有帮助的信息,请告诉我。
所以我在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 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填充它.... 也许我只是忽略了一些东西,因为我工作的时间太长但是非常感谢所有的帮助。谢谢!
答案 0 :(得分:0)
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())));
}
}
答案 1 :(得分:0)
完全忘记了我的问题仍然存在...无论如何我解决了我的问题,仍然不知道为什么我得到了错误,但我用一个非常简单的解决方案解决了它。
我没有为孩子设置父级,而是反过来设置父级的子级。我的代码如下(还有一些新的代码,因为我在不久前修复了它。)
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);
现在它有效......希望这有助于某人!
感谢所有给予投入的人!