我正在尝试编辑表单我正确地获取具有现有值的表单但是当我尝试将新数据放入字段并提交表单时,我得到一个错误的调用成员函数setName()对非对象
我的控制器方法是
public function editAction(Request $request){
$request = Request::createFromGlobals();
$group_id = $request->query->get('id');
$em = $this->getDoctrine()->getEntityManager();
$patent_group = $em->getRepository('MunichInnovationGroupBundle:PmPatentgroups')->find($group_id);
//echo '<pre>';
//print_r($patent_group);
//echo '</pre>';
$form = $this->createForm(new PatentgroupType(), $patent_group);
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
$name = $form["name"]->getData();
$description = $form["description"]->getData();
$patent_group->setName($name);
$patent_group->setDescription($description);
$em->flush();
return $this->redirect($this->generateUrl('MunichInnovationGroupBundle_portfolio'));
}
return $this->render('MunichInnovationGroupBundle:Patentgroups:edit.html.twig', array(
'form' => $form->createView(),
'group_id' => $group_id
));
}
这是我在$ patent_group中获得的
MunichInnovationGroup\Bundle\Entity\PmPatentgroups Object
(
[id:MunichInnovationGroup\Bundle\Entity\PmPatentgroups:private] => c3e0eb98-ada5-11e1-96a5-9787dec335c2
[name:MunichInnovationGroup\Bundle\Entity\PmPatentgroups:private] => Zoha Ali khan
[description:MunichInnovationGroup\Bundle\Entity\PmPatentgroups:private] => No Description Provided for this Group
[portfolio_id:MunichInnovationGroup\Bundle\Entity\PmPatentgroups:private] => Proxies\MunichInnovationGroupBundleEntityPmPortfoliosProxy Object
(
[_entityPersister:Proxies\MunichInnovationGroupBundleEntityPmPortfoliosProxy:private] => Doctrine\ORM\Persisters\BasicEntityPersister Object
(
[_class:protected] => Doctrine\ORM\Mapping\ClassMetadata Object
(
[reflFields] => Array
(
[id] => ReflectionProperty Object
(
[name] => id
[class] => MunichInnovationGroup\Bundle\Entity\PmPortfolios
)
[portfolioName] => ReflectionProperty Object
(
[name] => portfolioName
[class] => MunichInnovationGroup\Bundle\Entity\PmPortfolios
)
我在这里做错了,因为我收到了这个错误
Call to a member function setName() on a non-object
任何想法? 提前致谢
答案 0 :(得分:0)
您的对象$ patent_group很可能在您的数据库中找不到。
答案 1 :(得分:0)
我的修复:请评论它是否是一个好的解决方案
public function editAction(Request $request){
$request = Request::createFromGlobals();
$group_id = $request->query->get('id');
$em = $this->getDoctrine()->getEntityManager();
$patentgroup = $em->getRepository('MunichInnovationGroupBundle:PmPatentgroups')->findOneBy(array('id'=>$group_id));
$form = $this->createForm(new PatentgroupType(), $patentgroup);
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
$group_id = $form["id"]->getData();
$name = $form["name"]->getData();
$description = $form["description"]->getData();
$em = $this->getDoctrine()->getEntityManager();
$patentgroup = $em->getRepository('MunichInnovationGroupBundle:PmPatentgroups')->findOneBy(array('id'=>$group_id));
$patentgroup->setName($name);
$patentgroup->setDescription($description);
$em->flush();
return $this->redirect($this->generateUrl('MunichInnovationGroupBundle_portfolio'));
}
return $this->render('MunichInnovationGroupBundle:Patentgroups:edit.html.twig', array(
'form' => $form->createView(),
'group_id' => $group_id
));
}