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 {#389
+id: 1
+name: "foo"
+creationDate: DateTime @1405849049 {#393
date: 2014-07-20 11:37:29.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#400
-snapshot: []
-owner: Test {#389}
-association: array:15 [ …15]
-em: EntityManager {#342 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#403 …}
-isDirty: false
#collection: ArrayCollection {#409
-elements: []
}
#initialized: false
}
}
"test" => Test {#413
+id: 2
+name: "test"
+creationDate: DateTime @1405845622 {#414
date: 2014-07-20 10:40:22.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#416
-snapshot: []
-owner: Test {#413}
-association: array:15 [ …15]
-em: EntityManager {#342 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#403 …}
-isDirty: false
#collection: ArrayCollection {#417
-elements: []
}
#initialized: false
}
}
"bar" => Test {#415
+id: 3
+name: "bar"
+creationDate: DateTime @1406093238 {#412
date: 2014-07-23 07:27:18.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#419
-snapshot: []
-owner: Test {#415}
-association: array:15 [ …15]
-em: EntityManager {#342 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#403 …}
-isDirty: false
#collection: ArrayCollection {#420
-elements: []
}
#initialized: false
}
}
]

DQL
array:3 [
"foo" => Test {#389
+id: 1
+name: "foo"
+creationDate: DateTime @1405849049 {#393
date: 2014-07-20 11:37:29.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#400
-snapshot: []
-owner: Test {#389}
-association: array:15 [ …15]
-em: EntityManager {#342 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#403 …}
-isDirty: false
#collection: ArrayCollection {#409
-elements: []
}
#initialized: false
}
}
"test" => Test {#413
+id: 2
+name: "test"
+creationDate: DateTime @1405845622 {#414
date: 2014-07-20 10:40:22.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#416
-snapshot: []
-owner: Test {#413}
-association: array:15 [ …15]
-em: EntityManager {#342 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#403 …}
-isDirty: false
#collection: ArrayCollection {#417
-elements: []
}
#initialized: false
}
}
"bar" => Test {#415
+id: 3
+name: "bar"
+creationDate: DateTime @1406093238 {#412
date: 2014-07-23 07:27:18.0 Europe/Berlin (+02:00)
}
+subTests: PersistentCollection {#419
-snapshot: []
-owner: Test {#415}
-association: array:15 [ …15]
-em: EntityManager {#342 …11}
-backRefFieldName: "test"
-typeClass: ClassMetadata {#403 …}
-isDirty: false
#collection: ArrayCollection {#420
-elements: []
}
#initialized: false
}
}
]
Used Versions
PHP 7.2, Zend 3.0, Symfony 4.0, Laravel 5.6, PHPUnit 6.5, Doctrine 2.8