1

フォームを編集しようとしていますが、既存の値を持つフォームを正しく取得しますが、フィールドに新しいデータを入力してフォームを送信しようとすると、非オブジェクトのメンバー関数 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  

何か案は?前もって感謝します

4

2 に答える 2

0

あなたのオブジェクト $patent_group はおそらくあなたのDBで見つかりませんでした。

于 2012-06-04T13:45:47.143 に答える
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
    ));
}
于 2012-06-04T14:39:56.310 に答える