开发者

specify key values for return $query->fetchArray()?

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-pro开发者_运维问答ject.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.


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');
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜