Use a column value of a Doctrine Entity as key of an array result

Code
<?php

use PHPSnippets\Db\Entity\Test;

// Index by uses a given value as result array key
// Be sure to use only primary or unique keys

// #### QueryBuilder ####

$repo $entityManager->getRepository(Test::CLASS);

// index by test.name
$qb $repo->createQueryBuilder('test''test.name');

$result $qb->setMaxResults(10)
    ->
getQuery()
    ->
getResult();

echo 
'QueryBuilder';
dump($result);

// #### DQL ####

$result $entityManager->createQuery(
    
'SELECT test FROM PHPSnippets\Db\Entity\Test test
    INDEX BY test.name'
    
)->setMaxResults(10)
    ->
getResult();

echo 
'DQL';
dump($result);
Result
QueryBuilder
array:3 [
"foo" => Test {#1500
+id: 1
+name: "foo"
+creationDate: DateTime @1405849049 {#1488
date: 2014-07-20 11:37:29.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#1497
-snapshot: []
-owner: Test {#1500}
-association: array:15 [ …15]
-em: EntityManager {#1437 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#1499 …}
-isDirty: false
#collection: ArrayCollection {#1503
-elements: []
}
#initialized: false
}
}
"test" => Test {#1507
+id: 2
+name: "test"
+creationDate: DateTime @1405845622 {#1511
date: 2014-07-20 10:40:22.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#1508
-snapshot: []
-owner: Test {#1507}
-association: array:15 [ …15]
-em: EntityManager {#1437 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#1499 …}
-isDirty: false
#collection: ArrayCollection {#1510
-elements: []
}
#initialized: false
}
}
"bar" => Test {#1509
+id: 3
+name: "bar"
+creationDate: DateTime @1406093238 {#1506
date: 2014-07-23 07:27:18.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#1513
-snapshot: []
-owner: Test {#1509}
-association: array:15 [ …15]
-em: EntityManager {#1437 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#1499 …}
-isDirty: false
#collection: ArrayCollection {#1514
-elements: []
}
#initialized: false
}
}
]

DQL
array:3 [
"foo" => Test {#1500
+id: 1
+name: "foo"
+creationDate: DateTime @1405849049 {#1488
date: 2014-07-20 11:37:29.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#1497
-snapshot: []
-owner: Test {#1500}
-association: array:15 [ …15]
-em: EntityManager {#1437 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#1499 …}
-isDirty: false
#collection: ArrayCollection {#1503
-elements: []
}
#initialized: false
}
}
"test" => Test {#1507
+id: 2
+name: "test"
+creationDate: DateTime @1405845622 {#1511
date: 2014-07-20 10:40:22.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#1508
-snapshot: []
-owner: Test {#1507}
-association: array:15 [ …15]
-em: EntityManager {#1437 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#1499 …}
-isDirty: false
#collection: ArrayCollection {#1510
-elements: []
}
#initialized: false
}
}
"bar" => Test {#1509
+id: 3
+name: "bar"
+creationDate: DateTime @1406093238 {#1506
date: 2014-07-23 07:27:18.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#1513
-snapshot: []
-owner: Test {#1509}
-association: array:15 [ …15]
-em: EntityManager {#1437 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#1499 …}
-isDirty: false
#collection: ArrayCollection {#1514
-elements: []
}
#initialized: false
}
}
]