Fetch a very simple array as result in DOctrine 1.2

Skip to first unread message


Jan 18, 2010, 8:18:18 AM1/18/10
to doctrine-user
Hi there Doctrine team,

I've been struggling to get the following working: for the symfony
autocompleter widget one needs an array that contains the records
index as key and one string value as value. I've been trying the
following to achieve this:

1. add the INDEXBY keyword in the from() function
2. select only one column in the select() function
3. fetch as array with Doctrine::HYDRATE_SCALAR

So my query looks like this, in analogy with the Symfony Blogpost

$query = Doctrine_Query::create()
->select("a.name") // select only one
->from("Author a INDEXBY a.id") // set the index of the resultset
->fetchArray(array(), Doctrine::HYDRATE_SCALAR); // fetch as simple

Although this seems to be the correct way to achieve this, I'm still
getting an array as follows:
1. the id's are correctly set as index
2. the name column is selected but also the id column
3. the results are thus still arrays instead of simple string values

Array (
2 => Array (
'id' => 2,
'name' => 'foo'
5 => Array (
'id' => 5,
'name' => 'bar'

so I have to loop through this result set just to create the following

Array (
2 => 'foo',
5 => 'bar'

This seems just to stupid to me to have to loop through the result to
create this simple array. I can't imagine Doctrine doesn't have some
kind of way to achieve this straight from the query. I mean, this
seems like a very common thing. As I said, I've been searching in the
documentation, the API and the code but I can't seem to find a working

Does anyone has had any similar issue, can someone elaborate on this?

Thanks a lot in advance and keep up the great work of making Doctrine
the #1 PHP ORM!

Kind regards,



Jan 18, 2010, 8:21:57 AM1/18/10
to doctrine-user


Jan 18, 2010, 8:33:50 AM1/18/10
to doctrine-user
Hi there Doctrine Team,

The post mentioned above is terribly old..
So for Doctrine 1.2, is there still no way to achieve this?

Thx a lot in advance!


On Jan 18, 2:21 pm, 4levels <4lev...@gmail.com> wrote:
> Hi there Doctrine Team,
> I just found a similar post in this very same user group:

> http://groups.google.com/group/doctrine-user/browse_thread/thread/5b1...

Pete Hatton

Jan 18, 2010, 8:56:08 AM1/18/10
to doctri...@googlegroups.com
Sounds like you are looking for toKeyValueArray() - That feature looks
like it's been there since 1.1




Jan 18, 2010, 11:27:04 AM1/18/10
to doctrine-user
Hi Pete,

this is exactly what I was looking for, works like a charm.
Just another trivial related question: do you know if specifying the
select columns and the INDEXBY keyword speeds things up?

Thx a lot for pointing me in the right direction and the links to the
Doctrine docs..

Keep up the good works!


On Jan 18, 2:56 pm, "Pete Hatton" <p...@monolight.org> wrote:
> Sounds like you are looking for toKeyValueArray() - That feature looks  
> like it's been there since 1.1

> http://www.doctrine-project.org/Doctrine_Collection/1_2#method_tokeyv...http://www.doctrine-project.org/upgrade/1_1#Doctrine%20Collection
> P.

Pete Hatton

Jan 18, 2010, 7:07:49 PM1/18/10
to doctri...@googlegroups.com
Short answer, not sure as I've not used keyword myself.

If I understand the documentation correctly
ry-language:indexby-keyword I doubt it makes a difference. (I'll leave it
the right people to explain that one.)

However if you are using toKeyValueArray, having INDEXBY looks totally
pointless, since the toKeyValueArray is generating the index keys for the
array you need.


P.S. Just in case you are thinking I'm a Doctrine dev, I'm not. However
Doctrine is a great library!

Reply all
Reply to author
0 new messages