title: Symfony2 Doctrine2 Many-To-Many relation with two Owning Sides and Doctrine cmd line tools
body: <p>In my Symdony2 project I've two related entities: Service and ServiceGroup. This should be many-to-many relationship, because each group can have many services, and each service can belongs to many groups. Moreover, I need a user interface to manage services and groups. So, when editing a Service, user should be able to choose to which groups it belongs. Analogously, when editing a ServiceGroup user should be able to choose which services belongs to this group. I've already achieved this by setting up a Many-To-Many relation in my Doctrine entites. Everything is working like a charm, including user interface build on custom form types in Symfony2 (I've used &quot;entity&quot; form field type to allow user to select services in ServiceGroup editor and groups in Service editor). The only problem I've is that I cannot use Doctrine command line to update database schema anymore.</p>
  <p>Here is part of my Service entity source code:</p>
  <pre><code>class Service
  {
      /**
       * @var ArrayCollection $groups
       * @ORM\ManyToMany(targetEntity=&quot;ServiceGroup&quot;)
       * @ORM\JoinTable(
       *      name=&quot;service_servicegroup&quot;,
       *      joinColumns={@ORM\JoinColumn(name=&quot;service_id&quot;, referencedColumnName=&quot;id&quot;)},
       *      inverseJoinColumns={@ORM\JoinColumn(name=&quot;servicegroup_id&quot;, referencedColumnName=&quot;id&quot;)}
       * )
       */
      private $groups;
  }
  </code></pre>
  <p>And here is part of my ServiceGroup entity source code:</p>
  <pre><code>class ServiceGroup
  {
      /**
       * @var ArrayCollection $services
       * @ORM\ManyToMany(targetEntity=&quot;Service&quot;)
       * @ORM\JoinTable(
       *      name=&quot;service_servicegroup&quot;,
       *      joinColumns={@ORM\JoinColumn(name=&quot;servicegroup_id&quot;, referencedColumnName=&quot;id&quot;)},
       *      inverseJoinColumns={@ORM\JoinColumn(name=&quot;service_id&quot;, referencedColumnName=&quot;id&quot;)}
       * )
       */
      private $services;
  }
  </code></pre>
  <p>I using JoinTable in both cases, because this is the only way I found working when it comes to save relations in user interface editors, which looks like this:</p>
  <p>Service editor:</p>
  <blockquote>
  <p><strong>Service editor</strong></p>
  <p>Name: [ Service 1 ]</p>
  <p>Groups to which this service belongs:</p>
  <p>[x] Group A</p>
  <p>[ ] Group B</p>
  <p>[ ] Group C</p>
  <p>[ <strong>SAVE</strong> ]</p>
  </blockquote>
  <p>And ServiceGroup editor:</p>
  <blockquote>
  <p><strong>Group editor</strong></p>
  <p>Name: [ Group A ]</p>
  <p>Services belongs to this group:</p>
  <p>[x] Service 1</p>
  <p>[ ] Service 2</p>
  <p>[ ] Service 3</p>
  <p>[ <strong>SAVE</strong> ]</p>
  </blockquote>
  <p>With this Many-To-Many configuration I'm able to use this editors (forms) without a problem, when using Many-To-Many without JoinTable annotation, I'm able to use only one form completely, and the second one is not saving changes in &quot;Groups to which this service belongs&quot; or &quot;Services belongs to this group&quot; option (depends on in which direction I'm setting mappedBy and inversedBy parameters in Many-To-Many annotation statement).</p>
  <p>The problem I have is connected with doctrine schema generation mechanism, when trying to update schema using Symfony2 command:</p>
  <pre><code>php app/console doctrine:schema:update --dump-sql
  </code></pre>
  <p>I'm getting this exception:</p>
  <pre><code>[Doctrine\DBAL\Schema\SchemaException]                      
  The table with name 'service_servicegroup' already exists.
  </code></pre>
  <p>It looks like Doctrine trying to create 'service_servicegroup' table for each JoinTable statement. So, it's working on current schema, which I've build in database using the same command, but step-by-step, first when no Many-To-Many relation defined and next with only one Many-To-Many relation definition (for Service entity). When I've added Many-To-Many relation to second entity (ServiceGroup), my application seams to be working without a problem from user point of view, but I'm not able to use 'doctrine:schema:update' command anymore.</p>
  <p>I've no idea what is wrong with my code, maybe this relation should be implemented different way, or maybe it's a Doctrine bug/limitation. Any help or suggestion would be appreciated.</p>
  <p><strong>UPDATE:</strong></p>
  <p>I've noticed that what I need is to configure ManyToMany relation to have two owning sides. Default is having one owning side and one inverse side. Doctrine <a href="http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/reference/association-mapping.html#owning-side-and-inverse-side" rel="noreferrer">documentation</a> tells that you can have two owning sides in ManyToMany relation, but doesn't explain it a lot. Anyone can give an example?</p>
  <p><strong>WORKAROUND:</strong></p>
  <p>I've found a workaround solutions, that maybe isn't ideal but it's working for me.
  Since there is no way to have two owning sides in many-to-many relation, I've changed Doctrine annotation for my entites. Service entity is now the owning side:</p>
  <pre><code>class Service
  {
      /**
       * @var ArrayCollection $groups
       * @ORM\ManyToMany(targetEntity=&quot;ServiceGroup&quot;, inversedBy=&quot;services&quot;, cascade={&quot;all&quot;})
       * @ORM\JoinTable(
       *      name=&quot;service_to_group_assoc&quot;,
       *      joinColumns={@ORM\JoinColumn(name=&quot;service_id&quot;, referencedColumnName=&quot;id&quot;)},
       *      inverseJoinColumns={@ORM\JoinColumn(name=&quot;group_id&quot;, referencedColumnName=&quot;id&quot;)}
       * )
       */
      private $groups;
  }
  </code></pre>
  <p>And ServiceGroup entity is inverse side:</p>
  <pre><code>class ServiceGroup
  {
      /**
       * @var ArrayCollection $services
       * @ORM\ManyToMany(targetEntity=&quot;Service&quot;, mappedBy=&quot;groups&quot;, cascade={&quot;all&quot;})
       */
      private $services;
  }
  </code></pre>
  <p>Unfortunately, with this configuration, relation is updated only when updating Service entity. When I change $services in ServiceGroup object and persist it, the relation will be unchanged. So, I've change my Controller class, and with a help of small workaround solution, I've achieved expected result. This is a part of my Controller code, which is responsible for updating ServiceGroup entity (with a use of custom form type):</p>
  <pre><code>// before update - get copy of currently related services:
  $services = clone $group-&gt;getServices();
  
  // ...
  
  // when $form-&gt;isValid() etc. updating the ServiceGroup entity:
  
  // add selected services to group
  foreach($group-&gt;getServices() as $service)
  {
      $service-&gt;addServiceGroup($group);
      $services-&gt;removeElement($service);
  }
  
  // remove unselected services from group
  foreach($services as $service)
  {
      $service-&gt;removeServiceGroup($group);
  }
  </code></pre>
  <p>This are implementations of addServiceGroup and removeServiceGroup methods of Service entity class:</p>
  <pre><code>/**
   * Add group
   *
   * @param ServiceGroup $groups
   */
  public function addServiceGroup(ServiceGroup $groups)
  {
      if(!in_array($groups, $this-&gt;groups-&gt;toArray()))
      {
          $this-&gt;groups[] = $groups;
      }
  }
  
  /**
   * Remove group
   *
   * @param ServiceGroup $groups
   */
  public function removeServiceGroup(ServiceGroup $groups)
  {
      $key = $this-&gt;groups-&gt;indexOf($groups);
      
      if($key!==FALSE) 
      {
          $this-&gt;groups-&gt;remove($key);
      }
  }
  </code></pre>
  <p>Now I have working many-to-many relation with owning (Service) and inverse (ServiceGroup) side, and forms that updated both entity and relation when saving (default form for Service entity is enough, but for ServiceGroup I've provided above mentioned modifications). The Symfony/Doctrine console tools are working like a charm. This probably can be solved in better (simpler?) way, but for me this is enough for now.</p>