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

-8
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

+3-26
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

-11
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

-5
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

-5
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

-5
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

-10
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

-5
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

-5
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

-10
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
}

tests/specs/fk_col_name/app/models/base/Delivery.php

-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,4 @@ public function rules()
2727
'title_string' => [['title'], 'string'],
2828
];
2929
}
30-
31-
public function getWebhook()
32-
{
33-
return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id'])->inverseOf('redelivery_of');
34-
}
3530
}

tests/specs/fk_col_name/app/models/base/User.php

-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,4 @@ public function rules()
2828
'name_string' => [['name'], 'string'],
2929
];
3030
}
31-
32-
public function getWebhook()
33-
{
34-
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user');
35-
}
3631
}

tests/specs/fk_col_name_index/app/models/base/Delivery.php

-10
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,4 @@ public function rules()
2727
'title_string' => [['title'], 'string'],
2828
];
2929
}
30-
31-
public function getWebhook()
32-
{
33-
return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id'])->inverseOf('redelivery_of');
34-
}
35-
36-
public function getWebhook2()
37-
{
38-
return $this->hasOne(\app\models\Webhook::class, ['rd_abc_2' => 'id'])->inverseOf('rd2');
39-
}
4030
}

tests/specs/fk_col_name_index/app/models/base/User.php

-5
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,4 @@ public function rules()
2828
'name_string' => [['name'], 'string'],
2929
];
3030
}
31-
32-
public function getWebhook()
33-
{
34-
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user');
35-
}
3631
}

tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Mailing.php

-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,4 @@ public function rules()
3030
'paymentMethodName_string' => [['paymentMethodName'], 'string'],
3131
];
3232
}
33-
34-
public function getContact()
35-
{
36-
return $this->hasOne(\app\models\Contact::class, ['mailing_id' => 'id'])->inverseOf('mailing');
37-
}
3833
}

tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/base/Invoice.php

-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,4 @@ public function rules()
2323
{
2424
return [];
2525
}
26-
27-
public function getOrder()
28-
{
29-
return $this->hasOne(\app\models\Order::class, ['invoice_id' => 'id'])->inverseOf('invoice');
30-
}
3126
}

tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Account.php

-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,4 @@ public function rules()
3030
'paymentMethodName_string' => [['paymentMethodName'], 'string'],
3131
];
3232
}
33-
34-
public function getContact()
35-
{
36-
return $this->hasOne(\app\models\Contact::class, ['account_id' => 'id'])->inverseOf('account');
37-
}
3833
}

tests/specs/issue_fix/25_generate_inverse_relations/mysql/models/base/User.php

-15
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,4 @@ public function getAccounts()
3434
{
3535
return $this->hasMany(\app\models\Account::class, ['user_id' => 'id'])->inverseOf('user');
3636
}
37-
38-
public function getAccount()
39-
{
40-
return $this->hasOne(\app\models\Account::class, ['user_id' => 'id'])->inverseOf('user');
41-
}
42-
43-
public function getAccount2()
44-
{
45-
return $this->hasOne(\app\models\Account::class, ['user2_id' => 'id'])->inverseOf('user2');
46-
}
47-
48-
public function getAccount3()
49-
{
50-
return $this->hasOne(\app\models\Account::class, ['user3' => 'id'])->inverseOf('user3');
51-
}
5237
}

tests/specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/User.php

-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,4 @@ public function rules()
2727
'name_string' => [['name'], 'string'],
2828
];
2929
}
30-
31-
public function getPost()
32-
{
33-
return $this->hasOne(\app\models\Post::class, ['user' => 'id'])->inverseOf('user');
34-
}
3530
}

tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Animal.php

-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,4 @@ public function rules()
2727
'name_string' => [['name'], 'string'],
2828
];
2929
}
30-
31-
public function getInvoice()
32-
{
33-
return $this->hasOne(\app\models\Invoice::class, ['animal_id' => 'id'])->inverseOf('animal');
34-
}
3530
}

tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Fruit.php

-5
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,4 @@ public function rules()
2727
'name_string' => [['name'], 'string'],
2828
];
2929
}
30-
31-
public function getInvoice()
32-
{
33-
return $this->hasOne(\app\models\Invoice::class, ['fruit_id' => 'id'])->inverseOf('fruit');
34-
}
3530
}

tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/User.php

-10
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,4 @@ public function rules()
2727
'name_string' => [['name'], 'string'],
2828
];
2929
}
30-
31-
public function getInvoice()
32-
{
33-
return $this->hasOne(\app\models\Invoice::class, ['user_id' => 'id'])->inverseOf('user');
34-
}
35-
36-
public function getInvoice2()
37-
{
38-
return $this->hasOne(\app\models\Invoice::class, ['user_2_id' => 'id'])->inverseOf('user_2');
39-
}
4030
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.yaml',
5+
'generateUrls' => false,
6+
'generateModels' => true,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => false,
11+
'generateMigrations' => false,
12+
'generateModelFaker' => false, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
13+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
openapi: "3.0.0"
2+
3+
info:
4+
version: 1.0.0
5+
title: '#88'
6+
7+
paths:
8+
/:
9+
get:
10+
responses:
11+
'200':
12+
description: The response
13+
14+
components:
15+
schemas:
16+
Address:
17+
type: object
18+
properties:
19+
id:
20+
type: integer
21+
name:
22+
type: string
23+
Human:
24+
type: object
25+
properties:
26+
id:
27+
type: integer
28+
name:
29+
type: string
30+
address:
31+
$ref: '#/components/schemas/Address'
32+
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
class Address extends \app\models\base\Address
6+
{
7+
8+
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
class Human extends \app\models\base\Human
6+
{
7+
8+
9+
}
10+

0 commit comments

Comments
 (0)