@@ -37,6 +37,11 @@ class AttributeResolver
37
37
*/
38
38
public array $ relations = [];
39
39
40
+ /**
41
+ * @var array keys contains class names and value contains array of belongs to relations
42
+ */
43
+ public array $ belongsToRelations = [];
44
+
40
45
/**
41
46
* @var NonDbRelation[]|array
42
47
*/
@@ -60,11 +65,6 @@ class AttributeResolver
60
65
61
66
private ?Config $ config ;
62
67
63
- /**
64
- * @var AttributeRelation[]|array
65
- */
66
- public array $ inverseRelations = [];
67
-
68
68
public function __construct (string $ schemaName , ComponentSchema $ schema , JunctionSchemas $ junctions , ?Config $ config = null )
69
69
{
70
70
$ this ->schemaName = $ schemaName ;
@@ -232,6 +232,7 @@ protected function resolveProperty(
232
232
->setForeignKeyColumnName ($ property ->fkColName )
233
233
->setFakerStub ($ this ->guessFakerStub ($ attribute , $ property ))
234
234
->setTableName ($ this ->componentSchema ->resolveTableName ($ this ->schemaName ));
235
+
235
236
if ($ property ->isReference ()) {
236
237
if ($ property ->isVirtual ()) {
237
238
throw new InvalidDefinitionException ('References not supported for virtual attributes ' );
@@ -277,12 +278,17 @@ protected function resolveProperty(
277
278
$ relation ->onDeleteFkConstraint = $ property ->onDeleteFkConstraint ;
278
279
if ($ property ->isRefPointerToSelf ()) {
279
280
$ 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 ;
280
288
}
281
289
$ this ->relations [$ property ->getName ()] = $ relation ;
282
- if (!$ property ->isRefPointerToSelf ()) {
283
- $ this ->addInverseRelation ($ relatedClassName , $ attribute , $ property , $ fkProperty );
284
- }
285
290
}
291
+
286
292
if (!$ property ->isReference () && !$ property ->hasRefItems ()) {
287
293
[$ min , $ max ] = $ property ->guessMinMax ();
288
294
$ attribute ->setIsVirtual ($ property ->isVirtual ())
@@ -338,6 +344,7 @@ protected function resolveProperty(
338
344
->asHasMany ([$ foreignPk => $ this ->componentSchema ->getPkName ()]);
339
345
return ;
340
346
}
347
+
341
348
$ relatedClassName = $ property ->getRefClassName ();
342
349
$ relatedTableName = $ property ->getRefSchema ()->resolveTableName ($ relatedClassName );
343
350
if ($ this ->catchManyToMany (
@@ -518,22 +525,4 @@ public static function relationName(string $propertyName, ?string $fkColumnName)
518
525
}
519
526
return $ relationName ;
520
527
}
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
- }
539
528
}
0 commit comments