title: Array Collection, symfony: add a relation
body: <p>I'm new of Symfony and php, and I'm trying to understand, without outcome, the array collection.</p>
  
  <p>Now I have two entity, Mission and User, in relation ManytoMany. I have a form to create new Missions and a form to create new User. </p>
  
  <p>Now I have to create a "modifyMissionAction" that allows me to set the Users for that missions, but I didn't understand how to do it. 
  I read the documentation <a href="http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html" rel="nofollow">here</a> but it doesn't help. How could I do?</p>
  
  <p>Thank you</p>
  
  <p>This is my User Entity is:</p>
  
  <pre><code> abstract class User extends BaseUser
   {
  
        /**
        * @var \Doctrine\Common\Collections\ArrayCollection
        * 
        * @ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\Mission", inversedBy="users", orphanRemoval=true)
        * @ORM\JoinTable(name="user_mission")
        */
       private $missions;    
       /**
        * Add missions
        *
        * @param \Acme\ManagementBundle\Entity\Mission $missions
        * @return User
        */
       public function addMission(\Acme\ManagementBundle\Entity\Mission $missions)
       {
           $this-&gt;missions[] = $missions;
  
           return $this;
       }
  //...
  </code></pre>
  
  <p>And my Mission Entity:</p>
  
  <pre><code>&lt;?php
  
  namespace Acme\ManagementBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Doctrine\Common\Collections\ArrayCollection;
  use Doctrine\Common\Collections\Collection;
  
  /**
   * @ORM\Entity
   */
  class Mission {
      /** 
       * @ORM\Id
       * @ORM\GeneratedValue
       * @ORM\Column(type="integer")
       * @var integer
       */
      protected $id;
          /** 
       * @ORM\Column(type="string", length=60)
       * @var String
       */
      protected $name;
      /** 
       * @ORM\Column(type="string", length=600)
       * @var String
       */
      protected $description;
      /**
       * @var \Doctrine\Common\Collections\ArrayCollection
       *
       * @ORM\ManyToMany(targetEntity="Acme\ManagementBundle\Entity\User", mappedBy="missions", cascade={"all"}, orphanRemoval=true)
       */
      private $users;
  
      public function __construct(){
          $this -&gt; users = new ArrayCollection();
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return Mission
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set description
       *
       * @param string $description
       * @return Mission
       */
      public function setDescription($description)
      {
          $this-&gt;description = $description;
  
          return $this;
      }
  
      /**
       * Get description
       *
       * @return string 
       */
      public function getDescription()
      {
          return $this-&gt;description;
      }
  
      /**
       * Add users
       *
       * @param \Acme\ManagementBundle\Entity\User $users
       * @return Mission
       */
      public function addUser(\Acme\ManagementBundle\Entity\User $users)
      {
          $this-&gt;users[] = $users;
  
          return $this;
      }
  
      /**
       * Remove users
       *
       * @param \Acme\ManagementBundle\Entity\User $users
       */
      public function removeUser(\Acme\ManagementBundle\Entity\User $users)
      {
          $this-&gt;users-&gt;removeElement($users);
      }
  
      /**
       * Get users
       *
       * @return \Doctrine\Common\Collections\Collection 
       */
      public function getUsers()
      {
          return $this-&gt;users;
      }
      public function __toString()
      {
          return $this-&gt;name;
      }
  }
  </code></pre>