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

时间:2014-01-26 15:05:37

标签: php symfony doctrine arraycollection

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

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

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

很明显,学说不知道如何挽救任务的价值。

我该如何管理?

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

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

更新---

我的控制器现在是:

       $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(); 

更新---

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

       $product = new Product();
       $product->setMission($this->getUser()->getMission());

1 个答案:

答案 0 :(得分:4)

用于管理用户和使命之间的ManyToMany关系

在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;

在Mission.php中:

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

然后为您的表单:

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

看一下“type”和“allow_add”