Skip to content

Commit 8921fed

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 fix-conflicting-pull-requests
2 parents f83bbd4 + ca1e12a commit 8921fed

File tree

39 files changed

+152
-235
lines changed

39 files changed

+152
-235
lines changed

src/generator/default/dbmodel.php

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

src/lib/AttributeResolver.php

Lines changed: 3 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;
@@ -232,6 +227,7 @@ protected function resolveProperty(
232227
->setForeignKeyColumnName($property->fkColName)
233228
->setFakerStub($this->guessFakerStub($attribute, $property))
234229
->setTableName($this->componentSchema->resolveTableName($this->schemaName));
230+
235231
if ($property->isReference()) {
236232
if ($property->isVirtual()) {
237233
throw new InvalidDefinitionException('References not supported for virtual attributes');
@@ -279,10 +275,8 @@ protected function resolveProperty(
279275
$relation->asSelfReference();
280276
}
281277
$this->relations[$property->getName()] = $relation;
282-
if (!$property->isRefPointerToSelf()) {
283-
$this->addInverseRelation($relatedClassName, $attribute, $property, $fkProperty);
284-
}
285278
}
279+
286280
if (!$property->isReference() && !$property->hasRefItems()) {
287281
[$min, $max] = $property->guessMinMax();
288282
$attribute->setIsVirtual($property->isVirtual())
@@ -338,6 +332,7 @@ protected function resolveProperty(
338332
->asHasMany([$foreignPk => $this->componentSchema->getPkName()]);
339333
return;
340334
}
335+
341336
$relatedClassName = $property->getRefClassName();
342337
$relatedTableName = $property->getRefSchema()->resolveTableName($relatedClassName);
343338
if ($this->catchManyToMany(
@@ -518,22 +513,4 @@ public static function relationName(string $propertyName, ?string $fkColumnName)
518513
}
519514
return $relationName;
520515
}
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-
}
539516
}

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)