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" => PHPSnippets\Db\Entity\Test {#1429
+id: 1
+name: "foo"
+creationDate: DateTime @1405849049 {#1427
date: 2014-07-20 11:37:29.0 Europe/Berlin (+02:00)
}
+subTests: Doctrine\ORM\PersistentCollection {#1436
-snapshot: []
-owner: PHPSnippets\Db\Entity\Test {#1429}
-association: array:15 [ …15]
-em: Doctrine\ORM\EntityManager {#1322 …11}
-backRefFieldName: "test"
-typeClass: Doctrine\ORM\Mapping\ClassMetadata {#1432 …}
-isDirty: false
#collection: Doctrine\Common\Collections\ArrayCollection {#1440
-elements: []
}
#initialized: false
}
}
"test" => PHPSnippets\Db\Entity\Test {#1442
+id: 2
+name: "test"
+creationDate: DateTime @1405845622 {#1441
date: 2014-07-20 10:40:22.0 Europe/Berlin (+02:00)
}
+subTests: Doctrine\ORM\PersistentCollection {#1443
-snapshot: []
-owner: PHPSnippets\Db\Entity\Test {#1442}
-association: array:15 [ …15]
-em: Doctrine\ORM\EntityManager {#1322 …11}
-backRefFieldName: "test"
-typeClass: Doctrine\ORM\Mapping\ClassMetadata {#1432 …}
-isDirty: false
#collection: Doctrine\Common\Collections\ArrayCollection {#1444
-elements: []
}
#initialized: false
}
}
"bar" => PHPSnippets\Db\Entity\Test {#1446
+id: 3
+name: "bar"
+creationDate: DateTime @1406093238 {#1445
date: 2014-07-23 07:27:18.0 Europe/Berlin (+02:00)
}
+subTests: Doctrine\ORM\PersistentCollection {#1447
-snapshot: []
-owner: PHPSnippets\Db\Entity\Test {#1446}
-association: array:15 [ …15]
-em: Doctrine\ORM\EntityManager {#1322 …11}
-backRefFieldName: "test"
-typeClass: Doctrine\ORM\Mapping\ClassMetadata {#1432 …}
-isDirty: false
#collection: Doctrine\Common\Collections\ArrayCollection {#1448
-elements: []
}
#initialized: false
}
}
]

DQL
^ array:3 [
"foo" => PHPSnippets\Db\Entity\Test {#1429
+id: 1
+name: "foo"
+creationDate: DateTime @1405849049 {#1427
date: 2014-07-20 11:37:29.0 Europe/Berlin (+02:00)
}
+subTests: Doctrine\ORM\PersistentCollection {#1436
-snapshot: []
-owner: PHPSnippets\Db\Entity\Test {#1429}
-association: array:15 [ …15]
-em: Doctrine\ORM\EntityManager {#1322 …11}
-backRefFieldName: "test"
-typeClass: Doctrine\ORM\Mapping\ClassMetadata {#1432 …}
-isDirty: false
#collection: Doctrine\Common\Collections\ArrayCollection {#1440
-elements: []
}
#initialized: false
}
}
"test" => PHPSnippets\Db\Entity\Test {#1442
+id: 2
+name: "test"
+creationDate: DateTime @1405845622 {#1441
date: 2014-07-20 10:40:22.0 Europe/Berlin (+02:00)
}
+subTests: Doctrine\ORM\PersistentCollection {#1443
-snapshot: []
-owner: PHPSnippets\Db\Entity\Test {#1442}
-association: array:15 [ …15]
-em: Doctrine\ORM\EntityManager {#1322 …11}
-backRefFieldName: "test"
-typeClass: Doctrine\ORM\Mapping\ClassMetadata {#1432 …}
-isDirty: false
#collection: Doctrine\Common\Collections\ArrayCollection {#1444
-elements: []
}
#initialized: false
}
}
"bar" => PHPSnippets\Db\Entity\Test {#1446
+id: 3
+name: "bar"
+creationDate: DateTime @1406093238 {#1445
date: 2014-07-23 07:27:18.0 Europe/Berlin (+02:00)
}
+subTests: Doctrine\ORM\PersistentCollection {#1447
-snapshot: []
-owner: PHPSnippets\Db\Entity\Test {#1446}
-association: array:15 [ …15]
-em: Doctrine\ORM\EntityManager {#1322 …11}
-backRefFieldName: "test"
-typeClass: Doctrine\ORM\Mapping\ClassMetadata {#1432 …}
-isDirty: false
#collection: Doctrine\Common\Collections\ArrayCollection {#1448
-elements: []
}
#initialized: false
}
}
]
Used Versions
PHP 8.0, Laminas MVC 3.2, Symfony 5.2, Laravel 8.28, PHPUnit 9.5, Doctrine ORM 2.8