title: Query Builder or Closure or DQL, which is the right one for get ride of this complex SQL query with ManyToMany relationship?
body: <p>So I've this two entities:</p>
  
  <pre><code>class TipoRegistro
  {
      /**
       * @ORM\ManyToMany(targetEntity="AppBundle\Entity\TipoTramite", inversedBy="tipoRegistros", cascade={"persist"})
       * @ORM\JoinTable(name="nomencladores.tipo_registro_tipo_tramite", schema="nomencladores",
       *      joinColumns={@ORM\JoinColumn(name="tipo_registro_id", referencedColumnName="id")},
       *      inverseJoinColumns={@ORM\JoinColumn(name="tipo_tramite_id", referencedColumnName="id")}
       * )
       */
      protected $tipoTramites;
  }
  
  class TipoTramite
  {
      /**
       * @ORM\ManyToMany(targetEntity="AppBundle\Entity\TipoRegistro", mappedBy="tipoTramites", cascade={"persist"})
       */
      protected $tipoRegistros;
  }
  </code></pre>
  
  <p>I need to get all the <code>TipoRegistro</code> associated to <code>TipoTramite</code> trhough ManyToMany relationship as show before as the SQL below shows: </p>
  
  <pre><code>SELECT tr.* 
  FROM
      nomencladores.tipo_registro tr
  LEFT JOIN
      nomencladores.tipo_registro_tipo_tramite AS trtt
  ON (trtt.tipo_registro_id = tr."id")
  WHERE
      tr.activo = true
  AND
      trtt.tipo_tramite_id = 1;
  </code></pre>
  
  <p>I don't know if I can use a custom repository here so my only solution is to use <code>query_builder</code> parameter at form level and this is what I'm doing:</p>
  
  <pre><code>'query_builder' =&gt; function (EntityRepository $er) {
      return $er-&gt;createQueryBuilder('tr')
          -&gt;where('tr.activo = :activo')
          -&gt;leftJoin('tr.tipoTramites', 'ttr')
          -&gt;andWhere("ttr.tipo_tramite_id  = :tramite")
          -&gt;setParameter("tramite", 1)
          -&gt;setParameter('activo', true);
  }
  </code></pre>
  
  <p>But that solution cause this error:</p>
  
  <blockquote>
    <p>[Semantical Error] line 0, col 121 near 'tipo_tramite_id': Error:
    Class AppBundle\Entity\TipoTramite has no field or
    association named tipo_tramite_id</p>
  </blockquote>
  
  <p>I tried this one also (as suggested per user here):</p>
  
  <pre><code>-&gt;andWhere("ttr.tipoTramites  = :tramite")
  </code></pre>
  
  <p>But the errors turns into this one:</p>
  
  <blockquote>
    <p>[Semantical Error] line 0, col 120 near 'tipoTramites': Error: Invalid
    PathExpression. StateFieldPathExpression or
    SingleValuedAssociationField expected</p>
  </blockquote>
  
  <p>Also I've tried this other solution:</p>
  
  <pre><code>-&gt;andWhere("tr.id = :tramite")
  </code></pre>
  
  <p>But this one doesn't return the right values. I read something about <a href="http://symfony.com/doc/current/reference/forms/types/entity.html#query-builder" rel="nofollow"><code>Closures</code></a> on the entity field type but has no idea in how to implement on this scenario. The other idea comes to me is use a DQL and a preparement statement but I thin this can't be used at form level, so I'm out of ideas, then how I can get those values? How I should establish the relation between this ManyToMany entities?</p>