Symfony DQL - 转换数组查询结果

时间:2015-02-12 16:54:12

标签: sql symfony dql

我试图转换查询结果的数组。

首先我做了这个查询:

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();
}

结果是这样的:

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' 

我想要的是这样的数组:

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

事实上,对于每个相同的'key'(此处为'FORMAT1''FORMAT2',按'locale'ENFR重新分组

是否可以在查询中执行此操作?

我尝试过GROUPBY和DISTINCT但没有发生任何事......

如果在查询中不可能,可以使用循环重新绘制数组....

谢谢你!

2 个答案:

答案 0 :(得分:1)

Doctrine提供了一种在创建QueryBuilder时指定indexBy的方法,以便不按顺序方式而是通过特定键索引生成的数组:

createQueryBuilder($alias, $indexBy = null)

所以你可能想尝试createQueryBuilder('c', 'c.key')之类的东西来使用c键作为数组索引

答案 1 :(得分:0)

我找到了一个带有foreach的解决方案来重绘数组...

$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;

结果:

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)