|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\ORM\Utility; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
8
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
9
|
|
|
use Doctrine\ORM\Mapping\FieldMetadata; |
|
10
|
|
|
use Doctrine\ORM\Mapping\ToOneAssociationMetadata; |
|
11
|
|
|
|
|
12
|
|
|
final class NormalizeIdentifier |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* Given a flat identifier, this method will produce another flat identifier, but with all |
|
16
|
|
|
* association fields that are mapped as identifiers replaced by entity references, recursively. |
|
17
|
|
|
* |
|
18
|
|
|
* @throws \Doctrine\ORM\ORMException |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __invoke( |
|
21
|
|
|
EntityManagerInterface $entityManager, |
|
22
|
|
|
ClassMetadata $targetClass, |
|
23
|
|
|
array $flatIdentifier |
|
24
|
|
|
) : array { |
|
25
|
|
|
$normalizedAssociatedId = []; |
|
26
|
|
|
|
|
27
|
|
|
foreach ($targetClass->getDeclaredPropertiesIterator() as $name => $declaredProperty) { |
|
28
|
|
|
if (! \array_key_exists($name, $flatIdentifier)) { |
|
29
|
|
|
continue; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if ($declaredProperty instanceof FieldMetadata) { |
|
33
|
|
|
$normalizedAssociatedId[$name] = $flatIdentifier[$name]; |
|
34
|
|
|
|
|
35
|
|
|
continue; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
if ($declaredProperty instanceof ToOneAssociationMetadata) { |
|
39
|
|
|
$targetIdMetadata = $entityManager->getClassMetadata($declaredProperty->getTargetEntity()); |
|
40
|
|
|
|
|
41
|
|
|
// Note: the ORM prevents using an entity with a composite identifier as an identifier association |
|
42
|
|
|
// therefore, reset($targetIdMetadata->identifier) is always correct |
|
43
|
|
|
$normalizedAssociatedId[$name] = $entityManager->getReference( |
|
44
|
|
|
$targetIdMetadata->getClassName(), |
|
|
|
|
|
|
45
|
|
|
$this->__invoke( |
|
46
|
|
|
$entityManager, |
|
47
|
|
|
$targetIdMetadata, |
|
|
|
|
|
|
48
|
|
|
[reset($targetIdMetadata->identifier) => $flatIdentifier[$name]] |
|
|
|
|
|
|
49
|
|
|
) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $normalizedAssociatedId; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.