44
55namespace Cycle \ORM \Service \Implementation ;
66
7- use Cycle \ORM \EntityProxyInterface ;
8- use Cycle \ORM \Exception \ORMException ;
97use Cycle \ORM \Heap \HeapInterface ;
108use Cycle \ORM \Heap \Node ;
9+ use Cycle \ORM \Reference \ReferenceInterface ;
1110use Cycle \ORM \Service \EntityFactoryInterface ;
1211use Cycle \ORM \Service \IndexProviderInterface ;
1312use Cycle \ORM \Service \MapperProviderInterface ;
1413use Cycle \ORM \Service \RelationProviderInterface ;
1514use Cycle \ORM \SchemaInterface ;
1615use Cycle \ORM \Select \LoaderInterface ;
16+ use Cycle \ORM \Service \RoleResolverInterface ;
1717
1818/**
1919 * @internal
@@ -26,6 +26,7 @@ public function __construct(
2626 private MapperProviderInterface $ mapperProvider ,
2727 private RelationProviderInterface $ relationProvider ,
2828 private IndexProviderInterface $ indexProvider ,
29+ private RoleResolverInterface $ roleResolver ,
2930 ) {}
3031
3132 public function make (
@@ -37,7 +38,7 @@ public function make(
3738 $ role = $ data [LoaderInterface::ROLE_KEY ] ?? $ role ;
3839 unset($ data [LoaderInterface::ROLE_KEY ]);
3940 // Resolved role
40- $ rRole = $ this ->resolveRole ($ role );
41+ $ rRole = $ this ->roleResolver -> resolveRole ($ role );
4142 $ relMap = $ this ->relationProvider ->getRelationMap ($ rRole );
4243 $ mapper = $ this ->mapperProvider ->getMapper ($ rRole );
4344
@@ -63,10 +64,25 @@ public function make(
6364 $ e = $ this ->heap ->find ($ rRole , $ ids );
6465
6566 if ($ e !== null ) {
67+ // Get not resolved relations (references)
68+ $ refs = \array_filter (
69+ $ mapper ->fetchRelations ($ e ),
70+ fn ($ v ) => $ v instanceof ReferenceInterface,
71+ );
72+
73+ if ($ refs === []) {
74+ return $ e ;
75+ }
76+
6677 $ node = $ this ->heap ->get ($ e );
6778 \assert ($ node !== null );
6879
69- return $ mapper ->hydrate ($ e , $ relMap ->init ($ this , $ node , $ castedData ));
80+ // Replace references with actual relation data
81+ return $ mapper ->hydrate ($ e , $ relMap ->init (
82+ $ this ,
83+ $ node ,
84+ \array_intersect_key ($ castedData , $ refs ),
85+ ));
7086 }
7187 }
7288 }
@@ -79,31 +95,4 @@ public function make(
7995
8096 return $ mapper ->hydrate ($ e , $ relMap ->init ($ this , $ node , $ castedData ));
8197 }
82-
83- public function resolveRole (object |string $ entity ): string
84- {
85- if (\is_object ($ entity )) {
86- $ node = $ this ->heap ->get ($ entity );
87- if ($ node !== null ) {
88- return $ node ->getRole ();
89- }
90-
91- $ class = $ entity ::class;
92- if (!$ this ->schema ->defines ($ class )) {
93- $ parentClass = \get_parent_class ($ entity );
94-
95- if ($ parentClass === false
96- || !$ entity instanceof EntityProxyInterface
97- || !$ this ->schema ->defines ($ parentClass )
98- ) {
99- throw new ORMException ("Unable to resolve role of ` $ class`. " );
100- }
101- $ class = $ parentClass ;
102- }
103-
104- $ entity = $ class ;
105- }
106-
107- return $ this ->schema ->resolveAlias ($ entity ) ?? throw new ORMException ("Unable to resolve role ` $ entity`. " );
108- }
10998}
0 commit comments