本文介绍了如何在Doctrine2的查询结果中获取集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用doctrine2执行查询,并需要它返回集合对象。

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.

关于说:

结果类型取决于什么,如何实现

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

eg

use Doctrine\Common\Collections;

$result = $this->getEntityManager()
    ->createQueryBuilder()
    ->select('p')
    ->from('...\Player', 'p')
    ->getQuery()
    ->getResult();

$players = new Collections\ArrayCollection($result);

这篇关于如何在Doctrine2的查询结果中获取集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 04:48