diff --git a/src/generator/default/dbmodel.php b/src/generator/default/dbmodel.php index a85fa7bb..448aa5ba 100644 --- a/src/generator/default/dbmodel.php +++ b/src/generator/default/dbmodel.php @@ -144,12 +144,14 @@ public function getgetCamelName() ?>() } -inverseRelations as $relationName => $relation): ?> +belongsToRelations as $relationName => $relation): ?>getCamelName(), $usedRelationNames) ? $i : '' ?> - public function getgetCamelName().($i===1 ? '' : $i) ?>() + # belongs to relation + public function getgetCamelName() . ($number) ?>() { return $this->getMethod() ?>(\\getClassName() ?>::class, linkToString() ?>)->inverseOf('getInverse() ?>'); + echo $relation->linkToString() ?>); } - +getCamelName(); endforeach; ?> } diff --git a/src/lib/AttributeResolver.php b/src/lib/AttributeResolver.php index cc41a6fb..0c0c41c9 100644 --- a/src/lib/AttributeResolver.php +++ b/src/lib/AttributeResolver.php @@ -37,6 +37,11 @@ class AttributeResolver */ public array $relations = []; + /** + * @var array keys contains class names and value contains array of belongs to relations + */ + public array $belongsToRelations = []; + /** * @var NonDbRelation[]|array */ @@ -60,11 +65,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; @@ -232,6 +232,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'); @@ -277,12 +278,17 @@ protected function resolveProperty( $relation->onDeleteFkConstraint = $property->onDeleteFkConstraint; if ($property->isRefPointerToSelf()) { $relation->asSelfReference(); + } else { # belongs to relations https://github.com/php-openapi/yii2-openapi/issues/90 + $belongsToRelation = Yii::createObject( + AttributeRelation::class, + [$this->schemaName, $this->tableName, $this->schemaName] + ) + ->asHasOne([$attribute->columnName => $fkProperty->getName()]); + $this->belongsToRelations[$property->getRefClassName()][] = $belongsToRelation; } $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()) @@ -338,6 +344,7 @@ protected function resolveProperty( ->asHasMany([$foreignPk => $this->componentSchema->getPkName()]); return; } + $relatedClassName = $property->getRefClassName(); $relatedTableName = $property->getRefSchema()->resolveTableName($relatedClassName); if ($this->catchManyToMany( @@ -518,22 +525,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; - } } diff --git a/src/lib/SchemaToDatabase.php b/src/lib/SchemaToDatabase.php index ccb230a3..0c513b94 100644 --- a/src/lib/SchemaToDatabase.php +++ b/src/lib/SchemaToDatabase.php @@ -106,18 +106,15 @@ 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 + // handle belongs to relation foreach ($resolvers as $aResolver) { - foreach ($aResolver->inverseRelations as $name => $relations) { - foreach ($relations as $relation) { - /** @var AttributeRelation $relation */ - $models[$name]->inverseRelations[] = $relation; - } + foreach ($aResolver->belongsToRelations as $name => $relations) { + /** @var AttributeRelation[] $relations */ + $models[$name]->belongsToRelations = [...$models[$name]->belongsToRelations, ...$relations]; } } diff --git a/src/lib/items/DbModel.php b/src/lib/items/DbModel.php index 08135d71..5b19d03c 100644 --- a/src/lib/items/DbModel.php +++ b/src/lib/items/DbModel.php @@ -67,11 +67,6 @@ class DbModel extends BaseObject */ public array $many2many = []; - /** - * @var array|AttributeRelation[] inverse relations - */ - public array $inverseRelations = []; - public array $junctionCols = []; /** @@ -91,9 +86,9 @@ class DbModel extends BaseObject public bool $descriptionIsComment = false; /** - * @var array Automatically generated scenarios from the model 'x-scenarios'. + * @var AttributeRelation[] belongs to relations */ - private $_scenarios; + public array $belongsToRelations = []; /** * @var bool @@ -103,6 +98,11 @@ class DbModel extends BaseObject */ public $drop = false; + /** + * @var array Automatically generated scenarios from the model 'x-scenarios'. + */ + private $_scenarios; + public function getTableAlias(): string { return '{{%' . $this->tableName . '}}'; diff --git a/tests/specs/blog/models/base/Category.php b/tests/specs/blog/models/base/Category.php index 188ec03c..287bdc9f 100644 --- a/tests/specs/blog/models/base/Category.php +++ b/tests/specs/blog/models/base/Category.php @@ -39,8 +39,9 @@ public function getPosts() return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category'); } + # belongs to relation public function getPost() { - return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category'); + return $this->hasOne(\app\models\Post::class, ['category_id' => 'id']); } } diff --git a/tests/specs/blog/models/base/Post.php b/tests/specs/blog/models/base/Post.php index 633de3f4..2a97d0fa 100644 --- a/tests/specs/blog/models/base/Post.php +++ b/tests/specs/blog/models/base/Post.php @@ -62,8 +62,9 @@ public function getComments() return $this->hasMany(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post'); } + # belongs to relation public function getComment() { - return $this->hasOne(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post'); + return $this->hasOne(\app\models\Comment::class, ['post_id' => 'uid']); } } diff --git a/tests/specs/blog/models/base/User.php b/tests/specs/blog/models/base/User.php index dbd3a708..557328f9 100644 --- a/tests/specs/blog/models/base/User.php +++ b/tests/specs/blog/models/base/User.php @@ -45,13 +45,15 @@ public function rules() ]; } + # belongs to relation public function getPost() { - return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id'])->inverseOf('created_by'); + return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id']); } - public function getComment2() + # belongs to relation + public function getComment() { - return $this->hasOne(\app\models\Comment::class, ['author_id' => 'id'])->inverseOf('author'); + return $this->hasOne(\app\models\Comment::class, ['author_id' => 'id']); } } diff --git a/tests/specs/blog_v2/models/base/Category.php b/tests/specs/blog_v2/models/base/Category.php index 4207f516..1b97af75 100644 --- a/tests/specs/blog_v2/models/base/Category.php +++ b/tests/specs/blog_v2/models/base/Category.php @@ -39,8 +39,9 @@ public function getPosts() return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category'); } + # belongs to relation public function getPost() { - return $this->hasOne(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category'); + return $this->hasOne(\app\models\Post::class, ['category_id' => 'id']); } } diff --git a/tests/specs/blog_v2/models/base/Post.php b/tests/specs/blog_v2/models/base/Post.php index debc5008..0b82cefc 100644 --- a/tests/specs/blog_v2/models/base/Post.php +++ b/tests/specs/blog_v2/models/base/Post.php @@ -74,8 +74,9 @@ public function getTags() ->viaTable('posts2tags', ['post_id' => 'id']); } + # belongs to relation public function getComment() { - return $this->hasOne(\app\models\Comment::class, ['post_id' => 'id'])->inverseOf('post'); + return $this->hasOne(\app\models\Comment::class, ['post_id' => 'id']); } } diff --git a/tests/specs/blog_v2/models/base/User.php b/tests/specs/blog_v2/models/base/User.php index 45c5b4e0..820b8259 100644 --- a/tests/specs/blog_v2/models/base/User.php +++ b/tests/specs/blog_v2/models/base/User.php @@ -48,13 +48,15 @@ public function rules() ]; } + # belongs to relation public function getPost() { - return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id'])->inverseOf('created_by'); + return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id']); } - public function getComment2() + # belongs to relation + public function getComment() { - return $this->hasOne(\app\models\Comment::class, ['user_id' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Comment::class, ['user_id' => 'id']); } } diff --git a/tests/specs/fk_col_name/app/models/base/Delivery.php b/tests/specs/fk_col_name/app/models/base/Delivery.php index 75a64400..3bb543a1 100644 --- a/tests/specs/fk_col_name/app/models/base/Delivery.php +++ b/tests/specs/fk_col_name/app/models/base/Delivery.php @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getWebhook() { - return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id'])->inverseOf('redelivery_of'); + return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id']); } } diff --git a/tests/specs/fk_col_name/app/models/base/User.php b/tests/specs/fk_col_name/app/models/base/User.php index ede2e7c0..7847f799 100644 --- a/tests/specs/fk_col_name/app/models/base/User.php +++ b/tests/specs/fk_col_name/app/models/base/User.php @@ -29,8 +29,9 @@ public function rules() ]; } + # belongs to relation public function getWebhook() { - return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id']); } } diff --git a/tests/specs/fk_col_name_index/app/models/base/Delivery.php b/tests/specs/fk_col_name_index/app/models/base/Delivery.php index a17531d1..98b9c801 100644 --- a/tests/specs/fk_col_name_index/app/models/base/Delivery.php +++ b/tests/specs/fk_col_name_index/app/models/base/Delivery.php @@ -28,13 +28,15 @@ public function rules() ]; } + # belongs to relation public function getWebhook() { - return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id'])->inverseOf('redelivery_of'); + return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id']); } + # belongs to relation public function getWebhook2() { - return $this->hasOne(\app\models\Webhook::class, ['rd_abc_2' => 'id'])->inverseOf('rd2'); + return $this->hasOne(\app\models\Webhook::class, ['rd_abc_2' => 'id']); } } diff --git a/tests/specs/fk_col_name_index/app/models/base/User.php b/tests/specs/fk_col_name_index/app/models/base/User.php index ede2e7c0..7847f799 100644 --- a/tests/specs/fk_col_name_index/app/models/base/User.php +++ b/tests/specs/fk_col_name_index/app/models/base/User.php @@ -29,8 +29,9 @@ public function rules() ]; } + # belongs to relation public function getWebhook() { - return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id']); } } diff --git a/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Mailing.php b/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Mailing.php index 57bb103f..2639a486 100644 --- a/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Mailing.php +++ b/tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Mailing.php @@ -31,8 +31,9 @@ public function rules() ]; } + # belongs to relation public function getContact() { - return $this->hasOne(\app\models\Contact::class, ['mailing_id' => 'id'])->inverseOf('mailing'); + return $this->hasOne(\app\models\Contact::class, ['mailing_id' => 'id']); } } diff --git a/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/base/Invoice.php b/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/base/Invoice.php index cf4c0f73..1f685781 100644 --- a/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/base/Invoice.php +++ b/tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/base/Invoice.php @@ -24,8 +24,9 @@ public function rules() return []; } + # belongs to relation public function getOrder() { - return $this->hasOne(\app\models\Order::class, ['invoice_id' => 'id'])->inverseOf('invoice'); + return $this->hasOne(\app\models\Order::class, ['invoice_id' => 'id']); } } diff --git a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Account.php b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Account.php index cd597631..079b93b9 100644 --- a/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Account.php +++ b/tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Account.php @@ -31,8 +31,9 @@ public function rules() ]; } + # belongs to relation public function getContact() { - return $this->hasOne(\app\models\Contact::class, ['account_id' => 'id'])->inverseOf('account'); + return $this->hasOne(\app\models\Contact::class, ['account_id' => 'id']); } } diff --git a/tests/specs/issue_fix/25_generate_inverse_relations/mysql/models/base/User.php b/tests/specs/issue_fix/25_generate_inverse_relations/mysql/models/base/User.php index 7e26bb25..c1b63234 100644 --- a/tests/specs/issue_fix/25_generate_inverse_relations/mysql/models/base/User.php +++ b/tests/specs/issue_fix/25_generate_inverse_relations/mysql/models/base/User.php @@ -35,18 +35,21 @@ public function getAccounts() return $this->hasMany(\app\models\Account::class, ['user_id' => 'id'])->inverseOf('user'); } + # belongs to relation public function getAccount() { - return $this->hasOne(\app\models\Account::class, ['user_id' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Account::class, ['user_id' => 'id']); } + # belongs to relation public function getAccount2() { - return $this->hasOne(\app\models\Account::class, ['user2_id' => 'id'])->inverseOf('user2'); + return $this->hasOne(\app\models\Account::class, ['user2_id' => 'id']); } + # belongs to relation public function getAccount3() { - return $this->hasOne(\app\models\Account::class, ['user3' => 'id'])->inverseOf('user3'); + return $this->hasOne(\app\models\Account::class, ['user3' => 'id']); } } diff --git a/tests/specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/User.php b/tests/specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/User.php index 390d8bdb..547aa903 100644 --- a/tests/specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/User.php +++ b/tests/specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/User.php @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getPost() { - return $this->hasOne(\app\models\Post::class, ['user' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Post::class, ['user' => 'id']); } } diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Animal.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Animal.php index 81e157ac..bfd317f7 100644 --- a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Animal.php +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Animal.php @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getInvoice() { - return $this->hasOne(\app\models\Invoice::class, ['animal_id' => 'id'])->inverseOf('animal'); + return $this->hasOne(\app\models\Invoice::class, ['animal_id' => 'id']); } } diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Fruit.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Fruit.php index d56f106c..ab5c4054 100644 --- a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Fruit.php +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Fruit.php @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getInvoice() { - return $this->hasOne(\app\models\Invoice::class, ['fruit_id' => 'id'])->inverseOf('fruit'); + return $this->hasOne(\app\models\Invoice::class, ['fruit_id' => 'id']); } } diff --git a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/User.php b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/User.php index a6ce6e79..c132c14b 100644 --- a/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/User.php +++ b/tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/User.php @@ -28,13 +28,15 @@ public function rules() ]; } + # belongs to relation public function getInvoice() { - return $this->hasOne(\app\models\Invoice::class, ['user_id' => 'id'])->inverseOf('user'); + return $this->hasOne(\app\models\Invoice::class, ['user_id' => 'id']); } + # belongs to relation public function getInvoice2() { - return $this->hasOne(\app\models\Invoice::class, ['user_2_id' => 'id'])->inverseOf('user_2'); + return $this->hasOne(\app\models\Invoice::class, ['user_2_id' => 'id']); } } diff --git a/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.php b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.php new file mode 100644 index 00000000..59eb14b0 --- /dev/null +++ b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.php @@ -0,0 +1,13 @@ + '@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` +]; diff --git a/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.yaml b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.yaml new file mode 100644 index 00000000..8acbf164 --- /dev/null +++ b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.yaml @@ -0,0 +1,31 @@ +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' diff --git a/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/Address.php b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/Address.php new file mode 100644 index 00000000..abfd7d59 --- /dev/null +++ b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/Address.php @@ -0,0 +1,10 @@ + [['name'], 'trim'], + 'name_string' => [['name'], 'string'], + ]; + } + + # belongs to relation + public function getHuman() + { + return $this->hasOne(\app\models\Human::class, ['address_id' => 'id']); + } +} diff --git a/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/base/Human.php b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/base/Human.php new file mode 100644 index 00000000..dadaa276 --- /dev/null +++ b/tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/base/Human.php @@ -0,0 +1,39 @@ + [['name'], 'trim'], + 'name_string' => [['name'], 'string'], + 'address_id_integer' => [['address_id'], 'integer'], + 'address_id_exist' => [['address_id'], 'exist', 'targetRelation' => 'address'], + ]; + } + + public function getAddress() + { + return $this->hasOne(\app\models\Address::class, ['id' => 'address_id']); + } +} diff --git a/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.php b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.php new file mode 100644 index 00000000..2c6dfb9b --- /dev/null +++ b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.php @@ -0,0 +1,13 @@ + '@specs/issue_fix/90_implement_belongs_to_relations_in_models/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` +]; diff --git a/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.yaml b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.yaml new file mode 100644 index 00000000..b62591f0 --- /dev/null +++ b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/index.yaml @@ -0,0 +1,26 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: \#90 +paths: + /: + get: + responses: + '200': + description: The information + +components: + schemas: + User: + type: object + properties: + id: + type: integer + + Address: + type: object + properties: + id: + type: integer + user: + $ref: '#/components/schemas/User' diff --git a/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/Address.php b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/Address.php new file mode 100644 index 00000000..abfd7d59 --- /dev/null +++ b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/Address.php @@ -0,0 +1,10 @@ + [['user_id'], 'integer'], + 'user_id_exist' => [['user_id'], 'exist', 'targetRelation' => 'user'], + ]; + } + + public function getUser() + { + return $this->hasOne(\app\models\User::class, ['id' => 'user_id']); + } +} diff --git a/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/base/User.php b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/base/User.php new file mode 100644 index 00000000..a6a35b66 --- /dev/null +++ b/tests/specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql/models/base/User.php @@ -0,0 +1,32 @@ +hasOne(\app\models\Address::class, ['user_id' => 'id']); + } +} diff --git a/tests/specs/issue_fix/model_name_more_than_once_in_faker_148/app/models/base/Account.php b/tests/specs/issue_fix/model_name_more_than_once_in_faker_148/app/models/base/Account.php index f875eadd..a296c4c0 100644 --- a/tests/specs/issue_fix/model_name_more_than_once_in_faker_148/app/models/base/Account.php +++ b/tests/specs/issue_fix/model_name_more_than_once_in_faker_148/app/models/base/Account.php @@ -29,18 +29,21 @@ public function rules() ]; } + # belongs to relation public function getE123() { - return $this->hasOne(\app\models\E123::class, ['account_id' => 'id'])->inverseOf('account'); + return $this->hasOne(\app\models\E123::class, ['account_id' => 'id']); } + # belongs to relation public function getE1232() { - return $this->hasOne(\app\models\E123::class, ['account_2_id' => 'id'])->inverseOf('account_2'); + return $this->hasOne(\app\models\E123::class, ['account_2_id' => 'id']); } + # belongs to relation public function getE1233() { - return $this->hasOne(\app\models\E123::class, ['account_3_id' => 'id'])->inverseOf('account_3'); + return $this->hasOne(\app\models\E123::class, ['account_3_id' => 'id']); } } diff --git a/tests/specs/petstore/models/base/Store.php b/tests/specs/petstore/models/base/Store.php index 9589d1b8..d9eafaca 100644 --- a/tests/specs/petstore/models/base/Store.php +++ b/tests/specs/petstore/models/base/Store.php @@ -29,8 +29,9 @@ public function rules() ]; } + # belongs to relation public function getPet() { - return $this->hasOne(\app\models\Pet::class, ['store_id' => 'id'])->inverseOf('store'); + return $this->hasOne(\app\models\Pet::class, ['store_id' => 'id']); } } diff --git a/tests/specs/petstore_jsonapi/models/base/Pet.php b/tests/specs/petstore_jsonapi/models/base/Pet.php index 6444cf28..0f057dc1 100644 --- a/tests/specs/petstore_jsonapi/models/base/Pet.php +++ b/tests/specs/petstore_jsonapi/models/base/Pet.php @@ -64,8 +64,9 @@ public function getDuplicates() return $this->hasMany(\app\models\Pet::class, ['tag' => 'tag']); } + # belongs to relation public function getPetStatistic() { - return $this->hasOne(\app\models\PetStatistic::class, ['parentPet_id' => 'id'])->inverseOf('parentPet'); + return $this->hasOne(\app\models\PetStatistic::class, ['parentPet_id' => 'id']); } } diff --git a/tests/specs/petstore_namespace/mymodels/base/Store.php b/tests/specs/petstore_namespace/mymodels/base/Store.php index 24d0f4e4..35aa907f 100644 --- a/tests/specs/petstore_namespace/mymodels/base/Store.php +++ b/tests/specs/petstore_namespace/mymodels/base/Store.php @@ -29,8 +29,9 @@ public function rules() ]; } + # belongs to relation public function getPet() { - return $this->hasOne(\app\mymodels\Pet::class, ['store_id' => 'id'])->inverseOf('store'); + return $this->hasOne(\app\mymodels\Pet::class, ['store_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/A123.php b/tests/specs/relations_in_faker/app/models/base/A123.php index 0c733222..74d782f2 100644 --- a/tests/specs/relations_in_faker/app/models/base/A123.php +++ b/tests/specs/relations_in_faker/app/models/base/A123.php @@ -37,8 +37,9 @@ public function getB123() return $this->hasOne(\app\models\B123::class, ['id' => 'b123_id']); } + # belongs to relation public function getRouting() { - return $this->hasOne(\app\models\Routing::class, ['a123_id' => 'id'])->inverseOf('a123'); + return $this->hasOne(\app\models\Routing::class, ['a123_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/Account.php b/tests/specs/relations_in_faker/app/models/base/Account.php index e1198a95..c5bfedb1 100644 --- a/tests/specs/relations_in_faker/app/models/base/Account.php +++ b/tests/specs/relations_in_faker/app/models/base/Account.php @@ -29,8 +29,9 @@ public function rules() ]; } + # belongs to relation public function getDomain() { - return $this->hasOne(\app\models\Domain::class, ['account_id' => 'id'])->inverseOf('account'); + return $this->hasOne(\app\models\Domain::class, ['account_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/B123.php b/tests/specs/relations_in_faker/app/models/base/B123.php index 7ad4eb77..63727b75 100644 --- a/tests/specs/relations_in_faker/app/models/base/B123.php +++ b/tests/specs/relations_in_faker/app/models/base/B123.php @@ -37,13 +37,15 @@ public function getC123() return $this->hasOne(\app\models\C123::class, ['id' => 'c123_id']); } + # belongs to relation public function getA123() { - return $this->hasOne(\app\models\A123::class, ['b123_id' => 'id'])->inverseOf('b123'); + return $this->hasOne(\app\models\A123::class, ['b123_id' => 'id']); } - public function getE1232() + # belongs to relation + public function getE123() { - return $this->hasOne(\app\models\E123::class, ['b123_id' => 'id'])->inverseOf('b123'); + return $this->hasOne(\app\models\E123::class, ['b123_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/C123.php b/tests/specs/relations_in_faker/app/models/base/C123.php index 6c5677dd..7f516f78 100644 --- a/tests/specs/relations_in_faker/app/models/base/C123.php +++ b/tests/specs/relations_in_faker/app/models/base/C123.php @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getB123() { - return $this->hasOne(\app\models\B123::class, ['c123_id' => 'id'])->inverseOf('c123'); + return $this->hasOne(\app\models\B123::class, ['c123_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/D123.php b/tests/specs/relations_in_faker/app/models/base/D123.php index 80a5017d..71555b18 100644 --- a/tests/specs/relations_in_faker/app/models/base/D123.php +++ b/tests/specs/relations_in_faker/app/models/base/D123.php @@ -28,8 +28,9 @@ public function rules() ]; } + # belongs to relation public function getRouting() { - return $this->hasOne(\app\models\Routing::class, ['d123_id' => 'id'])->inverseOf('d123'); + return $this->hasOne(\app\models\Routing::class, ['d123_id' => 'id']); } } diff --git a/tests/specs/relations_in_faker/app/models/base/Domain.php b/tests/specs/relations_in_faker/app/models/base/Domain.php index df6e0822..fa563120 100644 --- a/tests/specs/relations_in_faker/app/models/base/Domain.php +++ b/tests/specs/relations_in_faker/app/models/base/Domain.php @@ -45,8 +45,9 @@ public function getRoutings() return $this->hasMany(\app\models\Routing::class, ['domain_id' => 'id'])->inverseOf('domain'); } + # belongs to relation public function getRouting() { - return $this->hasOne(\app\models\Routing::class, ['domain_id' => 'id'])->inverseOf('domain'); + return $this->hasOne(\app\models\Routing::class, ['domain_id' => 'id']); } } diff --git a/tests/unit/IssueFixTest.php b/tests/unit/IssueFixTest.php index 9d20ae4d..50221b3c 100644 --- a/tests/unit/IssueFixTest.php +++ b/tests/unit/IssueFixTest.php @@ -1029,4 +1029,32 @@ public function test23ConsiderOpenapiExtensionXNoRelationAlsoInOtherPertinentPla $this->checkFiles($actualFiles, $expectedFiles); $this->runActualMigrations(); } + + // https://github.com/php-openapi/yii2-openapi/issues/88 + public function test88InCaseOfUpdatingAModelGeneratorCreatesRedundantInverseRelations() + { + $testFile = Yii::getAlias("@specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.php"); + $this->runGenerator($testFile); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + } + + // https://github.com/php-openapi/yii2-openapi/issues/90 + public function test90ImplementBelongsToRelationsInModels() + { + $testFile = Yii::getAlias("@specs/issue_fix/90_implement_belongs_to_relations_in_models/index.php"); + $this->runGenerator($testFile); + $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [ + 'recursive' => true, + ]); + $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/90_implement_belongs_to_relations_in_models/mysql"), [ + 'recursive' => true, + ]); + $this->checkFiles($actualFiles, $expectedFiles); + } }