I found a very useful Doctrine function to set an attribute on a table for getting the database IDs also as keys in the resulting Doctrine_Collection. This function is documented here: http://www.doctrine-project.org/projects/orm/1.2/docs/manual/component-overview/en#collection:key-mapping
Now the question. I cannot use the table object itself, because I need to create a dynamic query on the table (and not the magic finders as in the example).
I tried this code:
$doctrineTable = Doctrine_Core::getTable($table);
$doctrineTable->setAttribute(Doctrine_Core::ATTR_COLL_KEY, "id");
$q = $doctrineTable->createQuery("t");
foreach ($filter as $c => $v) // lopp thru coumns
if (is_array($v)) // use whereIn if value is an array
$q->andWhereIn("t." . $c, $v);
elseif (is_null($v)) // use is null for null values
$q->andWhere("t." . $c . " IS NULL");
else // use where in other cases
$q->andWhere("t." . $c . "=?", $v);
return $q->fetchAll();
Unfortunately the resulting collection is still not using an associative array, but a normal one just using keys from 0 up.
Anybody has an idea how to achieve that for a query on a single table?
Cheers, Daniel
You are looking for the INDEXBY keyword.
The INDEXBY keyword offers a way of mapping certain columns as collection / array keys. By default Doctrine indexes multiple elements to numerically indexed arrays / collections. The mapping starts from zero. In order to override this behavior you need to use INDEXBY keyword