Skip to content

Commit e3326a6

Browse files
committed
Fix quality issues in DoctrineIdentifierLoader
1 parent 0ffce2b commit e3326a6

File tree

3 files changed

+28
-15
lines changed

3 files changed

+28
-15
lines changed

Mapping/Loader/AttributesLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function loadClassMetadata(
166166
*
167167
* @param ClassMetadata $classMetadata
168168
* @param string $attributeName
169-
* @param string[]|null $normalizationGroups
169+
* @param array|null $normalizationGroups
170170
*
171171
* @return AttributeMetadata
172172
*/

Mapping/Loader/DoctrineIdentifierLoader.php

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,25 @@
1111

1212
namespace Dunglas\ApiBundle\Mapping\Loader;
1313

14-
use Doctrine\ORM\EntityManager;
14+
use Doctrine\Common\Persistence\ManagerRegistry;
1515
use Dunglas\ApiBundle\Mapping\ClassMetadata;
1616

1717
/**
1818
* Doctrine identifier loader.
1919
*
2020
* @author Mikaël Labrut <[email protected]>
21+
* @author Kévin Dunglas <[email protected]>
2122
*/
2223
class DoctrineIdentifierLoader implements LoaderInterface
2324
{
2425
/**
25-
* @var EntityManager
26+
* @var ManagerRegistry
2627
*/
27-
private $entityManager;
28+
private $managerRegistry;
2829

29-
public function __construct(EntityManager $entityManager)
30+
public function __construct(ManagerRegistry $managerRegistry)
3031
{
31-
$this->entityManager = $entityManager;
32+
$this->managerRegistry = $managerRegistry;
3233
}
3334

3435
/**
@@ -40,17 +41,29 @@ public function loadClassMetadata(
4041
array $denormalizationGroups = null,
4142
array $validationGroups = null
4243
) {
43-
$doctrineClassMetaData = $this->entityManager->getClassMetadata($classMetadata->getReflectionClass()->getName());
44+
$className = $classMetadata->getReflectionClass()->name;
45+
46+
$manager = $this->managerRegistry->getManagerForClass($className);
47+
if (!$manager) {
48+
return true;
49+
}
50+
51+
$doctrineClassMetaData = $manager->getClassMetadata($className);
52+
if (!$doctrineClassMetaData) {
53+
return true;
54+
}
55+
4456
$identifiers = $doctrineClassMetaData->getIdentifier();
57+
if (1 !== count($identifiers)) {
58+
return true;
59+
}
4560

46-
if (1 === count($identifiers)) {
47-
$identifierName = $identifiers[0];
61+
$identifierName = $identifiers[0];
62+
foreach ($classMetadata->getAttributes() as $attribute) {
63+
if ($attribute->getName() === $identifierName) {
64+
$attribute->setIdentifier(true);
4865

49-
foreach ($classMetadata->getAttributes() as $attribute) {
50-
if ($attribute->getName() === $identifierName) {
51-
$attribute->setIdentifier(true);
52-
break;
53-
}
66+
return true;
5467
}
5568
}
5669

Resources/config/doctrine_orm.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
</service>
4646

4747
<service id="api.mapping.loaders.doctrine_identifier" class="Dunglas\ApiBundle\Mapping\Loader\DoctrineIdentifierLoader" public="false">
48-
<argument type="service" id="doctrine.orm.default_entity_manager" />
48+
<argument type="service" id="doctrine" />
4949
</service>
5050
</services>
5151

0 commit comments

Comments
 (0)