Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
cbb5adb
Create PR
SOHELAHMED7 Feb 12, 2025
ca8e804
Add passing test
SOHELAHMED7 Feb 14, 2025
b050abd
Failing test
SOHELAHMED7 Mar 1, 2025
45f3f90
Fix
SOHELAHMED7 Mar 1, 2025
e69fc04
Fix failing tests
SOHELAHMED7 Mar 3, 2025
ab6629b
Create PR
SOHELAHMED7 Mar 3, 2025
207228b
Add test stub
SOHELAHMED7 Mar 3, 2025
4c409f7
WIP
SOHELAHMED7 Mar 4, 2025
2eda480
Refactor
SOHELAHMED7 Mar 4, 2025
1231531
Refactor 2
SOHELAHMED7 Mar 4, 2025
d93d0b3
Merge branch '88-in-case-of-updating-a-model-generator-creates-redund…
SOHELAHMED7 Mar 4, 2025
a69f43a
Implement
SOHELAHMED7 Mar 4, 2025
f92b782
Complete the test
SOHELAHMED7 Mar 4, 2025
5b1ec54
Fix failing tests
SOHELAHMED7 Mar 4, 2025
2134ddf
Refactor and add docs
SOHELAHMED7 Mar 4, 2025
426fecb
Modify a file to create pull request at GitHub
SOHELAHMED7 Mar 7, 2025
49cd50e
Add message
SOHELAHMED7 Mar 7, 2025
ecbee94
Complete the test
SOHELAHMED7 Mar 7, 2025
229c003
Refactor
SOHELAHMED7 Mar 8, 2025
d89fe28
Refactor and fix failing test
SOHELAHMED7 Mar 8, 2025
4aa7668
Fix
SOHELAHMED7 Mar 8, 2025
24d559a
Merge branches 'master' and '96-component-schema-should-be-optional' …
SOHELAHMED7 Mar 12, 2025
95670f5
Merge branch 'master' of github.com:php-openapi/yii2-openapi into 79-…
SOHELAHMED7 Mar 12, 2025
c4c9467
Merge branch 'master' of github.com:php-openapi/yii2-openapi into 90-…
SOHELAHMED7 Mar 12, 2025
ca1e12a
Merge branch 'master' of github.com:php-openapi/yii2-openapi into 88-…
SOHELAHMED7 Mar 12, 2025
856e1d6
Merge branches 'master' and '96-component-schema-should-be-optional' …
SOHELAHMED7 Mar 12, 2025
f83bbd4
Merge branch '79-response-status-codes-are-not-the-codes-defined-in-s…
SOHELAHMED7 Mar 12, 2025
8921fed
Merge branch '88-in-case-of-updating-a-model-generator-creates-redund…
SOHELAHMED7 Mar 12, 2025
dd34cdb
Merge branch '78-properties-that-are-marked-as-readonly-are-not-read-…
SOHELAHMED7 Mar 12, 2025
4d32a93
Merge branch '90-implement-belongs-to-relations-in-models' of github.…
SOHELAHMED7 Mar 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions src/generator/default/dbmodel.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,4 @@ public function get<?= $relation->getCamelName() ?>()
<?php endif;?>
}
<?php endforeach; ?>
<?php $i = 1; foreach ($model->inverseRelations as $relationName => $relation): ?>

public function get<?= $relation->getCamelName().($i===1 ? '' : $i) ?>()
{
return $this-><?= $relation->getMethod() ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>::class, <?php
echo $relation->linkToString() ?>)->inverseOf('<?= $relation->getInverse() ?>');
}
<?php $i++; endforeach; ?>
}
29 changes: 3 additions & 26 deletions src/lib/AttributeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,6 @@ class AttributeResolver

private ?Config $config;

/**
* @var AttributeRelation[]|array
*/
public array $inverseRelations = [];

public function __construct(string $schemaName, ComponentSchema $schema, JunctionSchemas $junctions, ?Config $config = null)
{
$this->schemaName = $schemaName;
Expand Down Expand Up @@ -232,6 +227,7 @@ protected function resolveProperty(
->setForeignKeyColumnName($property->fkColName)
->setFakerStub($this->guessFakerStub($attribute, $property))
->setTableName($this->componentSchema->resolveTableName($this->schemaName));

if ($property->isReference()) {
if ($property->isVirtual()) {
throw new InvalidDefinitionException('References not supported for virtual attributes');
Expand Down Expand Up @@ -279,10 +275,8 @@ protected function resolveProperty(
$relation->asSelfReference();
}
$this->relations[$property->getName()] = $relation;
if (!$property->isRefPointerToSelf()) {
$this->addInverseRelation($relatedClassName, $attribute, $property, $fkProperty);
}
}

if (!$property->isReference() && !$property->hasRefItems()) {
[$min, $max] = $property->guessMinMax();
$attribute->setIsVirtual($property->isVirtual())
Expand Down Expand Up @@ -338,6 +332,7 @@ protected function resolveProperty(
->asHasMany([$foreignPk => $this->componentSchema->getPkName()]);
return;
}

$relatedClassName = $property->getRefClassName();
$relatedTableName = $property->getRefSchema()->resolveTableName($relatedClassName);
if ($this->catchManyToMany(
Expand Down Expand Up @@ -518,22 +513,4 @@ public static function relationName(string $propertyName, ?string $fkColumnName)
}
return $relationName;
}

/**
* @throws InvalidConfigException
*/
public function addInverseRelation(
string $relatedClassName,
Attribute $attribute,
PropertySchema $property,
PropertySchema $fkProperty
): void {
$inverseRelation = Yii::createObject(
AttributeRelation::class,
[$this->schemaName, $this->tableName, $this->schemaName]
)
->asHasOne([$attribute->columnName => $fkProperty->getName()]);
$inverseRelation->setInverse($property->getName());
$this->inverseRelations[$relatedClassName][] = $inverseRelation;
}
}
11 changes: 0 additions & 11 deletions src/lib/SchemaToDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,21 +106,10 @@ public function prepareModels(): array
/** @var AttributeResolver $resolver */
$resolver = Yii::createObject(AttributeResolver::class, [$schemaName, $schema, $junctions, $this->config]);

// $models[$schemaName] = $resolver->resolve();
$resolvers[$schemaName] = $resolver;
$models[$schemaName] = $resolvers[$schemaName]->resolve();
}

// handle inverse relation
foreach ($resolvers as $aResolver) {
foreach ($aResolver->inverseRelations as $name => $relations) {
foreach ($relations as $relation) {
/** @var AttributeRelation $relation */
$models[$name]->inverseRelations[] = $relation;
}
}
}

foreach ($models as $model) {
foreach ($model->many2many as $relation) {
if (isset($models[$relation->viaModelName])) {
Expand Down
5 changes: 0 additions & 5 deletions src/lib/items/DbModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ class DbModel extends BaseObject
*/
public array $many2many = [];

/**
* @var array|AttributeRelation[] inverse relations
*/
public array $inverseRelations = [];

public array $junctionCols = [];

/**
Expand Down
5 changes: 0 additions & 5 deletions tests/specs/blog/models/base/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,4 @@ public function getPosts()
{
return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
}

public function getPost()
{
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
}
}
5 changes: 0 additions & 5 deletions tests/specs/blog/models/base/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,4 @@ public function getComments()
{
return $this->hasMany(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post');
}

public function getComment()
{
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post');
}
}
10 changes: 0 additions & 10 deletions tests/specs/blog/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,4 @@ public function rules()
'email_unique' => [['email'], 'unique'],
];
}

public function getPost()
{
return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id'])->inverseOf('created_by');
}

public function getComment2()
{
return $this->hasOne(\app\models\Comment::class, ['author_id' => 'id'])->inverseOf('author');
}
}
5 changes: 0 additions & 5 deletions tests/specs/blog_v2/models/base/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,4 @@ public function getPosts()
{
return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
}

public function getPost()
{
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
}
}
5 changes: 0 additions & 5 deletions tests/specs/blog_v2/models/base/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,4 @@ public function getTags()
return $this->hasMany(\app\models\Tag::class, ['id' => 'tag_id'])
->viaTable('posts2tags', ['post_id' => 'id']);
}

public function getComment()
{
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'id'])->inverseOf('post');
}
}
10 changes: 0 additions & 10 deletions tests/specs/blog_v2/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,4 @@ public function rules()
'email_unique' => [['email'], 'unique'],
];
}

public function getPost()
{
return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id'])->inverseOf('created_by');
}

public function getComment2()
{
return $this->hasOne(\app\models\Comment::class, ['user_id' => 'id'])->inverseOf('user');
}
}
5 changes: 0 additions & 5 deletions tests/specs/fk_col_name/app/models/base/Delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,4 @@ public function rules()
'title_string' => [['title'], 'string'],
];
}

public function getWebhook()
{
return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id'])->inverseOf('redelivery_of');
}
}
5 changes: 0 additions & 5 deletions tests/specs/fk_col_name/app/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,4 @@ public function rules()
'name_string' => [['name'], 'string'],
];
}

public function getWebhook()
{
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user');
}
}
10 changes: 0 additions & 10 deletions tests/specs/fk_col_name_index/app/models/base/Delivery.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,4 @@ public function rules()
'title_string' => [['title'], 'string'],
];
}

public function getWebhook()
{
return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id'])->inverseOf('redelivery_of');
}

public function getWebhook2()
{
return $this->hasOne(\app\models\Webhook::class, ['rd_abc_2' => 'id'])->inverseOf('rd2');
}
}
5 changes: 0 additions & 5 deletions tests/specs/fk_col_name_index/app/models/base/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,4 @@ public function rules()
'name_string' => [['name'], 'string'],
];
}

public function getWebhook()
{
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,4 @@ public function rules()
'paymentMethodName_string' => [['paymentMethodName'], 'string'],
];
}

public function getContact()
{
return $this->hasOne(\app\models\Contact::class, ['mailing_id' => 'id'])->inverseOf('mailing');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,4 @@ public function rules()
{
return [];
}

public function getOrder()
{
return $this->hasOne(\app\models\Order::class, ['invoice_id' => 'id'])->inverseOf('invoice');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,4 @@ public function rules()
'paymentMethodName_string' => [['paymentMethodName'], 'string'],
];
}

public function getContact()
{
return $this->hasOne(\app\models\Contact::class, ['account_id' => 'id'])->inverseOf('account');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,4 @@ public function getAccounts()
{
return $this->hasMany(\app\models\Account::class, ['user_id' => 'id'])->inverseOf('user');
}

public function getAccount()
{
return $this->hasOne(\app\models\Account::class, ['user_id' => 'id'])->inverseOf('user');
}

public function getAccount2()
{
return $this->hasOne(\app\models\Account::class, ['user2_id' => 'id'])->inverseOf('user2');
}

public function getAccount3()
{
return $this->hasOne(\app\models\Account::class, ['user3' => 'id'])->inverseOf('user3');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,4 @@ public function rules()
'name_string' => [['name'], 'string'],
];
}

public function getPost()
{
return $this->hasOne(\app\models\Post::class, ['user' => 'id'])->inverseOf('user');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,4 @@ public function rules()
'name_string' => [['name'], 'string'],
];
}

public function getInvoice()
{
return $this->hasOne(\app\models\Invoice::class, ['animal_id' => 'id'])->inverseOf('animal');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,4 @@ public function rules()
'name_string' => [['name'], 'string'],
];
}

public function getInvoice()
{
return $this->hasOne(\app\models\Invoice::class, ['fruit_id' => 'id'])->inverseOf('fruit');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,4 @@ public function rules()
'name_string' => [['name'], 'string'],
];
}

public function getInvoice()
{
return $this->hasOne(\app\models\Invoice::class, ['user_id' => 'id'])->inverseOf('user');
}

public function getInvoice2()
{
return $this->hasOne(\app\models\Invoice::class, ['user_2_id' => 'id'])->inverseOf('user_2');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

return [
'openApiPath' => '@specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.yaml',
'generateUrls' => false,
'generateModels' => true,
'excludeModels' => [
'Error',
],
'generateControllers' => false,
'generateMigrations' => false,
'generateModelFaker' => false, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
openapi: "3.0.0"

info:
version: 1.0.0
title: '#88'

paths:
/:
get:
responses:
'200':
description: The response

components:
schemas:
Address:
type: object
properties:
id:
type: integer
name:
type: string
Human:
type: object
properties:
id:
type: integer
name:
type: string
address:
$ref: '#/components/schemas/Address'


Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class Address extends \app\models\base\Address
{


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace app\models;

class Human extends \app\models\base\Human
{


}

Loading