Skip to content

Commit 9ff2113

Browse files
committed
Implement
1 parent 5a5ee39 commit 9ff2113

File tree

6 files changed

+90
-24
lines changed

6 files changed

+90
-24
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ related objects, `x-no-relation` (type: boolean, default: false) is used.
350350

351351
This will not generate 'comments' column in database migrations. But it will generate `getComments()` relation in Yii model file.
352352

353-
In order to make it real database column, extension `x-no-relation` can be used.
353+
In order to make it real database column, OpenAPI extension `x-no-relation` can be used.
354354

355355
```yaml
356356
comments:

src/lib/AttributeResolver.php

+25-17
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,9 @@ protected function resolveProperty(
278278
if ($property->isRefPointerToSelf()) {
279279
$relation->asSelfReference();
280280
}
281-
$this->relations[$property->getName()] = $relation;
281+
if (empty($property->getAttr(CustomSpecAttr::NO_RELATION))) {
282+
$this->relations[$property->getName()] = $relation;
283+
}
282284
if (!$property->isRefPointerToSelf()) {
283285
$this->addInverseRelation($relatedClassName, $attribute, $property, $fkProperty);
284286
}
@@ -321,21 +323,25 @@ protected function resolveProperty(
321323
$fkProperty->getName(),
322324
'_id'
323325
)) {
326+
if (empty($property->getAttr(CustomSpecAttr::NO_RELATION))) {
327+
$this->relations[$property->getName()] =
328+
Yii::createObject(
329+
AttributeRelation::class,
330+
[static::relationName($property->getName(), $property->fkColName), $relatedTableName, $relatedClassName]
331+
)
332+
->asHasMany([$fkProperty->getName() => $fkProperty->getName()])->asSelfReference();
333+
}
334+
return;
335+
}
336+
$foreignPk = Inflector::camel2id($fkProperty->getName(), '_') . '_id';
337+
if (empty($property->getAttr(CustomSpecAttr::NO_RELATION))) {
324338
$this->relations[$property->getName()] =
325339
Yii::createObject(
326340
AttributeRelation::class,
327341
[static::relationName($property->getName(), $property->fkColName), $relatedTableName, $relatedClassName]
328342
)
329-
->asHasMany([$fkProperty->getName() => $fkProperty->getName()])->asSelfReference();
330-
return;
343+
->asHasMany([$foreignPk => $this->componentSchema->getPkName()]);
331344
}
332-
$foreignPk = Inflector::camel2id($fkProperty->getName(), '_') . '_id';
333-
$this->relations[$property->getName()] =
334-
Yii::createObject(
335-
AttributeRelation::class,
336-
[static::relationName($property->getName(), $property->fkColName), $relatedTableName, $relatedClassName]
337-
)
338-
->asHasMany([$foreignPk => $this->componentSchema->getPkName()]);
339345
return;
340346
}
341347
$relatedClassName = $property->getRefClassName();
@@ -349,13 +355,15 @@ protected function resolveProperty(
349355
return;
350356
}
351357
$attribute->setPhpType($relatedClassName . '[]');
352-
$this->relations[$property->getName()] =
353-
Yii::createObject(
354-
AttributeRelation::class,
355-
[static::relationName($property->getName(), $property->fkColName), $relatedTableName, $relatedClassName]
356-
)
357-
->asHasMany([Inflector::camel2id($this->schemaName, '_') . '_id' => $this->componentSchema->getPkName()])
358-
->setInverse(Inflector::variablize($this->schemaName));
358+
if (empty($property->getAttr(CustomSpecAttr::NO_RELATION))) {
359+
$this->relations[$property->getName()] =
360+
Yii::createObject(
361+
AttributeRelation::class,
362+
[static::relationName($property->getName(), $property->fkColName), $relatedTableName, $relatedClassName]
363+
)
364+
->asHasMany([Inflector::camel2id($this->schemaName, '_') . '_id' => $this->componentSchema->getPkName()])
365+
->setInverse(Inflector::variablize($this->schemaName));
366+
}
359367
return;
360368
}
361369
if ($this->componentSchema->isNonDb() && $attribute->isReference()) {

tests/specs/issue_fix/20_consider_openapi_spec_examples_in_faker_code_generation/mysql/models/base/Pet.php

-6
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
* @property array $one_of_from_multi_ref_arr
3030
*
3131
* @property array|\app\models\User[] $userRefObjArrNormal
32-
* @property array|\app\models\User[] $userRefObjArr
3332
*/
3433
abstract class Pet extends \yii\db\ActiveRecord
3534
{
@@ -53,9 +52,4 @@ public function getUserRefObjArrNormal()
5352
{
5453
return $this->hasMany(\app\models\User::class, ['pet_id' => 'id'])->inverseOf('pet');
5554
}
56-
57-
public function getUserRefObjArr()
58-
{
59-
return $this->hasMany(\app\models\User::class, ['pet_id' => 'id'])->inverseOf('pet');
60-
}
6155
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/23_consider_openapi_extension_x_no_relation_also_in_other_pertinent_place/index.yaml',
5+
'generateUrls' => false,
6+
'generateModels' => true,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => false,
11+
'generateMigrations' => true,
12+
'generateModelFaker' => true, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
13+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
openapi: 3.0.3
2+
3+
info:
4+
title: '#23'
5+
version: 1.0.0
6+
7+
paths:
8+
/:
9+
get:
10+
responses:
11+
'200':
12+
description: The Response
13+
14+
15+
16+
components:
17+
schemas:
18+
Payments:
19+
properties:
20+
id:
21+
type: integer
22+
currency:
23+
type: string
24+
samples:
25+
type: array
26+
x-no-relation: true
27+
items:
28+
$ref: '#/components/schemas/Sample'
29+
30+
Sample:
31+
properties:
32+
id:
33+
type: integer
34+
message:
35+
type: string

tests/unit/IssueFixTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -1001,4 +1001,20 @@ public function test74InvalidSchemaReferenceError()
10011001
$this->runActualMigrations();
10021002
}
10031003

1004+
// https://github.com/php-openapi/yii2-openapi/issues/23
1005+
public function test23ConsiderOpenapiExtensionXNoRelationAlsoInOtherPertinentPlace()
1006+
{
1007+
// 23-consider-openapi-extension-x-no-relation-also-in-other-pertinent-place
1008+
// 23_consider_openapi_extension_x_no_relation_also_in_other_pertinent_place
1009+
$testFile = Yii::getAlias("@specs/issue_fix/23_consider_openapi_extension_x_no_relation_also_in_other_pertinent_place/index.php");
1010+
$this->runGenerator($testFile);
1011+
// $actualFiles = FileHelper::findFiles(Yii::getAlias('@app'), [
1012+
// 'recursive' => true,
1013+
// ]);
1014+
// $expectedFiles = FileHelper::findFiles(Yii::getAlias("@specs/issue_fix/23_consider_openapi_extension_x_no_relation_also_in_other_pertinent_place/mysql"), [
1015+
// 'recursive' => true,
1016+
// ]);
1017+
// $this->checkFiles($actualFiles, $expectedFiles);
1018+
// $this->runActualMigrations();
1019+
}
10041020
}

0 commit comments

Comments
 (0)