2012-04-21 74 views
2

我有一种表单,您可以在其中编辑对象“one”的属性。该对象与另一个对象“many”具有一对多关系。我希望用户能够从表单中为“one”选择一个“多”对象。我无法弄清楚如何做到这一点!如何从Symfony中的对象列表中选择2编辑对象表格

现在:

\实体\ One.php

class One 
{ 
... 
    /* 
    * @ORM\ManyToOne(targetEntity="many", inversedBy="one") 
    * @ORM\JoinColumn(name="manyId", referencedColumnName="id") 
    */ 
    protected $manyId; 
... 
} 

\控制器\ OneController.php

class OneController extends Controller 
{ 
    ... 
    public function editAction($oneId, Request $request) 
    { 
     if ($oneId) { 
      $one = $this->getDoctrine() 
       ->getRepository('One') 
       ->find($oneId); 
     } else { 
      $one = new One(); 
     } 

     $em = $this->getDoctrine()->getEntityManager(); 
     $manyEntity = 'Bundle\Entity\Many'; 
     $manyList = new EntityChoiceList($em, $manyEntity); 

     $form = $this->createFormBuilder($one) 
      ->add('many', 'choice', array('choice_list' => $manyList)) 
      ->getForm(); 

     if ($request->getMethod() == 'POST') { 
      $form->bindRequest($request); 

      if ($form->isValid()) { 
       $entityManager = $this->getDoctrine()->getEntityManager(); 
       $entityManager->persist($one); 
      } 
     } 
    } 
... 
} 

这将导致错误消息 “类型的参数预期” 标“,”Proxies \ BundleEntityManyProxy“给出”。

感谢您的帮助!

回答