如何在 Doctrine2 的查询结果中获取 Collection [英] How to get a Collection in Doctrine2's query results
问题描述
我正在尝试使用 dictionary2 执行查询并需要它返回一个集合对象.
I am trying to execute a query using doctrine2 and need it to return a collection object.
简化片段:
$players = $this->getEntityManager()
->createQueryBuilder()
->select('p')
->from('...Player', 'p')
->getQuery()
->getResult();
返回的对象是一个玩家数组.
The returned object is an array of Player.
查询结果格式 说:
结果是对象的普通集合(纯)或对象嵌套在结果行中的数组(混合).
The result is either a plain collection of objects (pure) or an array where the objects are nested in the result rows (mixed).
结果类型取决于什么以及如何获得集合对象?
On what does the result type depend and how can I achieve getting a collection object?
推荐答案
getResult()
总是返回一个数组.如果你想要一个集合,你必须将 getResult()
返回的数组传递给 Doctrine 的 ArrayCollection
The getResult()
always returns an array. If you want a collection, you must pass the array that is returned by getResult()
to Doctrine's ArrayCollection
例如
use DoctrineCommonCollectionsArrayCollection;
$result = $this
->getEntityManager()
->createQueryBuilder()
->select('p')
->from('...Player', 'p')
->getQuery()
->getResult()
;
$players = new ArrayCollection($result);
这篇关于如何在 Doctrine2 的查询结果中获取 Collection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!