Skip to content

Commit 5dda237

Browse files
committed
Handle multiple relations and add more tests
1 parent e499530 commit 5dda237

File tree

5 files changed

+44
-21
lines changed

5 files changed

+44
-21
lines changed

src/generator/default/dbmodel.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function rules()
8787
public function get<?= $relation->getCamelName() ?>()
8888
{
8989
return $this-><?= $relation->getMethod() ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>::class, <?php
90-
echo $relation->linkToString()?>);
90+
echo $relation->linkToString()?>)<?= $relation->getInverse() ? '->inverseOf(\''.$relation->getInverse().'\')' : '' ?>;
9191
}
9292
<?php endforeach; ?>
9393
<?php foreach ($model->many2many as $relation): ?>
@@ -103,12 +103,12 @@ public function get<?= $relation->getCamelName() ?>()
103103
<?php endif;?>
104104
}
105105
<?php endforeach; ?>
106-
<?php foreach ($model->inverseRelations as $relationName => $relation): ?>
106+
<?php $i = 1; foreach ($model->inverseRelations as $relationName => $relation): ?>
107107

108-
public function get<?= $relation->getCamelName() ?>()
108+
public function get<?= $relation->getCamelName().($i===1 ? '' : $i) ?>()
109109
{
110-
return $this-><?= $relation->getMethod() ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>::class, <?php
111-
echo $relation->linkToString() ?>)->inverseOf('<?= Inflector::variablize($model->getClassName()) ?>');
110+
return $this-><?= $relation->getMethod() ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>::class, <?php
111+
echo $relation->linkToString() ?>)->inverseOf('<?= $relation->getInverse() ?>');
112112
}
113-
<?php endforeach; ?>
113+
<?php $i++; endforeach; ?>
114114
}

src/lib/AttributeResolver.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -249,14 +249,7 @@ protected function resolveProperty(
249249
$relation->asSelfReference();
250250
}
251251
$this->relations[$property->getName()] = $relation;
252-
253-
$inverseRelation = Yii::createObject(
254-
AttributeRelation::class,
255-
[$this->schemaName, $this->tableName, $this->schemaName]
256-
)
257-
->asHasOne([$attribute->columnName => $fkProperty->getName()]);
258-
$inverseRelation->setInverse(true);
259-
$this->inverseRelations[$relatedClassName] = $inverseRelation;
252+
$this->addInverseRelation($relatedClassName, $attribute, $property, $fkProperty);
260253
}
261254
if (!$property->isReference() && !$property->hasRefItems()) {
262255
[$min, $max] = $property->guessMinMax();
@@ -329,7 +322,8 @@ protected function resolveProperty(
329322
AttributeRelation::class,
330323
[$property->getName(), $relatedTableName, $relatedClassName]
331324
)
332-
->asHasMany([Inflector::camel2id($this->schemaName, '_') . '_id' => $this->schema->getPkName()]);
325+
->asHasMany([Inflector::camel2id($this->schemaName, '_') . '_id' => $this->schema->getPkName()])
326+
->setInverse(Inflector::variablize($this->schemaName));
333327
return;
334328
}
335329
if ($this->schema->isNonDb() && $attribute->isReference()) {
@@ -498,4 +492,21 @@ public function createDbModel(): DbModel
498492
],
499493
]);
500494
}
495+
496+
public function addInverseRelation(
497+
string $relatedClassName,
498+
Attribute $attribute,
499+
PropertySchema $property,
500+
PropertySchema $fkProperty
501+
): void
502+
{
503+
$inverseRelation = Yii::createObject(
504+
AttributeRelation::class,
505+
[$this->schemaName, $this->tableName, $this->schemaName]
506+
)
507+
->asHasOne([$attribute->columnName => $fkProperty->getName()]);
508+
$inverseRelation->setInverse($property->getName());
509+
$this->inverseRelations[$relatedClassName][] = $inverseRelation;
510+
511+
}
501512
}

src/lib/SchemaToDatabase.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,11 @@ public function prepareModels(): array
104104
foreach ($resolvers as $aResolver) {
105105
/** @var AttributeResolver $aResolver */
106106
if ($aResolver->inverseRelations) {
107-
foreach ($aResolver->inverseRelations as $name => $aRelation) {
108-
/** @var AttributeRelation $aRelation */
109-
$models[$name]->inverseRelations[] = $aRelation;
107+
foreach ($aResolver->inverseRelations as $name => $relations) {
108+
foreach ($relations as $relation) {
109+
/** @var AttributeRelation $relation */
110+
$models[$name]->inverseRelations[] = $relation;
111+
}
110112
}
111113
}
112114
}

src/lib/items/AttributeRelation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class AttributeRelation
3434

3535
private bool $selfReference = false;
3636

37-
private bool $inverse = false;
37+
private ?string $inverse = null;
3838

3939
public function __construct(
4040
string $name,
@@ -184,13 +184,13 @@ public function linkToString(): string
184184
);
185185
}
186186

187-
public function setInverse(bool $inverse): AttributeRelation
187+
public function setInverse(string $inverse): AttributeRelation
188188
{
189189
$this->inverse = $inverse;
190190
return $this;
191191
}
192192

193-
public function getInverse(): bool
193+
public function getInverse(): ?string
194194
{
195195
return $this->inverse;
196196
}

tests/specs/issue_fix/25_generate_inverse_relations/index.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ components:
1919
name:
2020
type: string
2121
maxLength: 128
22+
accounts:
23+
type: array
24+
items:
25+
$ref: '#/components/schemas/Account'
2226

2327
Account:
2428
description: Account
@@ -38,6 +42,12 @@ components:
3842
type: string
3943
user:
4044
$ref: '#/components/schemas/User'
45+
user2: # copy of user (not one to many)
46+
$ref: '#/components/schemas/User'
47+
user3: # copy of user (not one to many)
48+
allOf:
49+
- $ref: '#/components/schemas/User'
50+
- x-fk-column-name: user3
4151

4252
paths:
4353
'/account':

0 commit comments

Comments
 (0)