40

I am trying to execute a query using doctrine2 and need it to return a collection object.

Simplified snippet:

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

The returned object is an array of Player.

The information on query result formats says:

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?

Anthony
  • 2,014
  • 2
  • 19
  • 29
mkraemer
  • 658
  • 1
  • 7
  • 9

3 Answers3

77

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

e.g.

use Doctrine\Common\Collections\ArrayCollection;

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

$players = new ArrayCollection($result);
DuDa
  • 3,718
  • 4
  • 16
  • 36
chriswoodford
  • 1,223
  • 9
  • 8
1

Since you're selecting only 'p' objects (and no individual column or aggregate values) and you're using getResult() for the hydration mode, your query should be returning pure objects rather than an array.

My best guess is that the problem has to do with the shorthand syntax you're using to build the query. The first thing I'd try is writing out the query "long form" like this:

$em = $this->getEntityManager();
$qb = $em->createQueryBuilder();

$qb->select('p')
   ->from('...\Player', 'p');

$query = $qb->getQuery();
$players = $query->getResult();

I would have guessed that your shorthand approach would be fine, but I've found Doctrine to be kind of finicky when it comes to method chaining.

There are a couple other things to consider depending on how simplified your snippet is. But in my experience, the query written as shown will return Player objects.

cantera
  • 24,479
  • 25
  • 95
  • 138
  • By the way, you might also consider switching to the 2.1 version of the manual. There were a couple of significant new features added to 2.1 (such as support for foreign keys as primary keys) that might lead to confusion if you're using an old version of the documentation. – cantera Nov 19 '11 at 21:54
  • I tried using less method chaining without success. My original query is more complicated (if it was not, i would use the findAll() method of my playerRepository - which interestingly returns a collection), but the problem exists already with the query given in my first post. – mkraemer Nov 20 '11 at 12:00
  • The query as shown will produce objects. If your query does not, then there is another issue with your code or datastore that cannot be solved given only the information you provided. – cantera Nov 20 '11 at 14:10
-1

to return an object instead of array, you have to use "Partial Objects".

here is tested code example https://stackoverflow.com/a/12044461/1178870

Community
  • 1
  • 1
ismaail E.G.
  • 419
  • 5
  • 16
  • This just returns every entity as a general object (instead of the entity class). OP wanted a collection (implementcing Collection interface from doctrine) of entities instead of an array of entities. – Matěj Koubík Jan 29 '21 at 12:20