Skip to content

88 in case of updating a model generator creates redundant inverse relations #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -144,12 +144,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