Symfony DQL - 转换数组查询结果
Symfony DQL - transform the array query result
我正在尝试转换查询结果的数组。
首先我做了这个查询:
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'
(EN
和 FR
)
是否可以在查询中这样做?
我尝试使用 GROUPBY 和 DISTINCT,但没有任何反应...
如果查询不到,可能会循环重绘数组....
谢谢!
我找到了使用 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)
Doctrine 提供了一种在创建 QueryBuilder 时指定 indexBy 的方法,以便不是按顺序而是按特定键索引结果数组:
createQueryBuilder($alias, $indexBy = null)
所以你可能想尝试 createQueryBuilder('c', 'c.key')
来使用 c 键作为数组索引
我正在尝试转换查询结果的数组。
首先我做了这个查询:
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'
(EN
和 FR
)
是否可以在查询中这样做?
我尝试使用 GROUPBY 和 DISTINCT,但没有任何反应...
如果查询不到,可能会循环重绘数组....
谢谢!
我找到了使用 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)
Doctrine 提供了一种在创建 QueryBuilder 时指定 indexBy 的方法,以便不是按顺序而是按特定键索引结果数组:
createQueryBuilder($alias, $indexBy = null)
所以你可能想尝试 createQueryBuilder('c', 'c.key')
来使用 c 键作为数组索引