gpt4 book ai didi

php - 更改主键值的 getResult 数组键

转载 作者:IT王子 更新时间:2023-10-28 23:59:56 26 4
gpt4 key购买 nike

是否可以更改 Doctrine2 中 getResult() 的数组键值?

例子:


$qb->select('t.id, t.name')->from('Table', 't');

当我打印这个时,我得到了,这不是我想要的:

print_r($qb->getQuery()->getResult()); <br/><br/>
//Print result: Array ( [0] => Array ( [id] => 20 [name] => Name1 ) [1] => Array ( [id] => 21 [percentagem] => Name2 ) )

我想要的是:


Array ( [20] => Array ( [id] => 20 [name] => Name1 ) [21] => Array ( [id] => 21 [percentagem] => Name2 ) )

建议、提示将不胜感激。

最佳答案

我真的很高兴这东西有多酷:

$query = $this->getEntityManager()->createQuery('
SELECT user FROM UserBundle:User user
INDEX BY user.id
WHERE user.id = 1
'
);

The INDEX BY construct is nothing that directly translates into SQL but that affects object and array hydration. After each FROM and JOIN clause you specify by which field this class should be indexed in the result. By default a result is incremented by numerical keys starting with 0. However with INDEX BY you can specify any other column to be the key of your result, it really only makes sense with primary or unique fields though.

来源:Doctrine ORM 2 Documentation Using INDEX BY

  • 请在 WHERE 之前使用 INDEX BY

关于php - 更改主键值的 getResult 数组键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11327798/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com