title: Symfony2 collection of Entities - how to add/remove association with existing entities?
body: <h1>1. Quick overview</h1>
  
  <h3>1.1 Goal</h3>
  
  <p>What I'm trying to achieve is a create/edit user tool. Editable fields are:</p>
  
  <ul>
  <li>username (type: text)</li>
  <li>plainPassword (type: password)</li>
  <li>email (type: email)</li>
  <li>groups (type: collection)</li>
  <li>avoRoles (type: collection)</li>
  </ul>
  
  <p><em>Note: the last property is not named <strong>$roles</strong> becouse my User class is extending FOSUserBundle's User class and overwriting roles brought more problems. To avoid them I simply decided to store my collection of roles under <strong>$avoRoles</strong>.</em></p>
  
  <h3>1.2 User Interface</h3>
  
  <p><a href="http://rockoweogrodki.radiowww.eu/tmp/scr1.png">My template</a> consists of 2 sections:</p>
  
  <ol>
  <li>User form</li>
  <li>Table displaying $userRepository->findAllRolesExceptOwnedByUser($user);</li>
  </ol>
  
  <p><em>Note: findAllRolesExceptOwnedByUser() is a custom repository function, returns a subset of all roles (those not yet assigned to $user).</em></p>
  
  <h3>1.3 Desired functionality</h3>
  
  <p>1.3.1 Add role:</p>
  
  <pre>
  
      <b>WHEN</b> user clicks "+" (add) button in Roles table  
      <b>THEN</b> jquery removes that row from Roles table  
      <b>AND</b>  jquery adds new list item to User form (avoRoles list)
  
  </pre>
  
  <p>1.3.2 Remove roles:</p>
  
  <pre>
  
      <b>WHEN</b> user clicks "x" (remove) button in  User form (avoRoles list)  
      <b>THEN</b> jquery removes that list item from User form (avoRoles list)  
      <b>AND</b>  jquery adds new row to Roles table
  
  </pre>
  
  <p>1.3.3 Save changes:</p>
  
  <pre>
  
      <b>WHEN</b> user clicks "Zapisz" (save) button  
      <b>THEN</b> user form submits all fields (username, password, email, avoRoles, groups)  
      <b>AND</b>  saves avoRoles as an ArrayCollection of Role entities (ManyToMany relation)  
      <b>AND</b>  saves groups as an ArrayCollection of Role entities (ManyToMany relation)  
  
  </pre>
  
  <p><em>Note: ONLY existing Roles and Groups can be assigned to User. If for any reason they are not found the form should not validate.</em></p>
  
  <hr>
  
  <h1>2. Code</h1>
  
  <p>In this section I present/or shortly describe code behind this action. If description is not enough and you need to see the code just tell me and I'll paste it. I'm not pasteing it all in the first place to avoid spamming you with unnecessary code.</p>
  
  <h3>2.1 User class</h3>
  
  <p>My User class extends FOSUserBundle user class. </p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>namespace Avocode\UserBundle\Entity;
  
  use FOS\UserBundle\Entity\User as BaseUser;
  use Doctrine\ORM\Mapping as ORM;
  use Avocode\CommonBundle\Collections\ArrayCollection;
  use Symfony\Component\Validator\ExecutionContext;
  
  /**
   * @ORM\Entity(repositoryClass="Avocode\UserBundle\Repository\UserRepository")
   * @ORM\Table(name="avo_user")
   */
  class User extends BaseUser
  {
      const ROLE_DEFAULT = 'ROLE_USER';
      const ROLE_SUPER_ADMIN = 'ROLE_SUPER_ADMIN';
  
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\generatedValue(strategy="AUTO")
       */
      protected $id;
  
      /**
       * @ORM\ManyToMany(targetEntity="Group")
       * @ORM\JoinTable(name="avo_user_avo_group",
       *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
       * )
       */
      protected $groups;
  
      /**
       * @ORM\ManyToMany(targetEntity="Role")
       * @ORM\JoinTable(name="avo_user_avo_role",
       *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
       * )
       */
      protected $avoRoles;
  
      /**
       * @ORM\Column(type="datetime", name="created_at")
       */
      protected $createdAt;
  
      /**
       * User class constructor
       */
      public function __construct()
      {
          parent::__construct();
  
          $this-&gt;groups = new ArrayCollection();        
          $this-&gt;avoRoles = new ArrayCollection();
          $this-&gt;createdAt = new \DateTime();
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set user roles
       * 
       * @return User
       */
      public function setAvoRoles($avoRoles)
      {
          $this-&gt;getAvoRoles()-&gt;clear();
  
          foreach($avoRoles as $role) {
              $this-&gt;addAvoRole($role);
          }
  
          return $this;
      }
  
      /**
       * Add avoRole
       *
       * @param Role $avoRole
       * @return User
       */
      public function addAvoRole(Role $avoRole)
      {
          if(!$this-&gt;getAvoRoles()-&gt;contains($avoRole)) {
            $this-&gt;getAvoRoles()-&gt;add($avoRole);
          }
  
          return $this;
      }
  
      /**
       * Get avoRoles
       *
       * @return ArrayCollection
       */
      public function getAvoRoles()
      {
          return $this-&gt;avoRoles;
      }
  
      /**
       * Set user groups
       * 
       * @return User
       */
      public function setGroups($groups)
      {
          $this-&gt;getGroups()-&gt;clear();
  
          foreach($groups as $group) {
              $this-&gt;addGroup($group);
          }
  
          return $this;
      }
  
      /**
       * Get groups granted to the user.
       *
       * @return Collection
       */
      public function getGroups()
      {
          return $this-&gt;groups ?: $this-&gt;groups = new ArrayCollection();
      }
  
      /**
       * Get user creation date
       *
       * @return DateTime
       */
      public function getCreatedAt()
      {
          return $this-&gt;createdAt;
      }
  }
  </code></pre>
  
  <h3>2.2 Role class</h3>
  
  <p>My Role class extends Symfony Security Component Core Role class. </p>
  
  <pre class="lang-php prettyprint-override"><code>namespace Avocode\UserBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  use Avocode\CommonBundle\Collections\ArrayCollection;
  use Symfony\Component\Security\Core\Role\Role as BaseRole;
  
  /**
   * @ORM\Entity(repositoryClass="Avocode\UserBundle\Repository\RoleRepository")
   * @ORM\Table(name="avo_role")
   */
  class Role extends BaseRole
  {    
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\generatedValue(strategy="AUTO")
       */
      protected $id;
  
      /**
       * @ORM\Column(type="string", unique="TRUE", length=255)
       */
      protected $name;
  
      /**
       * @ORM\Column(type="string", length=255)
       */
      protected $module;
  
      /**
       * @ORM\Column(type="text")
       */
      protected $description;
  
      /**
       * Role class constructor
       */
      public function __construct()
      {
      }
  
      /**
       * Returns role name.
       * 
       * @return string
       */    
      public function __toString()
      {
          return (string) $this-&gt;getName();
      }
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return Role
       */
      public function setName($name)
      {      
          $name = strtoupper($name);
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set module
       *
       * @param string $module
       * @return Role
       */
      public function setModule($module)
      {
          $this-&gt;module = $module;
  
          return $this;
      }
  
      /**
       * Get module
       *
       * @return string 
       */
      public function getModule()
      {
          return $this-&gt;module;
      }
  
      /**
       * Set description
       *
       * @param text $description
       * @return Role
       */
      public function setDescription($description)
      {
          $this-&gt;description = $description;
  
          return $this;
      }
  
      /**
       * Get description
       *
       * @return text 
       */
      public function getDescription()
      {
          return $this-&gt;description;
      }
  }
  </code></pre>
  
  <h3>2.3 Groups class</h3>
  
  <p>Since I've got the same problem with groups as with roles, I'm skipping them here. If I get roles working I know I can do the same with groups.</p>
  
  <h3>2.4 Controller</h3>
  
  <pre class="lang-php prettyprint-override"><code>namespace Avocode\UserBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  use Symfony\Component\HttpFoundation\RedirectResponse;
  use Symfony\Component\Security\Core\SecurityContext;
  use JMS\SecurityExtraBundle\Annotation\Secure;
  use Avocode\UserBundle\Entity\User;
  use Avocode\UserBundle\Form\Type\UserType;
  
  class UserManagementController extends Controller
  {
      /**
       * User create
       * @Secure(roles="ROLE_USER_ADMIN")
       */
      public function createAction(Request $request)
      {      
          $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
  
          $user = new User();
          $form = $this-&gt;createForm(new UserType(array('password' =&gt; true)), $user);
  
          $roles = $em-&gt;getRepository('AvocodeUserBundle:User')
                      -&gt;findAllRolesExceptOwned($user);
          $groups = $em-&gt;getRepository('AvocodeUserBundle:User')
                      -&gt;findAllGroupsExceptOwned($user);
  
          if($request-&gt;getMethod() == 'POST' &amp;&amp; $request-&gt;request-&gt;has('save')) {
              $form-&gt;bindRequest($request);
  
              if($form-&gt;isValid()) {
                  /* Persist, flush and redirect */
                  $em-&gt;persist($user);
                  $em-&gt;flush();
                  $this-&gt;setFlash('avocode_user_success', 'user.flash.user_created');
                  $url = $this-&gt;container-&gt;get('router')-&gt;generate('avocode_user_show', array('id' =&gt; $user-&gt;getId()));
  
                  return new RedirectResponse($url);
              }
          }
  
          return $this-&gt;render('AvocodeUserBundle:UserManagement:create.html.twig', array(
            'form' =&gt; $form-&gt;createView(),
            'user' =&gt; $user,
            'roles' =&gt; $roles,
            'groups' =&gt; $groups,
          ));
      }
  }
  </code></pre>
  
  <h3>2.5 Custom repositories</h3>
  
  <p>It is not neccesary to post this since they work just fine - they return a subset of all Roles/Groups (those not assigned to user).</p>
  
  <h3>2.6 UserType</h3>
  
  <p>UserType:</p>
  
  <pre class="lang-php prettyprint-override"><code>namespace Avocode\UserBundle\Form\Type;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilder;
  
  class UserType extends AbstractType
  {    
      private $options; 
  
      public function __construct(array $options = null) 
      { 
          $this-&gt;options = $options; 
      }
  
      public function buildForm(FormBuilder $builder, array $options)
      {
          $builder-&gt;add('username', 'text');
  
          // password field should be rendered only for CREATE action
          // the same form type will be used for EDIT action
          // thats why its optional
  
          if($this-&gt;options['password'])
          {
            $builder-&gt;add('plainpassword', 'repeated', array(
                          'type' =&gt; 'text',
                          'options' =&gt; array(
                            'attr' =&gt; array(
                              'autocomplete' =&gt; 'off'
                            ),
                          ),
                          'first_name' =&gt; 'input',
                          'second_name' =&gt; 'confirm', 
                          'invalid_message' =&gt; 'repeated.invalid.password',
                       ));
          }
  
          $builder-&gt;add('email', 'email', array(
                          'trim' =&gt; true,
                       ))
  
          // collection_list is a custom field type
          // extending collection field type
          //
          // the only change is diffrent form name
          // (and a custom collection_list_widget)
          // 
          // in short: it's a collection field with custom form_theme
          // 
                  -&gt;add('groups', 'collection_list', array(
                          'type' =&gt; new GroupNameType(),
                          'allow_add' =&gt; true,
                          'allow_delete' =&gt; true,
                          'by_reference' =&gt; true,
                          'error_bubbling' =&gt; false,
                          'prototype' =&gt; true,
                       ))
                  -&gt;add('avoRoles', 'collection_list', array(
                          'type' =&gt; new RoleNameType(),
                          'allow_add' =&gt; true,
                          'allow_delete' =&gt; true,
                          'by_reference' =&gt; true,
                          'error_bubbling' =&gt; false,
                          'prototype' =&gt; true,
                       ));
      }
  
      public function getName()
      {
          return 'avo_user';
      }
  
      public function getDefaultOptions(array $options){
  
          $options = array(
            'data_class' =&gt; 'Avocode\UserBundle\Entity\User',
          );
  
          // adding password validation if password field was rendered
  
          if($this-&gt;options['password'])
            $options['validation_groups'][] = 'password';
  
          return $options;
      }
  }
  </code></pre>
  
  <h3>2.7 RoleNameType</h3>
  
  <p>This form is supposed to render:</p>
  
  <ul>
  <li>hidden Role ID</li>
  <li>Role name (READ ONLY)</li>
  <li>hidden module (READ ONLY)</li>
  <li>hidden description (READ ONLY)</li>
  <li>remove (x) button</li>
  </ul>
  
  <p><em>Module and description are rendered as hidden fields, becouse when Admin removes a role from a User, that role should be added by jQuery to Roles Table - and this table has Module and Description columns.</em></p>
  
  <pre class="lang-php prettyprint-override"><code>namespace Avocode\UserBundle\Form\Type;
  
  use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilder;
  
  class RoleNameType extends AbstractType
  {
      public function buildForm(FormBuilder $builder, array $options)
      {
          $builder            
              -&gt;add('', 'button', array(
                'required' =&gt; false,
              ))  // custom field type rendering the "x" button
  
              -&gt;add('id', 'hidden')
  
              -&gt;add('name', 'label', array(
                'required' =&gt; false,
              )) // custom field type rendering &amp;lt;span&amp;gt; item instead of &amp;lt;input&amp;gt; item
  
              -&gt;add('module', 'hidden', array('read_only' =&gt; true))
              -&gt;add('description', 'hidden', array('read_only' =&gt; true))
          ;        
      }
  
      public function getName()
      {
          // no_label is a custom widget that renders field_row without the label
  
          return 'no_label';
      }
  
      public function getDefaultOptions(array $options){
          return array('data_class' =&gt; 'Avocode\UserBundle\Entity\Role');
      }
  }
  </code></pre>
  
  <hr>
  
  <h1>3. Current/known Problems</h1>
  
  <h3>3.1 Case 1: configuration as quoted above</h3>
  
  <p>The above configuration returns error: </p>
  
  <pre class="lang-php prettyprint-override"><code>Property "id" is not public in class "Avocode\UserBundle\Entity\Role". Maybe you should create the method "setId()"?
  </code></pre>
  
  <p>But setter for ID should not be required.</p>
  
  <ol>
  <li>First becouse I don't want to create a NEW role. I want just to create a relation between existing Role and User entities.</li>
  <li><p>Even if I did want to create a new Role, it's ID should be auto-generated:</p>
  
  <p>/**</p>
  
  <ul>
  <li>@ORM\Id</li>
  <li>@ORM\Column(type="integer")</li>
  <li>@ORM\generatedValue(strategy="AUTO")
  */
  protected $id;</li>
  </ul></li>
  </ol>
  
  <h3>3.2 Case 2: added setter for ID property in Role entity</h3>
  
  <p>I think it's wrong, but I did it just to be sure. After adding this code to Role entity:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function setId($id)
  {
      $this-&gt;id = $id;
      return $this;
  }
  </code></pre>
  
  <p>If I create new user and add a role, then SAVE... What happens is:</p>
  
  <ol>
  <li>New user is created</li>
  <li>New user has role with the desired ID assigned (yay!)</li>
  <li><strong>but that role's name is overwritten with empty string</strong> (bummer!)</li>
  </ol>
  
  <p>Obviously, thats not what I want. I don't want to edit/overwrite roles. I just want to add a relation between them and the User.</p>
  
  <h3>3.3 Case 3: Workaround suggested by Jeppe</h3>
  
  <p>When I first encountered this problem I ended up with a workaround, the same that Jeppe suggested. Today (for other reasons) I had to remake my form/view and the workaround stopped working.</p>
  
  <p>What changes in Case3 UserManagementController -> createAction:</p>
  
  <pre class="lang-php prettyprint-override"><code>  // in createAction
    // instead of $user = new User
    $user = $this-&gt;updateUser($request, new User());
  
    //and below updateUser function
  
  
      /**
       * Creates mew iser and sets its properties
       * based on request
       * 
       * @return User Returns configured user
       */
      protected function updateUser($request, $user)
      {
          if($request-&gt;getMethod() == 'POST')
          {
            $avo_user = $request-&gt;request-&gt;get('avo_user');
  
            /**
             * Setting and adding/removeing groups for user
             */
            $owned_groups = (array_key_exists('groups', $avo_user)) ? $avo_user['groups'] : array();
            foreach($owned_groups as $key =&gt; $group) {
              $owned_groups[$key] = $group['id'];
            }
  
            if(count($owned_groups) &gt; 0)
            {
              $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
              $groups = $em-&gt;getRepository('AvocodeUserBundle:Group')-&gt;findById($owned_groups);
              $user-&gt;setGroups($groups);
            }
  
            /**
             * Setting and adding/removeing roles for user
             */
            $owned_roles = (array_key_exists('avoRoles', $avo_user)) ? $avo_user['avoRoles'] : array();
            foreach($owned_roles as $key =&gt; $role) {
              $owned_roles[$key] = $role['id'];
            }
  
            if(count($owned_roles) &gt; 0)
            {
              $em = $this-&gt;getDoctrine()-&gt;getEntityManager();
              $roles = $em-&gt;getRepository('AvocodeUserBundle:Role')-&gt;findById($owned_roles);
              $user-&gt;setAvoRoles($roles);
            }
  
            /**
             * Setting other properties
             */
            $user-&gt;setUsername($avo_user['username']);
            $user-&gt;setEmail($avo_user['email']);
  
            if($request-&gt;request-&gt;has('generate_password'))
              $user-&gt;setPlainPassword($user-&gt;generateRandomPassword());  
          }
  
          return $user;
      }
  </code></pre>
  
  <p>Unfortunately this does not change anything.. the results are either CASE1 (with no ID setter) or CASE2 (with ID setter).</p>
  
  <h3>3.4 Case 4: as suggested by userfriendly</h3>
  
  <p>Adding cascade={"persist", "remove"} to mapping.</p>
  
  <pre class="lang-php prettyprint-override"><code>/**
   * @ORM\ManyToMany(targetEntity="Group", cascade={"persist", "remove"})
   * @ORM\JoinTable(name="avo_user_avo_group",
   *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
   * )
   */
  protected $groups;
  
  /**
   * @ORM\ManyToMany(targetEntity="Role", cascade={"persist", "remove"})
   * @ORM\JoinTable(name="avo_user_avo_role",
   *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="role_id", referencedColumnName="id")}
   * )
   */
  protected $avoRoles;
  </code></pre>
  
  <p>And changeing <strong>by_reference</strong> to <strong>false</strong> in FormType:</p>
  
  <pre class="lang-php prettyprint-override"><code>// ...
  
                  -&gt;add('avoRoles', 'collection_list', array(
                          'type' =&gt; new RoleNameType(),
                          'allow_add' =&gt; true,
                          'allow_delete' =&gt; true,
                          'by_reference' =&gt; false,
                          'error_bubbling' =&gt; false,
                          'prototype' =&gt; true,
                       ));
  
  // ...
  </code></pre>
  
  <p>And keeping workaround code suggested in 3.3 did change something:</p>
  
  <ol>
  <li>Association between user and role was <strong>not created</strong></li>
  <li>.. but Role entity's name was overwritten by empty string (like in 3.2)</li>
  </ol>
  
  <p>So.. it did change something but in the wrong direction.</p>
  
  <h1>4. Versions</h1>
  
  <h3>4.1 Symfony2 v2.0.15</h3>
  
  <h3>4.2 Doctrine2 v2.1.7</h3>
  
  <h3>4.3 FOSUserBundle version: <a href="https://github.com/FriendsOfSymfony/FOSUserBundle/commit/6fb81861d84d460f1d070ceb8ec180aac841f7fa">6fb81861d84d460f1d070ceb8ec180aac841f7fa</a></h3>
  
  <h1>5. Summary</h1>
  
  <p>I've tried many diffrent approaches (above are only the most recent ones) and after hours spent on studying code, google'ing and looking for the answer I just couldn't get this working. </p>
  
  <p>Any help will be greatly appreciated. If you need to know anything I'll post whatever part of code you need.</p>