title: Doctrine Issue - Entities not joining correctly
body: <p>So I'm having a strange problem with Doctrine that I don't understand and need help with. Essentially, I am running quite a complex query due to the nature of my database and its various joins and for some reason the response when I grab try to grab all records from my database is returning my entities incorrectly.</p>
  <p>See my doctrine query below:</p>
  <pre><code>$subQuery = $this-&gt;_em-&gt;createQueryBuilder()
              -&gt;from(Product::class, 'ex')
              -&gt;select('ppsi.id')
              -&gt;join('ex.productSpecificationItems', 'ppsi')
              -&gt;where('ex.id = p.id');
          $subQuery = $subQuery-&gt;getDQL();
  
          $subQuery2 = $this-&gt;_em-&gt;createQueryBuilder()
              -&gt;from(Product::class, 'ex2')
              -&gt;select('ppoiv.id')
              -&gt;join('ex2.productOptionItemValues', 'ppoiv')
              -&gt;where('ex2.id = p.id');
          $subQuery2 = $subQuery2-&gt;getDQL();
  
          $subQuery3 = $this-&gt;_em-&gt;createQueryBuilder()
              -&gt;from(Product::class, 'ex3')
              -&gt;select('poie.id')
              -&gt;join('ex3.productOptionItems', 'poie')
              -&gt;where('ex3.id = p.id');
          $subQuery3 = $subQuery3-&gt;getDQL();
  
          $qb = $this-&gt;createQueryBuilder('p')
              -&gt;select('p, pc, psc, i, po, poi, poiv, ps, psi')
              -&gt;join('p.productCategory', 'pc')
              -&gt;leftJoin('p.productSubCategory', 'psc')
              -&gt;leftJoin('p.images', 'i')
              -&gt;leftJoin('p.productOptions', 'po')
              -&gt;leftJoin('p.productSpecifications', 'ps')
              -&gt;leftJoin('ps.productSpecificationItems', 'psi', Join::WITH, 'psi.id IN (' . $subQuery. ')')
              -&gt;leftJoin('po.productOptionItems', 'poi', Join::WITH, 'poi.id IN (' . $subQuery3. ')')
              -&gt;leftJoin('poi.productOptionItemValue', 'poiv', Join::WITH, 'poiv.id IN (' . $subQuery2. ')')
              -&gt;addOrderBy('p.id', 'ASC');
  </code></pre>
  <p>The response of this causes some entities to have the productOptionItemValues either listed under the wrong productOptionItem, just causes blank records to return, or doesn't return them at all. See an example below (as a JSON for clarity):</p>
  <pre><code>{
          &quot;id&quot;: 373,
          &quot;name&quot;: &quot;test&quot;,
          &quot;code&quot;: &quot;test&quot;,
          &quot;url&quot;: &quot;373-test&quot;,
          &quot;price&quot;: &quot;1.00&quot;,
          &quot;stock&quot;: 1,
          &quot;description&quot;: &quot;&lt;p&gt;fghfgh mnsdbfksdhfjk sd&lt;/p&gt;\n&quot;,
          &quot;live&quot;: false,
          &quot;createdAt&quot;: &quot;2022-03-17T14:45:35+00:00&quot;,
          &quot;updatedAt&quot;: &quot;2022-03-17T15:12:44+00:00&quot;,
          &quot;featured&quot;: false,
          &quot;aggregateReviewScore&quot;: null,
          &quot;productCategory&quot;: {
              &quot;id&quot;: 2,
              &quot;name&quot;: &quot;Hex Set Screws&quot;,
              &quot;url&quot;: &quot;hex-set-screws&quot;
          },
          &quot;productSubCategory&quot;: {
              &quot;id&quot;: 12,
              &quot;name&quot;: &quot;Nylon&quot;,
              &quot;url&quot;: &quot;nylon&quot;
          },
          &quot;images&quot;: [],
          &quot;productOptions&quot;: [
              {
                  &quot;id&quot;: 1,
                  &quot;name&quot;: &quot;Colour&quot;,
                  &quot;productOptionItems&quot;: {
                      &quot;0&quot;: {
                          &quot;id&quot;: 2,
                          &quot;name&quot;: &quot;Black&quot;,
                          &quot;productOptionItemValue&quot;: [
                              {
                                  &quot;id&quot;: 1196,
                                  &quot;stock&quot;: 1,
                                  &quot;price&quot;: &quot;1.00&quot;,
                                  &quot;useOwn&quot;: false
                              },
                              {
                                  &quot;id&quot;: 1201,
                                  &quot;stock&quot;: 1,
                                  &quot;price&quot;: &quot;1.00&quot;,
                                  &quot;useOwn&quot;: false
                              },
                              {
                                  &quot;id&quot;: 1198,
                                  &quot;stock&quot;: 1,
                                  &quot;price&quot;: &quot;1.00&quot;,
                                  &quot;useOwn&quot;: false
                              }
                          ]
                      },
                      &quot;1&quot;: {
                          &quot;id&quot;: 8,
                          &quot;name&quot;: &quot;Yellow&quot;,
                          &quot;productOptionItemValue&quot;: [
                              {
                                  &quot;id&quot;: 1197,
                                  &quot;stock&quot;: 1,
                                  &quot;price&quot;: &quot;1.00&quot;,
                                  &quot;useOwn&quot;: false
                              }
                          ]
                      },
                      &quot;2&quot;: {
                          &quot;id&quot;: 14,
                          &quot;name&quot;: &quot;Light Grey&quot;
                      },
                      &quot;3&quot;: null,
                      &quot;4&quot;: {
                          &quot;id&quot;: 15,
                          &quot;name&quot;: &quot;Mid Grey&quot;
                      },
                      &quot;7&quot;: null,
                      &quot;8&quot;: {
                          &quot;id&quot;: 20,
                          &quot;name&quot;: &quot;Light Blue&quot;
                      },
                      &quot;11&quot;: null,
                      &quot;12&quot;: {
                          &quot;id&quot;: 22,
                          &quot;name&quot;: &quot;Purple&quot;
                      },
                      &quot;13&quot;: null
                  }
              },
              {
                  &quot;id&quot;: 2,
                  &quot;name&quot;: &quot;Pack Size&quot;,
                  &quot;productOptionItems&quot;: [
                      {
                          &quot;id&quot;: 3,
                          &quot;name&quot;: &quot;10&quot;,
                          &quot;productOptionItemValue&quot;: [
                              {
                                  &quot;id&quot;: 1180,
                                  &quot;stock&quot;: 100,
                                  &quot;price&quot;: &quot;10.00&quot;,
                                  &quot;useOwn&quot;: true
                              }
                          ]
                      },
                      {
                          &quot;id&quot;: 4,
                          &quot;name&quot;: &quot;100&quot;,
                          &quot;productOptionItemValue&quot;: [
                              {
                                  &quot;id&quot;: 1181,
                                  &quot;stock&quot;: 10,
                                  &quot;price&quot;: &quot;1000.00&quot;,
                                  &quot;useOwn&quot;: true
                              }
                          ]
                      }
                  ]
              }
          ],
          &quot;productSpecifications&quot;: []
      }
  </code></pre>
  <p>If I run the same query on a SINGLE product (so the same Doctrine Query as a above just with a p.id = :id WHERE added) it returns absolutely fine. See the response for that below (and the CORRECT response I am looking for):</p>
  <pre><code>{
      &quot;id&quot;: 373,
      &quot;name&quot;: &quot;test&quot;,
      &quot;code&quot;: &quot;test&quot;,
      &quot;url&quot;: &quot;373-test&quot;,
      &quot;price&quot;: 1,
      &quot;stock&quot;: 1,
      &quot;description&quot;: &quot;&lt;p&gt;fghfgh mnsdbfksdhfjk sd&lt;/p&gt;\n&quot;,
      &quot;live&quot;: false,
      &quot;createdAt&quot;: &quot;2022-03-17T14:45:35+00:00&quot;,
      &quot;updatedAt&quot;: &quot;2022-03-17T15:12:44+00:00&quot;,
      &quot;featured&quot;: false,
      &quot;aggregateReviewScore&quot;: null,
      &quot;productCategory&quot;: {
          &quot;id&quot;: 2,
          &quot;name&quot;: &quot;Hex Set Screws&quot;,
          &quot;url&quot;: &quot;hex-set-screws&quot;
      },
      &quot;productSubCategory&quot;: {
          &quot;id&quot;: 12,
          &quot;name&quot;: &quot;Nylon&quot;,
          &quot;url&quot;: &quot;nylon&quot;
      },
      &quot;images&quot;: [],
      &quot;productOptions&quot;: [
          {
              &quot;id&quot;: 1,
              &quot;name&quot;: &quot;Colour&quot;,
              &quot;productOptionItems&quot;: [
                  {
                      &quot;id&quot;: 2,
                      &quot;name&quot;: &quot;Black&quot;,
                      &quot;productOptionItemValue&quot;: {
                          &quot;id&quot;: 1196,
                          &quot;stock&quot;: 1,
                          &quot;price&quot;: 1,
                          &quot;useOwn&quot;: false
                      }
                  },
                  {
                      &quot;id&quot;: 7,
                      &quot;name&quot;: &quot;Red&quot;,
                      &quot;productOptionItemValue&quot;: {
                          &quot;id&quot;: 1201,
                          &quot;stock&quot;: 1,
                          &quot;price&quot;: 1,
                          &quot;useOwn&quot;: false
                      }
                  },
                  {
                      &quot;id&quot;: 8,
                      &quot;name&quot;: &quot;Yellow&quot;,
                      &quot;productOptionItemValue&quot;: {
                          &quot;id&quot;: 1197,
                          &quot;stock&quot;: 1,
                          &quot;price&quot;: 1,
                          &quot;useOwn&quot;: false
                      }
                  },
                  {
                      &quot;id&quot;: 14,
                      &quot;name&quot;: &quot;Light Grey&quot;,
                      &quot;productOptionItemValue&quot;: {
                          &quot;id&quot;: 1202,
                          &quot;stock&quot;: 1,
                          &quot;price&quot;: 1,
                          &quot;useOwn&quot;: false
                      }
                  },
                  {
                      &quot;id&quot;: 15,
                      &quot;name&quot;: &quot;Mid Grey&quot;,
                      &quot;productOptionItemValue&quot;: {
                          &quot;id&quot;: 1205,
                          &quot;stock&quot;: 1,
                          &quot;price&quot;: 1,
                          &quot;useOwn&quot;: false
                      }
                  },
                  {
                      &quot;id&quot;: 20,
                      &quot;name&quot;: &quot;Light Blue&quot;,
                      &quot;productOptionItemValue&quot;: {
                          &quot;id&quot;: 1203,
                          &quot;stock&quot;: 1,
                          &quot;price&quot;: 1,
                          &quot;useOwn&quot;: false
                      }
                  },
                  {
                      &quot;id&quot;: 22,
                      &quot;name&quot;: &quot;Purple&quot;,
                      &quot;productOptionItemValue&quot;: {
                          &quot;id&quot;: 1204,
                          &quot;stock&quot;: 1,
                          &quot;price&quot;: 1,
                          &quot;useOwn&quot;: false
                      }
                  },
                  {
                      &quot;id&quot;: 23,
                      &quot;name&quot;: &quot;Natural&quot;,
                      &quot;productOptionItemValue&quot;: {
                          &quot;id&quot;: 1198,
                          &quot;stock&quot;: 1,
                          &quot;price&quot;: 1,
                          &quot;useOwn&quot;: false
                      }
                  }
              ]
          },
          {
              &quot;id&quot;: 2,
              &quot;name&quot;: &quot;Pack Size&quot;,
              &quot;productOptionItems&quot;: [
                  {
                      &quot;id&quot;: 3,
                      &quot;name&quot;: &quot;10&quot;,
                      &quot;productOptionItemValue&quot;: {
                          &quot;id&quot;: 1180,
                          &quot;stock&quot;: 100,
                          &quot;price&quot;: 10,
                          &quot;useOwn&quot;: true
                      }
                  },
                  {
                      &quot;id&quot;: 4,
                      &quot;name&quot;: &quot;100&quot;,
                      &quot;productOptionItemValue&quot;: {
                          &quot;id&quot;: 1181,
                          &quot;stock&quot;: 10,
                          &quot;price&quot;: 1000,
                          &quot;useOwn&quot;: true
                      }
                  }
              ]
          }
      ],
      &quot;productSpecifications&quot;: []
  }
  </code></pre>
  <p>Any help or ideas would be greatly appreciated!</p>