title: Symfony2 Doctrine Array of joined objects
body: <p>I have the following repository:</p>
  
  <pre><code>class CustomerRepository extends EntityRepository
  {
  public function searchCustomer($criteria)
      {
      $q = $this-&gt;createQueryBuilder('c');
          $q-&gt;join('TeamERPCustomerBundle:Company', 'o', 
                      'WITH', 'c.company = o.id');
      if (isset($criteria) and $criteria!=""){
          $q-&gt;orWhere(sprintf("c.customer_name like '%s'", '%'.$criteria.'%'));
      (...)
      }
          $q = $q-&gt;getQuery()-&gt;getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
          return $q;
      }
  }
  </code></pre>
  
  <p>I call it from the controller as follows:</p>
  
  <pre><code>            $result = $this-&gt;getDoctrine()-&gt;getManager()
                  -&gt;getRepository('TeamERPCustomerBundle:Customer')
                  -&gt;searchCustomer($customerInfo);
  </code></pre>
  
  <p>It returns an array of elements as follows:</p>
  
  <pre><code>array (size=20)
    0 =&gt; 
  array (size=9)
    'id' =&gt; int 1
    'customer_name' =&gt; string 'Abel' (length=4)
    'address' =&gt; string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
    'postal_address' =&gt; string '75100' (length=5)
    'city_town_village' =&gt; string 'Las Tunas' (length=9)
    'e_mail' =&gt; string 'abel@ltu.sld.cu' (length=15)
    'land_line' =&gt; string '346386' (length=6)
    'cell_phone' =&gt; null
    'fax' =&gt; null
    1 =&gt; 
  array (size=2)
    'id' =&gt; int 1
    'company_name' =&gt; string 'Debswana' (length=8)
    2 =&gt; 
  array (size=9)
    'id' =&gt; int 2
    'customer_name' =&gt; string 'Kay' (length=3)
    'address' =&gt; null
    'postal_address' =&gt; null
    'city_town_village' =&gt; null
    'e_mail' =&gt; null
    'land_line' =&gt; null
    'cell_phone' =&gt; null
    'fax' =&gt; null
    3 =&gt; 
  array (size=2)
    'id' =&gt; int 3
    'company_name' =&gt; string 'DTC' (length=3)
  (...)
  </code></pre>
  
  <p>It gives one element of the array per object, but what I want it the classic mysql join; as result an array of elements of the same type. Something like mergin elements 1 and 2 together. </p>
  
  <p>The other problem with this query is that is does not work like that classic join ether, it gives all the customers, but only the companies that are not repeated.</p>
  
  <p>Can anyone help me to get a uniform array of elements?</p>
  
  <p><strong>Edit: I managed to find a solution:</strong></p>
  
  <pre><code>        $em = $this-&gt;getEntityManager();
  
      $query = $em-&gt;createQuery('
          SELECT c, i
          FROM TeamERPCustomerBundle:Customer c
          JOIN c.company i');
  
      //$query-&gt;setParameter('id', '1'); With this is can add as many parameter as I need
  
      return $query-&gt;getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
  </code></pre>
  
  <p>This returned something like this:</p>
  
  <pre><code>array (size=11)
    0 =&gt; 
  array (size=10)
    'id' =&gt; int 1
    'customer_name' =&gt; string 'Abel' (length=4)
    'address' =&gt; string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
    'postal_address' =&gt; string '75100' (length=5)
    'city_town_village' =&gt; string 'Las Tunas' (length=9)
    'e_mail' =&gt; string 'abel@ltu.sld.cu' (length=15)
    'land_line' =&gt; string '346386' (length=6)
    'cell_phone' =&gt; null
    'fax' =&gt; null
    'company' =&gt; 
      array (size=2)
        'id' =&gt; int 1
        'company_name' =&gt; string 'Debswana' (length=8)
  </code></pre>
  
  <p>As you might have already appreciated this array is different from what I was getting before. so now I just have to go through the array and change it to what ever I want. for instance:</p>
  
  <pre><code>           foreach ($result as $key =&gt; $value){
                 $result[$key]['company'] = $value['company']['company_name'];
             }
  </code></pre>
  
  <p>Now we are talking. This Is kind of what I wanted:</p>
  
  <pre><code>array (size=11)
    0 =&gt; 
  array (size=10)
    'id' =&gt; int 1
    'customer_name' =&gt; string 'Abel' (length=4)
    'address' =&gt; string 'Calle 52 # 60, % 3era y G. Quezada, Nuevo Sosa' (length=46)
    'postal_address' =&gt; string '75100' (length=5)
    'city_town_village' =&gt; string 'Las Tunas' (length=9)
    'e_mail' =&gt; string 'abel@ltu.sld.cu' (length=15)
    'land_line' =&gt; string '346386' (length=6)
    'cell_phone' =&gt; null
    'fax' =&gt; null
    'company' =&gt; string 'Debswana' (length=8)
  </code></pre>