Skip to content

Commit e499530

Browse files
committed
Implement
1 parent 71637c8 commit e499530

File tree

5 files changed

+81
-21
lines changed

5 files changed

+81
-21
lines changed

src/generator/default/dbmodel.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,12 @@ public function get<?= $relation->getCamelName() ?>()
103103
<?php endif;?>
104104
}
105105
<?php endforeach; ?>
106+
<?php foreach ($model->inverseRelations as $relationName => $relation): ?>
107+
108+
public function get<?= $relation->getCamelName() ?>()
109+
{
110+
return $this-><?= $relation->getMethod() ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>::class, <?php
111+
echo $relation->linkToString() ?>)->inverseOf('<?= Inflector::variablize($model->getClassName()) ?>');
112+
}
113+
<?php endforeach; ?>
106114
}

src/lib/AttributeResolver.php

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class AttributeResolver
3535
/**
3636
* @var AttributeRelation[]|array
3737
*/
38-
private array $relations = [];
38+
public array $relations = [];
39+
3940
/**
4041
* @var NonDbRelation[]|array
4142
*/
@@ -59,6 +60,11 @@ class AttributeResolver
5960

6061
private ?Config $config;
6162

63+
/**
64+
* @var AttributeRelation[]|array
65+
*/
66+
public array $inverseRelations = [];
67+
6268
public function __construct(string $schemaName, ComponentSchema $schema, JunctionSchemas $junctions, ?Config $config = null)
6369
{
6470
$this->schemaName = $schemaName;
@@ -94,22 +100,7 @@ public function resolve(): DbModel
94100
$this->resolveProperty($property, $isRequired, $nullableValue);
95101
}
96102
}
97-
return Yii::createObject(DbModel::class, [
98-
[
99-
'pkName' => $this->schema->getPkName(),
100-
'name' => $this->schemaName,
101-
'tableName' => $this->tableName,
102-
'description' => $this->schema->getDescription(),
103-
'attributes' => $this->attributes,
104-
'relations' => $this->relations,
105-
'nonDbRelations' => $this->nonDbRelations,
106-
'many2many' => $this->many2many,
107-
'indexes' => $this->prepareIndexes($this->schema->getIndexes()),
108-
//For valid primary keys for junction tables
109-
'junctionCols' => $this->isJunctionSchema ? $this->junctions->junctionCols($this->schemaName) : [],
110-
'isNotDb' => $this->schema->isNonDb(),
111-
],
112-
]);
103+
return $this->createDbModel();
113104
}
114105

115106
/**
@@ -258,6 +249,14 @@ protected function resolveProperty(
258249
$relation->asSelfReference();
259250
}
260251
$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;
261260
}
262261
if (!$property->isReference() && !$property->hasRefItems()) {
263262
[$min, $max] = $property->guessMinMax();
@@ -475,4 +474,28 @@ protected function resolvePropertyRef(PropertySchema $property, Attribute $attri
475474
$this->attributes[$property->getName()] =
476475
$attribute->setFakerStub($this->guessFakerStub($attribute, $fkProperty));
477476
}
477+
478+
/**
479+
* @throws InvalidDefinitionException
480+
* @throws InvalidConfigException
481+
*/
482+
public function createDbModel(): DbModel
483+
{
484+
return Yii::createObject(DbModel::class, [
485+
[
486+
'pkName' => $this->schema->getPkName(),
487+
'name' => $this->schemaName,
488+
'tableName' => $this->tableName,
489+
'description' => $this->schema->getDescription(),
490+
'attributes' => $this->attributes,
491+
'relations' => $this->relations,
492+
'nonDbRelations' => $this->nonDbRelations,
493+
'many2many' => $this->many2many,
494+
'indexes' => $this->prepareIndexes($this->schema->getIndexes()),
495+
//For valid primary keys for junction tables
496+
'junctionCols' => $this->isJunctionSchema ? $this->junctions->junctionCols($this->schemaName) : [],
497+
'isNotDb' => $this->schema->isNonDb(),
498+
],
499+
]);
500+
}
478501
}

src/lib/SchemaToDatabase.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use cebe\openapi\exceptions\TypeErrorException;
1212
use cebe\openapi\exceptions\UnresolvableReferenceException;
1313
use cebe\yii2openapi\lib\exceptions\InvalidDefinitionException;
14+
use cebe\yii2openapi\lib\items\AttributeRelation;
1415
use cebe\yii2openapi\lib\items\DbModel;
1516
use cebe\yii2openapi\lib\items\JunctionSchemas;
1617
use cebe\yii2openapi\lib\openapi\ComponentSchema;
@@ -79,7 +80,7 @@ public function __construct(Config $config)
7980
*/
8081
public function prepareModels(): array
8182
{
82-
$models = [];
83+
$models = $resolvers = [];
8384
$openApi = $this->config->getOpenApi();
8485
$junctions = $this->findJunctionSchemas();
8586
foreach ($openApi->components->schemas as $schemaName => $openApiSchema) {
@@ -93,8 +94,23 @@ public function prepareModels(): array
9394
}
9495
/**@var AttributeResolver $resolver */
9596
$resolver = Yii::createObject(AttributeResolver::class, [$schemaName, $schema, $junctions, $this->config]);
96-
$models[$schemaName] = $resolver->resolve();
97+
98+
// $models[$schemaName] = $resolver->resolve();
99+
$resolvers[$schemaName] = $resolver;
100+
$models[$schemaName] = $resolvers[$schemaName]->resolve();
101+
}
102+
103+
// handle inverse relation
104+
foreach ($resolvers as $aResolver) {
105+
/** @var AttributeResolver $aResolver */
106+
if ($aResolver->inverseRelations) {
107+
foreach ($aResolver->inverseRelations as $name => $aRelation) {
108+
/** @var AttributeRelation $aRelation */
109+
$models[$name]->inverseRelations[] = $aRelation;
110+
}
111+
}
97112
}
113+
98114
foreach ($models as $model) {
99115
foreach ($model->many2many as $relation) {
100116
if (isset($models[$relation->viaModelName])) {

src/lib/items/AttributeRelation.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,15 @@ class AttributeRelation
2727

2828
/**
2929
* hasOne/hasMany
30-
**/
30+
*/
3131
private ?string $method;
3232

3333
private array $link;
3434

3535
private bool $selfReference = false;
3636

37+
private bool $inverse = false;
38+
3739
public function __construct(
3840
string $name,
3941
?string $tableName = null,
@@ -181,4 +183,15 @@ public function linkToString(): string
181183
preg_replace('~\s+~', '', VarDumper::export($this->getLink()))
182184
);
183185
}
186+
187+
public function setInverse(bool $inverse): AttributeRelation
188+
{
189+
$this->inverse = $inverse;
190+
return $this;
191+
}
192+
193+
public function getInverse(): bool
194+
{
195+
return $this->inverse;
196+
}
184197
}

src/lib/items/DbModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class DbModel extends BaseObject
5151
*/
5252
public array $relations = [];
5353

54-
/***
54+
/**
5555
* @var array|NonDbRelation[] non-db relations
5656
*/
5757
public array $nonDbRelations = [];

0 commit comments

Comments
 (0)