4

I made a web application with Symfony2, in which a User has an array correlation ManytoMany with the entity Mission. The User can upload the entity $product through a form, and one of the data passed by the form is the mission associated to the user.

When I try to upload the data, appears the error:

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

It's clear that Doctrine don't know how to save the value of the mission.

How can I manage it?

I didn't know neither how to declare the mission object in my product entity. Now is simply like this:

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

UPDATE ---

My controller now is:

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

UPDATE ---

Now works, but I have a problem. When appears the form to upload the $product object, appears also the ->add('mission', 'entity'... In this field I can see all the mission stored, and not only the ones associated with the user. How should I change my controller? I tried to change my controller like this:

       $product = new Product();
       $product->setMission($this->getUser()->getMission());
Gianni Alessandro
  • 860
  • 6
  • 11
  • 28
  • insert `public function __toString() { return $this->mission; }` in your Entity class – Lkopo Jan 26 '14 at 15:11
  • Sorry, I'm really new of Symfony and php. Do I have to add it to my product.php entity? – Gianni Alessandro Jan 26 '14 at 15:13
  • Yes, in your Product.php entity :) – Lkopo Jan 26 '14 at 15:14
  • But the problem persists :( In my controller I set the mission in the product entity with $product->setMission($this->getUser()->getMission()); Should I change this? I tried in some ways but I didn't manage! – Gianni Alessandro Jan 26 '14 at 15:41
  • maybe this? http://stackoverflow.com/questions/10744574/symfony2-how-to-cast-data-type-in-a-twig-file If it helps, then you can delete __toString() method – Lkopo Jan 26 '14 at 15:43
  • 1
    But you have to use foreach in your controller where you are setting mission. – Lkopo Jan 26 '14 at 15:49
  • The link doesn't help, because my problem is not in the template but in the controller. My doubt is if I should save my $mission object in some kind of arraycorrelation with $product. – Gianni Alessandro Jan 26 '14 at 17:37
  • I wrote. You have to use foreach in your controller – Lkopo Jan 26 '14 at 21:30
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46152/discussion-between-gianni-alessandro-and-mr-smith) – Gianni Alessandro Jan 27 '14 at 12:55

1 Answers1

5

For manage a ManyToMany relation between User and Mission

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

in Mission.php:

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

then for your form:

http://symfony.com/doc/current/reference/forms/types/collection.html for manage collection of Mission in your User form.

take a look at "type" and "allow_add"

Vincent Barrault
  • 2,906
  • 1
  • 19
  • 17
  • Now works, but I have a problem. When appears the form to upload the $product object, appears also the ->add('mission', 'entity'... In this field I can see all the mission stored, and not only the ones associated with the user. How should I change my controller? I tried with ->add('mission','collection' .. but appears error. – Gianni Alessandro Jan 28 '14 at 17:06
  • you want to create a new mission, or choose one already created ? – Vincent Barrault Jan 29 '14 at 10:13
  • I have a similar problem [here](http://stackoverflow.com/questions/28175708/query-a-manytomany-relation-and-display-the-good-result-in-symfony-with-doctrine), Did it match for your problem? Thank you to help if you can. –  Jan 28 '15 at 16:00