1

Using Doctrine, I'm doing a query kinda like this:

$query = Doctrine_Query::create()->select('id,name')->from('people');
return $query->fetchArray();

It returns an object similar to the following array of arrays:

array(3) {
  [0]=>
  array(4) {
    ["id"]=>
    string(1) "34"
    ["name"]=>
    string(7) "John"
  }
  [1]=>
  array(4) {
    ["id"]=>
    string(1) "156"
    ["name"]=>
    string(6) "Bob"
  }
  [2]=>
  array(4) {
    ["id"]=>
    string(1) "27"
    ["name"]=>
    string(7) "Alex"
  }
}

Notice how the array's keys count up from 0, 1, 2, etc? My question: Is it possible to specify where you want the key values to come from? For example, I would like the key values in the above case to be 34, 156 and then 27.

I noticed in the Doctrine doc's that both fetchArray() has parameters that you can feed them, but it doesn't say anything about what those parameters are...

public array fetchArray(string params)

http://www.doctrine-project.org/api/orm/1.2/doctrine/doctrine_query.html#fetchArray()

Also, I can obviously just repopulate a new array with the prefered key/value combinations, but I'm trying to avoid the extra processing required.

1 Answer 1

2

Check out the INDEXBY DQL keyword.

Try something like:

<?php
$query = Doctrine_Query::create()
           ->select('p.id,p.name')
           ->from('people p INDEXBY p.id');
Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick. Thanks! Also I found the Doctrine_Collection->setColumnKey() method but it did not seem to do anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.