2

我有一个表单,您可以在其中编辑对象“一个”的属性。该对象与另一个对象“many”具有一对多的关系。我希望用户能够从表单中选择将“许多”对象分配给“一个”。我不知道该怎么做!

现在:

\Entity\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”给定”。

谢谢你的帮助!

4

1 回答 1

7

解决了!我应该写->add('many', 'entity', array('class' => 'BundleMany'))的。

请参阅http://forum.symfony-project.org/viewtopic.php?f=23&t=36604

于 2012-04-22T20:16:42.893 回答