@@ -35,7 +35,8 @@ class AttributeResolver
3535 /**
3636 * @var AttributeRelation[]|array
3737 */
38- private array $ relations = [];
38+ public array $ relations = [];
39+
3940 /**
4041 * @var NonDbRelation[]|array
4142 */
@@ -59,6 +60,11 @@ class AttributeResolver
5960
6061 private ?Config $ config ;
6162
63+ /**
64+ * @var AttributeRelation[]|array
65+ */
66+ public array $ inverseRelations = [];
67+
6268 public function __construct (string $ schemaName , ComponentSchema $ schema , JunctionSchemas $ junctions , ?Config $ config = null )
6369 {
6470 $ this ->schemaName = $ schemaName ;
@@ -94,22 +100,7 @@ public function resolve(): DbModel
94100 $ this ->resolveProperty ($ property , $ isRequired , $ nullableValue );
95101 }
96102 }
97- return Yii::createObject (DbModel::class, [
98- [
99- 'pkName ' => $ this ->schema ->getPkName (),
100- 'name ' => $ this ->schemaName ,
101- 'tableName ' => $ this ->tableName ,
102- 'description ' => $ this ->schema ->getDescription (),
103- 'attributes ' => $ this ->attributes ,
104- 'relations ' => $ this ->relations ,
105- 'nonDbRelations ' => $ this ->nonDbRelations ,
106- 'many2many ' => $ this ->many2many ,
107- 'indexes ' => $ this ->prepareIndexes ($ this ->schema ->getIndexes ()),
108- //For valid primary keys for junction tables
109- 'junctionCols ' => $ this ->isJunctionSchema ? $ this ->junctions ->junctionCols ($ this ->schemaName ) : [],
110- 'isNotDb ' => $ this ->schema ->isNonDb (),
111- ],
112- ]);
103+ return $ this ->createDbModel ();
113104 }
114105
115106 /**
@@ -258,6 +249,14 @@ protected function resolveProperty(
258249 $ relation ->asSelfReference ();
259250 }
260251 $ this ->relations [$ property ->getName ()] = $ relation ;
252+
253+ $ inverseRelation = Yii::createObject (
254+ AttributeRelation::class,
255+ [$ this ->schemaName , $ this ->tableName , $ this ->schemaName ]
256+ )
257+ ->asHasOne ([$ attribute ->columnName => $ fkProperty ->getName ()]);
258+ $ inverseRelation ->setInverse (true );
259+ $ this ->inverseRelations [$ relatedClassName ] = $ inverseRelation ;
261260 }
262261 if (!$ property ->isReference () && !$ property ->hasRefItems ()) {
263262 [$ min , $ max ] = $ property ->guessMinMax ();
@@ -475,4 +474,28 @@ protected function resolvePropertyRef(PropertySchema $property, Attribute $attri
475474 $ this ->attributes [$ property ->getName ()] =
476475 $ attribute ->setFakerStub ($ this ->guessFakerStub ($ attribute , $ fkProperty ));
477476 }
477+
478+ /**
479+ * @throws InvalidDefinitionException
480+ * @throws InvalidConfigException
481+ */
482+ public function createDbModel (): DbModel
483+ {
484+ return Yii::createObject (DbModel::class, [
485+ [
486+ 'pkName ' => $ this ->schema ->getPkName (),
487+ 'name ' => $ this ->schemaName ,
488+ 'tableName ' => $ this ->tableName ,
489+ 'description ' => $ this ->schema ->getDescription (),
490+ 'attributes ' => $ this ->attributes ,
491+ 'relations ' => $ this ->relations ,
492+ 'nonDbRelations ' => $ this ->nonDbRelations ,
493+ 'many2many ' => $ this ->many2many ,
494+ 'indexes ' => $ this ->prepareIndexes ($ this ->schema ->getIndexes ()),
495+ //For valid primary keys for junction tables
496+ 'junctionCols ' => $ this ->isJunctionSchema ? $ this ->junctions ->junctionCols ($ this ->schemaName ) : [],
497+ 'isNotDb ' => $ this ->schema ->isNonDb (),
498+ ],
499+ ]);
500+ }
478501}
0 commit comments