title: Too many joins with Doctrine using "Class Table Inheritance" on a Symfony project
body: <p>I'm working with Symfony and Doctrine and I ended up with the following errors:</p>
  
  <blockquote>
    <p>SQLSTATE[HY000]: General error: 1116 Too many tables; MySQL can only
    use 61 tables in a join`</p>
  </blockquote>
  
  <p>The main entity (topmost table of the hierarchy) contains a discriminator map of over 61 entities. A "Many-To-Many" association is made over itself, so an Entity can be linked to others as parents or children.</p>
  
  <p><strong>Here is the Entity Doctrine annotation</strong> :</p>
  
  <pre><code>/**
   * Entity
   *
   * @ORM\Table(name="entity")
   * @ORM\Entity(repositoryClass="Acme\EntityBundle\Repository\EntityRepository")
   * @InheritanceType("JOINED")
   * @DiscriminatorColumn(name="type", type="string")
   * @DiscriminatorMap({
   *      "sub_entity_1" = "\Acme\SubEntityABundle\Entity\E1",
   *      "sub_entity_2" = "\Acme\SubEntityABundle\Entity\E2",
   *      ...
   * })
   */
  class Entity
  {
      /**
       * @ORM\ManyToMany(targetEntity="Entity", inversedBy="parentEntities")
       * @ORM\JoinTable(name="rel_entity_entity",
       *   joinColumns={@ORM\JoinColumn(name="parent", referencedColumnName="id")},
       *   inverseJoinColumns={@ORM\JoinColumn(name="child", referencedColumnName="id")}
       * )
       */
      private $childrenEntities;
  
      /**
       * @ORM\ManyToMany(targetEntity="Entity", mappedBy="childrenEntities")
       *
       */
      private $parentEntities;
  
      /**
       * Get children entities
       *
       * @return \Doctrine\Common\Collections\Collection
       */
      public function getChildrenEntities()
      {
          return $this-&gt;childrenEntities;
      }
  
      /**
       * Set childrenEntities
       *
       * @param ArrayCollection $entities
       */
       public function setchildrenEntities(ArrayCollection $entities)
       {
           $this-&gt;childrenEntities = $entities;
       }
  
       ...
  }
  </code></pre>
  
  <p>Whenever I use the Query Builder in a formType to get a list of some classic Entities (which do not even extend the topclass "Entity"), Doctrine will do a left join on every entities in the discriminator map (and generate the General error of MySQL).</p>
  
  <p><strong>FormType</strong> :</p>
  
  <pre><code>public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder-&gt;add('childrenEntities', 'entity', array(
          'class' =&gt; 'AcmeEntityBundle:Entity',
          'required' =&gt; false,
          'choice_label' =&gt; 'slug', // slug is defined as a main entity's attribute
          'query_builder' =&gt; function (EntityRepository $er) {
              return $er-&gt;getSomeEntities();
          },
      ));
  
      ...
  }
  </code></pre>
  
  <p><strong>EntityRepository</strong> :</p>
  
  <pre><code>public function getSomeEntities()
  {
      return $this-&gt;getEntityManager()
          -&gt;createQueryBuilder()
          -&gt;select('e')
          -&gt;from('AcmeEntityBundle:Entity', 'e')
          -&gt;where('e.id IN (:ids)')
          // Tried that, doesn't work
          //-&gt;andWhere('e INSTANCE OF AcmeEntityBundle:Entity')
          -&gt;setParameter('ids', [53300, 12345]);
  }
  </code></pre>
  
  <p>Is there a way to tell doctrine not to make any join? I only need main entity's attribute, such an id or a slug, so I don't need any value from sub classes</p>
  
  <p>Doctrine version is : <strong>2.4</strong></p>
  
  <p>Some usefull doc I read :</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/29695371/query-field-of-root-entity-in-doctrine-joined-class-table-inheritance">Query field of root entity in doctrine (joined) class table inheritance</a></li>
  <li><a href="https://stackoverflow.com/questions/13860375/doctrine2-arbitrary-join-and-single-table-inheritance">Doctrine2: Arbitrary join and single table inheritance</a></li>
  <li><a href="http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#class-table-inheritance" rel="nofollow noreferrer">http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#class-table-inheritance</a></li>
  </ul>
  
  <p>Thank you!</p>