@@ -37,6 +37,11 @@ class AttributeResolver
3737 */
3838 public array $ relations = [];
3939
40+ /**
41+ * @var array keys contains class names and value contains array of belongs to relations
42+ */
43+ public array $ belongsToRelations = [];
44+
4045 /**
4146 * @var NonDbRelation[]|array
4247 */
@@ -60,11 +65,6 @@ class AttributeResolver
6065
6166 private ?Config $ config ;
6267
63- /**
64- * @var AttributeRelation[]|array
65- */
66- public array $ inverseRelations = [];
67-
6868 public function __construct (string $ schemaName , ComponentSchema $ schema , JunctionSchemas $ junctions , ?Config $ config = null )
6969 {
7070 $ this ->schemaName = $ schemaName ;
@@ -232,6 +232,7 @@ protected function resolveProperty(
232232 ->setForeignKeyColumnName ($ property ->fkColName )
233233 ->setFakerStub ($ this ->guessFakerStub ($ attribute , $ property ))
234234 ->setTableName ($ this ->componentSchema ->resolveTableName ($ this ->schemaName ));
235+
235236 if ($ property ->isReference ()) {
236237 if ($ property ->isVirtual ()) {
237238 throw new InvalidDefinitionException ('References not supported for virtual attributes ' );
@@ -277,12 +278,17 @@ protected function resolveProperty(
277278 $ relation ->onDeleteFkConstraint = $ property ->onDeleteFkConstraint ;
278279 if ($ property ->isRefPointerToSelf ()) {
279280 $ relation ->asSelfReference ();
281+ } else { # belongs to relations https://github.com/php-openapi/yii2-openapi/issues/90
282+ $ belongsToRelation = Yii::createObject (
283+ AttributeRelation::class,
284+ [$ this ->schemaName , $ this ->tableName , $ this ->schemaName ]
285+ )
286+ ->asHasOne ([$ attribute ->columnName => $ fkProperty ->getName ()]);
287+ $ this ->belongsToRelations [$ property ->getRefClassName ()][] = $ belongsToRelation ;
280288 }
281289 $ this ->relations [$ property ->getName ()] = $ relation ;
282- if (!$ property ->isRefPointerToSelf ()) {
283- $ this ->addInverseRelation ($ relatedClassName , $ attribute , $ property , $ fkProperty );
284- }
285290 }
291+
286292 if (!$ property ->isReference () && !$ property ->hasRefItems ()) {
287293 [$ min , $ max ] = $ property ->guessMinMax ();
288294 $ attribute ->setIsVirtual ($ property ->isVirtual ())
@@ -338,6 +344,7 @@ protected function resolveProperty(
338344 ->asHasMany ([$ foreignPk => $ this ->componentSchema ->getPkName ()]);
339345 return ;
340346 }
347+
341348 $ relatedClassName = $ property ->getRefClassName ();
342349 $ relatedTableName = $ property ->getRefSchema ()->resolveTableName ($ relatedClassName );
343350 if ($ this ->catchManyToMany (
@@ -518,22 +525,4 @@ public static function relationName(string $propertyName, ?string $fkColumnName)
518525 }
519526 return $ relationName ;
520527 }
521-
522- /**
523- * @throws InvalidConfigException
524- */
525- public function addInverseRelation (
526- string $ relatedClassName ,
527- Attribute $ attribute ,
528- PropertySchema $ property ,
529- PropertySchema $ fkProperty
530- ): void {
531- $ inverseRelation = Yii::createObject (
532- AttributeRelation::class,
533- [$ this ->schemaName , $ this ->tableName , $ this ->schemaName ]
534- )
535- ->asHasOne ([$ attribute ->columnName => $ fkProperty ->getName ()]);
536- $ inverseRelation ->setInverse ($ property ->getName ());
537- $ this ->inverseRelations [$ relatedClassName ][] = $ inverseRelation ;
538- }
539528}
0 commit comments