Search code examples
sqlsymfonydql

Symfony DQL - transform the array query result


I'm trying to transform the array of my query result.

First I did this query:

public function findAllTraduction()
{
    return $this->createQueryBuilder('c')
        ->select('c.key, c.content, c.id, f.locale')
        ->leftJoin('c.TraductionFile', 'f')
        ->groupBy('c.key')
        ->getQuery()
        ->getArrayResult();
}

The result is like that:

array 
  0 => 
    array 
      'key' => string 'FORMAT1' 
      'content' => string 'login'
      'id' => int 507
      'locale' => string 'en'
  1 => 
    array 
      'key' => string 'FORMAT1'
      'content' => string 'connecter' 
      'id' => int 508
      'locale' => string 'fr' 
  2 => 
    array
      'key' => string 'FORMAT2' 
      'content' => string 'password' 
      'id' => int 503
      'locale' => string 'en' 
  3 => 
    array 
      'key' => string 'FORMAT2' 
      'content' => string 'mot de passe'
      'id' => int 504
      'locale' => string 'fr' 

What I would like to have is an array like that:

array
  'FORMAT1' => 
    array
      'en' =>
        array
          'content' => string 'login'
          'id' => int 507
      'fr' =>
        array
          'content' => string 'connecter'
          'id' => int 508
  'FORMAT2' => 
    array
      'en' =>
        array
          'content' => string 'password'
          'id' => int 503
      'fr' =>
        array
          'content' => string 'mot de passe'
          'id' => int 504

In fact, for each same 'key' (here 'FORMAT1' and 'FORMAT2', regroup by 'locale' (EN and FR)

Is it possible to do that in the query ?

I tried with GROUPBY and DISTINCT but nothing happened...

If not possible in query, may be redraw an array with loop....

thank you !


Solution

  • I found a solution with foreach for redraw the array ...

    $catalogue = [];
    
            foreach ($catalogues_array as $index => $val) {
                $key = $val['key'];
                $locale = $val['locale'];
                unset($catalogues_array[$index]['key']);
                unset($catalogues_array[$index]['locale']);
                $catalogue[$key][$locale] = $catalogues_array[$index];
            }
            return $catalogue;
    

    the result :

    array (size=421)
      'base_template.page_title' => 
        array (size=2)
          'en' => 
            array (size=6)
              'content' => string 'Audio Video Caption - Subtitling & Transcription of media.' (length=58)
              'description' => null
              'traductionControle' => boolean false
              'id' => int 1
              'domain' => string 'messages' (length=8)
              'bundleName' => string 'app' (length=3)
          'fr' => 
            array (size=6)
              'content' => string 'Audio Video Caption - Sous-Titrage & Transcription de médias.' (length=62)
              'description' => null
              'traductionControle' => boolean false
              'id' => int 9
              'domain' => string 'messages' (length=8)
              'bundleName' => string 'app' (length=3)
      'base_template.meta.keywords' => 
        array (size=2)
          'en' => 
            array (size=6)
              'content' => string 'closed captioning, subtitle, speech recognition, transcription, traduction, video, audio' (length=88)
              'description' => null
              'traductionControle' => boolean false
              'id' => int 2
              'domain' => string 'messages' (length=8)
              'bundleName' => string 'app' (length=3)
          'fr' => 
            array (size=6)
              'content' => string 'sous-titre, reconnaissance vocale, transcription, traduction, video, audio' (length=74)
              'description' => null
              'traductionControle' => boolean false
              'id' => int 10
              'domain' => string 'messages' (length=8)
              'bundleName' => string 'app' (length=3)