1
0
mirror of synced 2024-12-17 16:38:43 +03:00
doctrine2/manual/new/docs/en/dql-doctrine-query-language/indexby-keyword.txt

31 lines
1.0 KiB
Plaintext
Raw Normal View History

2007-09-02 15:35:51 +04:00
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:
2007-09-02 02:35:43 +04:00
<code type="php">
$q = new Doctrine_Query();
2007-09-02 15:35:51 +04:00
$q->from('User u INDEXBY u.name');
2007-09-02 02:35:43 +04:00
$users = $q->execute();
</code>
Now the users in $users collection are accessible through their names.
<code type="php">
print $user['jack daniels']->id;
</code>
2007-09-02 15:35:51 +04:00
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.
2007-09-02 02:35:43 +04:00
<code type="php">
$q = new Doctrine_Query();
2007-09-02 15:35:51 +04:00
$q->from('User u INDEXBY u.name')->innerJoin('u.Group g INDEXBY g.name');
2007-09-02 02:35:43 +04:00
$users = $q->execute();
</code>
Now lets print out the drinkers club's creation date.
2007-09-02 02:43:40 +04:00
2007-09-02 02:45:09 +04:00
<code type="php">
2007-09-02 02:35:43 +04:00
print $users['jack daniels']->Group['drinkers club']->createdAt;
</code>