title: Doctrine 2.4 can't persist/insert many to many association entities
body: <p>people can you help me? im having troubles persisting an entity (Solicitud) on Doctrine 2.4 ,the entity has all of its associated objects already stored on its properties, when im passing the object to the entity manager for persist and flush.</p>
  
  <p>The fully descripted relations should be...</p>
  
  <ol>
  <li>Usuario-> Solicitudes (One to Many)</li>
  <li>Solicitud-> Servicios (Many to Many)</li>
  <li>Category->Servicios (One to Many)</li>
  </ol>
  
  <p>I did generated the entities and the DB structure from my XML Mapping...</p>
  
  <p>On my "solicitud" XML i have defined the relation as you can see below...and the intermediate table got effectively created when i executed the command:</p>
  
  <pre><code>vendor\bin\doctrine orm:schema-tool:update --force
  </code></pre>
  
  <p>with two columns and everything ok.</p>
  
  <p>this is on "Solicitud" on the fields that define the many to many relation</p>
  
  <pre><code>&lt;many-to-many field="servicios" target-entity="Servicio"&gt;
  &lt;cascade&gt;
  &lt;cascade-persist/&gt;
  &lt;/cascade&gt;
  &lt;join-table name="solicitud_servicio"&gt;
  &lt;join-columns&gt;
  &lt;join-column name="id_solicitud" referenced-column-name="id"/&gt;
  &lt;/join-columns&gt;
  &lt;inverse-join-columns&gt;
  &lt;join-column name="id_servicio" referenced-column-name="id"/&gt;
  &lt;/inverse-join-columns&gt;
  &lt;/join-table&gt;
  </code></pre>
  
  <p></p>
  
  <p>and this on Servicio</p>
  
  <pre><code>&lt;doctrine-mapping xmlns="http://doctrine-project.org..." 
    xmlns:xsi="http://www.w3.org/2001/XMLS..." 
    xsi:schemalocation="http://doctrine-project.org... http://doctrine-
    project.org..."&gt;
    &lt;entity name="Servicio" table="servicio"&gt;
    &lt;indexes&gt;
    &lt;index name="id_categoria" columns="id_categoria"/&gt;
    &lt;/indexes&gt;
    &lt;id name="id" type="integer" column="id"&gt;
    &lt;generator strategy="IDENTITY"/&gt;
    &lt;/id&gt;
     &lt;field name="codigo" type="string" column="codigo" length="15" 
    nullable="false"/&gt;
    &lt;field name="nombre" type="string" column="nombre" length="70" 
    nullable="false"/&gt;
    &lt;field name="detalle" type="string" column="detalle" length="250" 
    nullable="false"/&gt;
    &lt;field name="precioPublico" type="integer" column="precio_publico" 
    nullable="false"/&gt;
   &lt;field name="precioPrivado" type="integer" column="precio_privado" 
    nullable="false"/&gt;
    &lt;field name="medida" type="string" column="medida" length="15" 
    nullable="false"/&gt;
    &lt;field name="planificacion" type="integer" column="planificacion" 
    nullable="false"/&gt;
    &lt;field name="fabricacion" type="integer" column="fabricacion" 
    nullable="false"/&gt;
    &lt;field name="fechaCreacion" type="datetime" column="fecha_creacion" 
    nullable="true"/&gt;
    &lt;field name="ultimaModificacion" type="datetime" column="ultima_modificacion" 
    nullable="false"/&gt;
    &lt;many-to-one field="idCategoria" target-entity="Categoria"&gt;
    &lt;join-columns&gt;
    &lt;join-column name="id_categoria" referenced-column-name="id"/&gt;
    &lt;/join-columns&gt;
    &lt;/many-to-one&gt;
    &lt;/entity&gt;
  &lt;/doctrine-mapping&gt;
  </code></pre>
  
  <p>Is there something i'm missing? maybe another sentence calling to persist on another of the classes involved/related?</p>
  
  <p>i'm trying to persist through the "solicitud" entity in the entity manager sentences... i have them separated on a different layer as DAO, but it makes no problem, when the data arrives to this layer, i tried var_dump , and everything is okay.</p>
  
  <p>in my "SolicitudService" i wrote this function to collect all the related objects needed for persist , i use VO (virtual objects) with functions inside to build the data from object to Json or from Json to Objects and toEntity Pattern(from VO to Object of the needed class).</p>
  
  <p>what i do in this function:</p>
  
  <ol>
  <li>First i find the objects that i have on my SolicitudServicioVO The following three sentences after the try, then i create the new object Solicitud.</li>
  <li>Then i set the objects founded via DAO entities, into the "Solicitud" new object (5 lines after the "//set Solicitud Properties" comment).</li>
  <li>After that i iterate,($solicitudServicioVO->listadoServicio), through the listadoServicio ,(list of services coming from the front end), and create VO for each one, that are able in the next sentences to build themselves as an entity "or class of the needed type", in this case the ones that i mentioned in the relations definition at the beginning of this question.</li>
  <li><p>i send to persist, on this sentence</p>
  
  <pre><code>$this-&gt;solicitudDAO-&gt;create($solicitud);
  </code></pre></li>
  </ol>
  
  <p>that leads to the DAO that has...</p>
  
  <pre><code>function create($solicitud){
    $this-&gt;em-&gt;persist($solicitud);
    $this-&gt;em-&gt;flush();
    return $solicitud;
  }
  </code></pre>
  
  <p>This function could be more descriptive on its name by maybe calling it createSolicitud as its purpose its to create a new one, but nevermind...it also has to add a Solicitud related to the user.. so i insist , nevermind that by now.</p>
  
  <pre><code>function addSolicitud($solicitudServicioVO){
  try{
    $usuario = $this-&gt;usuarioDAO-&gt;find($solicitudServicioVO-&gt;getIdUsuario()-&gt;id);
    $direccion = $this-&gt;direccionDAO-&gt;find($solicitudServicioVO-
    &gt;getIdDireccion()-&gt;id);
    $facturacion = $this-&gt;datoFacturacionDAO-&gt;find($solicitudServicioVO-
    &gt;getIdFacturacion()-&gt;id);
  
    $solicitud = new Solicitud();
  
    //set Solicitud Properties
    $solicitud-&gt;setFechaCreacion(new DateTime());
    $solicitud-&gt;setEstado("solicitado");
    $solicitud-&gt;setIdFacturacion($facturacion);
    $solicitud-&gt;setIdDireccion($direccion);
    $solicitud-&gt;setIdUsuario($usuario);
  
    foreach($solicitudServicioVO-&gt;listadoServicio as $servicioVO)
    { 
      $categoriaServicioVO = $this-&gt;categoriaService-&gt;getCategoria($servicioVO-
      &gt;getCategoriaId());
      $servicio = $servicioVO-&gt;toEntity();
      $servicio-&gt;setIdCategoria($categoriaServicioVO-&gt;toEntity());
      $solicitud-&gt;addServicio($servicio);
    }
  
    $resp = $this-&gt;solicitudDAO-&gt;create($solicitud);
    $response = Utils::getInstancia()-&gt;httpResponseSuccess('001');
    $response['data'] = $resp;
    return $response; 
    }
    catch(Exception $e){
    $response = $e-&gt;getMessage();
    }
  }
  </code></pre>
  
  <p>So, i was the most descriptive that i could , im not bieng able to persist this data about the SOlicitud entity...do someone has already a similar problem or any extra ideas...maybe in the XML definition?, it does got trough the entities as annotations and nothing seems to be wrong.</p>
  
  <p>Please help me , this many to many cancer has been taking almost a week without resolution jajaja</p>