类 Doctrine\ORM\PersistentCollection 的对象无法转换为字符串

Gia*_*dro 4 php doctrine arraycollection symfony

我使用 Symfony2 制作了一个 Web 应用程序,其中用户具有数组关联 ManytoMany 与实体 Mission。用户可以通过表单上传实体$product,表单传递的数据之一就是与用户关联的任务。

当我尝试上传数据时,出现错误:

ContextErrorException: Catchable Fatal Error: Object of class   
Doctrine\ORM\PersistentCollection could not be converted to string in     
C:\BitNami\wampstack-5.4.23- 
0\frameworks\symfony\vendor\doctrine\dbal\lib\Doctrine\DBAL\Statement.php line 103
Run Code Online (Sandbox Code Playgroud)

很明显,Doctrine 不知道如何保存任务的价值。

我该如何管理?

我也不知道如何在我的产品实体中声明任务对象。现在就是这样:

/**
 * @var string
 *
 * @ORM\Column(name="mission", type="string", length=255)
 */
protected $mission;
Run Code Online (Sandbox Code Playgroud)

更新 - -

我的控制器现在是:

       $form = $this->createFormBuilder($product)
           ->add('name', 'text')
           ->add('mission', 'entity', array('required' => true, 'multiple' => false, 'class' => 'AcmeManagementBundle:Mission', 'query_builder' => function($repository) { return $repository->createQueryBuilder('c')->orderBy('c.id', 'ASC'); },))              
      //...
           ->add('save', 'submit')
           ->getForm(); 
Run Code Online (Sandbox Code Playgroud)

更新 - -

现在工作,但我有一个问题。当出现上传 $product 对象的表单时,也会出现 ->add('mission', 'entity'... 在这个字段中,我可以看到所有存储的任务,而不仅仅是与用户关联的任务。如何我应该改变我的控制器吗?我试着像这样改变我的控制器:

       $product = new Product();
       $product->setMission($this->getUser()->getMission());
Run Code Online (Sandbox Code Playgroud)

VBe*_*Bee 5

用于管理用户和任务之间的多对多关系

在 User.php 中:

/**
 * @var \Doctrine\Common\Collections\ArrayCollection
 * 
 * @ORM\ManyToMany(targetEntity="Your\SuperBundle\Entity\Mission", inversedBy="users", orphanRemoval=true)
 * @ORM\JoinTable(name="user_mission")
 */
private $missions;
Run Code Online (Sandbox Code Playgroud)

在 Mission.php 中:

/**
 * @var \Doctrine\Common\Collections\ArrayCollection
 *
 * @ORM\ManyToMany(targetEntity="Your\SuperBundle\Entity\User", mappedBy="missions", cascade={"all"}, orphanRemoval=true)
 */
private $users;
Run Code Online (Sandbox Code Playgroud)

那么对于你的表格:

http://symfony.com/doc/current/reference/forms/types/collection.html用于管理用户表单中的任务集合。

看看“类型”和“allow_add”