A strange Symfony Doctrine issue got me the other day. I guess it’s a lack of clear understanding of how Doctrine hydrates database query results when using getArrayResult() instead of getArray(). It seems that getArrayResult() returns just the first record from your query, no matter how many records the query actually returns but getArray() returns an multi-dimensional array containing all results from the same query.
So, I needed a list of all records to display on screen, not just the first one. All I did was change getArrayResult() to getArray() and this fixed my problem.
But why?!! Can anyone please provide the answer?! It’s not on Doctrine’s website, it’s not on stack. I’ve Googled it I’ve Binged it, if I had nothing better to do maybe I’ll Duckduckgo it. I’m afraid when I find the answer it will be so obvious that’s why I don’t get it. I’ve read some users say it depends on what you are selecting in the query, if it’s the record index or some other value or combination of values but have not found any documentation explaining the differences clearly. Doctrine’s horribly convoluted documentation says this about getArrayResults():
Query#getArrayResult()
: Retrieves an array graph (a nested array) that is largely interchangeable with the object graph generated byQuery#getResult()
for read-only purposes.
There’s money to be made — make an ORM that’s straightforward and intuitive.