11
11
12
12
namespace Dunglas \ApiBundle \Mapping \Loader ;
13
13
14
- use Doctrine \ORM \ EntityManager ;
14
+ use Doctrine \Common \ Persistence \ ManagerRegistry ;
15
15
use Dunglas \ApiBundle \Mapping \ClassMetadata ;
16
16
17
17
/**
18
18
* Doctrine identifier loader.
19
19
*
20
20
* @author Mikaël Labrut <[email protected] >
21
+ * @author Kévin Dunglas <[email protected] >
21
22
*/
22
23
class DoctrineIdentifierLoader implements LoaderInterface
23
24
{
24
25
/**
25
- * @var EntityManager
26
+ * @var ManagerRegistry
26
27
*/
27
- private $ entityManager ;
28
+ private $ managerRegistry ;
28
29
29
- public function __construct (EntityManager $ entityManager )
30
+ public function __construct (ManagerRegistry $ managerRegistry )
30
31
{
31
- $ this ->entityManager = $ entityManager ;
32
+ $ this ->managerRegistry = $ managerRegistry ;
32
33
}
33
34
34
35
/**
@@ -40,17 +41,29 @@ public function loadClassMetadata(
40
41
array $ denormalizationGroups = null ,
41
42
array $ validationGroups = null
42
43
) {
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
+
44
56
$ identifiers = $ doctrineClassMetaData ->getIdentifier ();
57
+ if (1 !== count ($ identifiers )) {
58
+ return true ;
59
+ }
45
60
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 );
48
65
49
- foreach ($ classMetadata ->getAttributes () as $ attribute ) {
50
- if ($ attribute ->getName () === $ identifierName ) {
51
- $ attribute ->setIdentifier (true );
52
- break ;
53
- }
66
+ return true ;
54
67
}
55
68
}
56
69
0 commit comments