title: Symfony/Doctrine: access to the join tables?
body: <p>In order to circumvent <a href="https://stackoverflow.com/questions/38624790/symfony-doctrine-variations-of-entities">a problem I had with variations of entities</a> the dirty way, I'm trying to constrain the number of colours available for a device repair. The available colours must not exceed the colours available for the device.</p>
  
  <p>So I have:</p>
  
  <p><strong>Device.php</strong></p>
  
  <p>This maps the device to the colours and the repairs available for this device.</p>
  
  <pre><code>/**
   * @ORM\OneToMany(targetEntity="Repair", mappedBy="device", cascade={"remove"})
   */
  private $repairs;
  
  /**
   * @ORM\ManyToMany(targetEntity="Color", inversedBy="devices")
   * @ORM\JoinTable(name="devices_colors",
   *      joinColumns={@ORM\JoinColumn(name="device_id", referencedColumnName="id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="color_id", referencedColumnName="id")}
   *      ) 
   */
  private $colors;
  </code></pre>
  
  <p><strong>Color.php</strong></p>
  
  <pre><code>/**
   * @ORM\ManyToMany(targetEntity="Device", mappedBy="colors")
   */
  private $devices;
  </code></pre>
  
  <p><strong>Repair.php</strong></p>
  
  <p>This maps a repair job to a device but also to a colour.</p>
  
  <pre><code>/**
   * @ORM\ManyToOne(targetEntity="Device", inversedBy="repairs")
   * @ORM\JoinColumn(name="device_id", referencedColumnName="id")
   */
  private $device;
  
  /**
   * @ORM\ManyToMany(targetEntity="Color")
   * @ORM\JoinTable(name="repairs_colors",
   *      joinColumns={@ORM\JoinColumn(name="repair_id", referencedColumnName="id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="color_id", referencedColumnName="id")}
   *      ) 
   */
  private $colors;
  </code></pre>
  
  <p><strong>RepairType.php</strong></p>
  
  <p>Point here is to:
  map this field 'colors' to the colours available in the <code>Color</code> Entity but limit these choices to what is available for this instance of <code>Device</code></p>
  
  <pre><code>   -&gt;add('colors', EntityType::class, array(
                'label'=&gt;'Kleuren',
                'class'=&gt;'AppBundle:Color',
                'choice_label'=&gt;'name',
                'choice_value'=&gt;'id',
                'multiple'=&gt;true,
                'expanded'=&gt;true,
                'query_builder'=&gt;function (EntityRepository $er) use ($device) {
                            return $er-&gt;createQueryBuilder('c')
                                  -&gt;join('AppBundle:Device','d')
                                  -&gt;where('d.id = :device')
                                  -&gt;setParameter('device', $device);
                      }
                )
          )
  </code></pre>
  
  <p>Because I know that a devices_colors join table is created, I can achieve the desired result through the following MySQL query directly on the server:</p>
  
  <pre><code>SELECT c.name FROM colors c LEFT JOIN devices_colors d ON (d.color_id=c.id) WHERE d.device_id = 1
  </code></pre>
  
  <p>However, my attempts above in RepairType do not achieve the same results. I always get <em>all</em> the colours available, not just the ones that have been set on <code>Device</code>. What am I doing wrong?</p>