Skip to content

Commit d93d0b3

Browse files
committed
Merge branch '88-in-case-of-updating-a-model-generator-creates-redundant-inverse-relations' of github.com:php-openapi/yii2-openapi into 90-implement-belongs-to-relations-in-models
2 parents 4c409f7 + 1231531 commit d93d0b3

File tree

39 files changed

+151
-235
lines changed

39 files changed

+151
-235
lines changed

src/generator/default/dbmodel.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,4 @@ public function get<?= $relation->getCamelName() ?>()
144144
<?php endif;?>
145145
}
146146
<?php endforeach; ?>
147-
<?php $i = 1; foreach ($model->inverseRelations as $relationName => $relation): ?>
148-
149-
public function get<?= $relation->getCamelName().($i===1 ? '' : $i) ?>()
150-
{
151-
return $this-><?= $relation->getMethod() ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>::class, <?php
152-
echo $relation->linkToString() ?>)->inverseOf('<?= $relation->getInverse() ?>');
153-
}
154-
<?php $i++; endforeach; ?>
155147
}

src/lib/AttributeResolver.php

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ class AttributeResolver
6060

6161
private ?Config $config;
6262

63-
/**
64-
* @var AttributeRelation[]|array
65-
*/
66-
public array $inverseRelations = [];
67-
6863
public function __construct(string $schemaName, ComponentSchema $schema, JunctionSchemas $junctions, ?Config $config = null)
6964
{
7065
$this->schemaName = $schemaName;
@@ -280,10 +275,8 @@ protected function resolveProperty(
280275
$relation->asSelfReference();
281276
}
282277
$this->relations[$property->getName()] = $relation;
283-
if (!$property->isRefPointerToSelf()) {
284-
$this->addInverseRelation($relatedClassName, $attribute, $property, $fkProperty);
285-
}
286278
}
279+
287280
if (!$property->isReference() && !$property->hasRefItems()) {
288281
[$min, $max] = $property->guessMinMax();
289282
$attribute->setIsVirtual($property->isVirtual())
@@ -339,6 +332,7 @@ protected function resolveProperty(
339332
->asHasMany([$foreignPk => $this->componentSchema->getPkName()]);
340333
return;
341334
}
335+
342336
$relatedClassName = $property->getRefClassName();
343337
$relatedTableName = $property->getRefSchema()->resolveTableName($relatedClassName);
344338
if ($this->catchManyToMany(
@@ -519,22 +513,4 @@ public static function relationName(string $propertyName, ?string $fkColumnName)
519513
}
520514
return $relationName;
521515
}
522-
523-
/**
524-
* @throws InvalidConfigException
525-
*/
526-
public function addInverseRelation(
527-
string $relatedClassName,
528-
Attribute $attribute,
529-
PropertySchema $property,
530-
PropertySchema $fkProperty
531-
): void {
532-
$inverseRelation = Yii::createObject(
533-
AttributeRelation::class,
534-
[$this->schemaName, $this->tableName, $this->schemaName]
535-
)
536-
->asHasOne([$attribute->columnName => $fkProperty->getName()]);
537-
$inverseRelation->setInverse($property->getName());
538-
$this->inverseRelations[$relatedClassName][] = $inverseRelation;
539-
}
540516
}

src/lib/SchemaToDatabase.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,10 @@ public function prepareModels(): array
106106
/** @var AttributeResolver $resolver */
107107
$resolver = Yii::createObject(AttributeResolver::class, [$schemaName, $schema, $junctions, $this->config]);
108108

109-
// $models[$schemaName] = $resolver->resolve();
110109
$resolvers[$schemaName] = $resolver;
111110
$models[$schemaName] = $resolvers[$schemaName]->resolve();
112111
}
113112

114-
// handle inverse relation
115-
foreach ($resolvers as $aResolver) {
116-
foreach ($aResolver->inverseRelations as $name => $relations) {
117-
foreach ($relations as $relation) {
118-
/** @var AttributeRelation $relation */
119-
$models[$name]->inverseRelations[] = $relation;
120-
}
121-
}
122-
}
123-
124113
foreach ($models as $model) {
125114
foreach ($model->many2many as $relation) {
126115
if (isset($models[$relation->viaModelName])) {

src/lib/items/DbModel.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,6 @@ class DbModel extends BaseObject
6767
*/
6868
public array $many2many = [];
6969

70-
/**
71-
* @var array|AttributeRelation[] inverse relations
72-
*/
73-
public array $inverseRelations = [];
74-
7570
public array $junctionCols = [];
7671

7772
/**

tests/specs/blog/models/base/Category.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,4 @@ public function getPosts()
3838
{
3939
return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
4040
}
41-
42-
public function getPost()
43-
{
44-
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
45-
}
4641
}

tests/specs/blog/models/base/Post.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,4 @@ public function getComments()
6161
{
6262
return $this->hasMany(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post');
6363
}
64-
65-
public function getComment()
66-
{
67-
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post');
68-
}
6964
}

tests/specs/blog/models/base/User.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,4 @@ public function rules()
4444
'email_unique' => [['email'], 'unique'],
4545
];
4646
}
47-
48-
public function getPost()
49-
{
50-
return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id'])->inverseOf('created_by');
51-
}
52-
53-
public function getComment2()
54-
{
55-
return $this->hasOne(\app\models\Comment::class, ['author_id' => 'id'])->inverseOf('author');
56-
}
5747
}

tests/specs/blog_v2/models/base/Category.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,4 @@ public function getPosts()
3838
{
3939
return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
4040
}
41-
42-
public function getPost()
43-
{
44-
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
45-
}
4641
}

tests/specs/blog_v2/models/base/Post.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,4 @@ public function getTags()
7373
return $this->hasMany(\app\models\Tag::class, ['id' => 'tag_id'])
7474
->viaTable('posts2tags', ['post_id' => 'id']);
7575
}
76-
77-
public function getComment()
78-
{
79-
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'id'])->inverseOf('post');
80-
}
8176
}

tests/specs/blog_v2/models/base/User.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,4 @@ public function rules()
4747
'email_unique' => [['email'], 'unique'],
4848
];
4949
}
50-
51-
public function getPost()
52-
{
53-
return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id'])->inverseOf('created_by');
54-
}
55-
56-
public function getComment2()
57-
{
58-
return $this->hasOne(\app\models\Comment::class, ['user_id' => 'id'])->inverseOf('user');
59-
}
6050
}

0 commit comments

Comments
 (0)