indexby-keyword.txt 1.04 KB
Newer Older
zYne's avatar
zYne committed
1
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 behaviour you need to use INDEXBY keyword as shown above:
zYne's avatar
zYne committed
2 3 4

<code type="php">
$q = new Doctrine_Query();
zYne's avatar
zYne committed
5
$q->from('User u INDEXBY u.name');
zYne's avatar
zYne committed
6 7 8 9 10 11 12 13 14 15

$users = $q->execute();
</code>

Now the users in $users collection are accessible through their names.

<code type="php">
print $user['jack daniels']->id;
</code>

zYne's avatar
zYne committed
16
The INDEXBY keyword can be applied to any given JOIN. This means that any given component can have each own indexing behaviour. In the following we use distinct indexing for both Users and Groups.
zYne's avatar
zYne committed
17 18 19 20


<code type="php">
$q = new Doctrine_Query();
zYne's avatar
zYne committed
21
$q->from('User u INDEXBY u.name')->innerJoin('u.Group g INDEXBY g.name');
zYne's avatar
zYne committed
22 23 24 25 26

$users = $q->execute();
</code>

Now lets print out the drinkers club's creation date. 
zYne's avatar
zYne committed
27

zYne's avatar
zYne committed
28
<code type="php">
zYne's avatar
zYne committed
29 30
print $users['jack daniels']->Group['drinkers club']->createdAt;
</code>