77use Cycle \Annotated \Annotation \Inheritance ;
88use Cycle \Annotated \Exception \AnnotationException ;
99use Cycle \Annotated \Utils \EntityUtils ;
10+ use Cycle \Schema \Definition \Entity ;
1011use Cycle \Schema \Definition \Entity as EntitySchema ;
1112use Cycle \Schema \Definition \Inheritance \JoinedTable as JoinedTableInheritanceSchema ;
1213use Cycle \Schema \Definition \Inheritance \SingleTable as SingleTableInheritanceSchema ;
@@ -57,8 +58,8 @@ public function run(Registry $registry): Registry
5758 $ childClass = $ parent ->getClass ();
5859 } while ($ this ->parseMetadata ($ parent , Inheritance::class) !== null );
5960
60- if ($ entity = $ this ->initInheritance ($ annotation , $ child , $ parent )) {
61- $ found [] = $ entity ;
61+ if ($ inheritanceEntity = $ this ->initInheritance ($ annotation , $ child , $ parent )) {
62+ $ found [] = $ inheritanceEntity ;
6263 }
6364 }
6465
@@ -67,17 +68,10 @@ public function run(Registry $registry): Registry
6768 if (!$ registry ->hasEntity ($ child ->getRole ())) {
6869 $ registry ->register ($ child );
6970
70- $ database = $ child ->getDatabase ();
71- $ tableName = $ child ->getTableName ();
72- if ($ entity ->getInheritance () instanceof SingleTableInheritanceSchema) {
73- $ database = $ parent ->getDatabase ();
74- $ tableName = $ parent ->getTableName ();
75- }
76-
7771 $ registry ->linkTable (
7872 $ child ,
79- $ database ,
80- $ tableName ,
73+ $ this -> getDatabase ( $ child , $ registry ) ,
74+ $ this -> getTableName ( $ child , $ registry )
8175 );
8276 }
8377 }
@@ -285,4 +279,36 @@ private function getOuterFields(
285279
286280 return $ parent ->getPrimaryFields ();
287281 }
282+
283+ private function getTableName (Entity $ child , Registry $ registry ): string
284+ {
285+ $ parent = $ this ->findParent ($ registry , $ this ->utils ->findParent ($ child ->getClass (), false ));
286+
287+ $ inheritance = $ parent ->getInheritance ();
288+ if (!$ inheritance instanceof SingleTableInheritanceSchema) {
289+ return $ child ->getTableName ();
290+ }
291+ $ entities = \array_map (
292+ static fn (string $ role ) => $ registry ->getEntity ($ role )->getClass (),
293+ $ inheritance ->getChildren ()
294+ );
295+
296+ return \in_array ($ child ->getClass (), $ entities , true ) ? $ parent ->getTableName () : $ child ->getTableName ();
297+ }
298+
299+ private function getDatabase (Entity $ child , Registry $ registry ): ?string
300+ {
301+ $ parent = $ this ->findParent ($ registry , $ this ->utils ->findParent ($ child ->getClass (), false ));
302+
303+ $ inheritance = $ parent ->getInheritance ();
304+ if (!$ inheritance instanceof SingleTableInheritanceSchema) {
305+ return $ child ->getDatabase ();
306+ }
307+ $ entities = \array_map (
308+ static fn (string $ role ) => $ registry ->getEntity ($ role )->getClass (),
309+ $ inheritance ->getChildren ()
310+ );
311+
312+ return \in_array ($ child ->getClass (), $ entities , true ) ? $ parent ->getDatabase () : $ child ->getDatabase ();
313+ }
288314}
0 commit comments