php 原则 2:查询结果作为关联数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14002888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Doctrine 2: Query result as associative array
提问by Yeroon
In my Repository class I use the following code to query:
在我的 Repository 类中,我使用以下代码进行查询:
$query = $this->getEntityManager()->createQuery("
SELECT s.term, COUNT(s.term) AS freq
FROM App\Entities\SearchTerm s
GROUP BY s.term
ORDER BY s.term ASC
");
$result = $query->getResult();
The result I get is something like:
我得到的结果是这样的:
array (size=4)
0 =>
array (size=2)
'term' => string '' (length=0)
'freq' => string '1' (length=1)
1 =>
array (size=2)
'term' => string 'foo' (length=3)
'freq' => string '1' (length=1)
2 =>
array (size=2)
'term' => string 'bar' (length=3)
'freq' => string '2' (length=1)
3 =>
array (size=2)
'term' => string 'baz' (length=3)
'freq' => string '2' (length=1)
But I would rather have an associative array as a result:
但我宁愿有一个关联数组作为结果:
array (size=4)
'' => string '1' (length=1)
'foo' => string '1' (length=1)
'bar' => string '2' (length=1)
'baz' => string '2' (length=1)
Is this possible without an extra for-loopto build the desired array?
如果没有额外的 for 循环来构建所需的数组,这可能吗?
回答by Axidepuy
I know its old but today I had to do almost the same, my solution without a custom hydrator
我知道它很旧,但今天我不得不做几乎相同的事情,我的解决方案没有定制的水合器
- INDEX BY s.term
- modify the getResult() to be sure
- 按 s.term 索引
- 修改 getResult() 以确保
like
喜欢
$result = $query->getQuery()->getResult(\Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY);
- format the result
- 格式化结果
as
作为
$resultNeeded = array_map(function($value) { return $value['freq']; }, $result);
回答by Wilt
If you want an array you can use the getArrayResultmethod.
如果你想要一个数组,你可以使用的getArrayResult方法。
Gets the array of results for the query.
Alias for execute(null, HYDRATE_ARRAY).
获取查询的结果数组。
execute(null, HYDRATE_ARRAY) 的别名。
$result = $query->getQuery()->getArrayResult();
So using this method will give you the exact same result as using the constant HYDRATE_ARRAY.
因此,使用此方法将为您提供与使用常量完全相同的结果HYDRATE_ARRAY。
回答by Guillaume
Use INDEX BY:
使用INDEX BY:
$em = $this->getEntityManager();
$query = $em->createQuery('SELECT c FROM SomeBundle:Configuration c INDEX BY c.name');
$query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
I saw it here and it works fine : https://coderwall.com/p/crz4dq/defining-a-column-to-be-the-key-of-the-result-hydrated-as-array-in-doctrine-2
我在这里看到它并且它工作正常:https: //coderwall.com/p/crz4dq/defining-a-column-to-be-the-key-of-the-result-hydred-as-array-in-doctrine -2
回答by hakre
Actually somewherethe transposition has to be done. See Hydration Modesabout what is returned by ->getResult()and which alternatives modes/variants already exist.
实际上,必须在某处进行换位。有关返回的内容以及已存在的替代模式/变体,请参阅水合模式->getResult()。
You can also add your own hydration mode at a central place. That is explained in Custom Hydration Modes.
您还可以在中心位置添加自己的补水模式。这在自定义水合模式中进行了解释。
回答by Ashfaq Muhammad
After lot of search I have found some solutions for doctrine object to array conversion. When we have associated objects in result.
经过大量搜索后,我找到了一些解决doctio object to array conversion 的解决方案。当我们在结果中有关联的对象时。
1.
1.
$person = $em->find('Person', 2);
$da = array();
$person = (array) $person;
foreach($person as $i=>$d) {
if (is_object($d)) {
$d = (array) $d;
foreach($d as $si=>$sd){
if (is_object($sd)) {
//var_dump('after convert array');
$da[$i][$si] = (array) $sd ;
//var_dump($da[$i][$si]);
} else {
$da[$i][$si] = $sd ;
//var_dump($da[$i][$si] );
}
}
} else {
$da[$i] = $d;
//var_dump($da[$i]);
}
}
echo '<pre>'; print_r($da); echo '<pre>'; exit;
2.
2.
$query = $em->createQuery('SELECT w FROM Person w WHERE w.Id = 2');
$person = $query->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
echo '<pre>'; \Doctrine\Common\Util\Debug::dump($person); exit;
3.
3.
$result = $em->createQueryBuilder();
$person = $result->select('p')
->from('PsnPersonPsn', 'p')
->where('p.Id= 1')
->getQuery()
->getResult(\Doctrine\ORM\Query::HYDRATE_ARRAY);
echo '<pre>'; \Doctrine\Common\Util\Debug::dump($person); exit;

