Skip to content

Commit 5104312

Browse files
committed
Refactor
1 parent a168e07 commit 5104312

File tree

3 files changed

+29
-32
lines changed

3 files changed

+29
-32
lines changed

TODO.taskpaper

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
TODO.taskpaper
22

33
### Generate inverse relations #25
4-
create failing test
5-
implement the solution
4+
create failing test @done (24-08-25 15:25)
5+
implement the solution @done (24-08-25 15:25)
66
☐ fix failing tests if any
77
☐ resolve TODOs if any
88
☐ review PR

src/lib/AttributeResolver.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,23 @@ public function resolve(): DbModel
100100
$this->resolveProperty($property, $isRequired, $nullableValue);
101101
}
102102
}
103-
return $this->createDbModel();
103+
104+
return Yii::createObject(DbModel::class, [
105+
[
106+
'pkName' => $this->schema->getPkName(),
107+
'name' => $this->schemaName,
108+
'tableName' => $this->tableName,
109+
'description' => $this->schema->getDescription(),
110+
'attributes' => $this->attributes,
111+
'relations' => $this->relations,
112+
'nonDbRelations' => $this->nonDbRelations,
113+
'many2many' => $this->many2many,
114+
'indexes' => $this->prepareIndexes($this->schema->getIndexes()),
115+
//For valid primary keys for junction tables
116+
'junctionCols' => $this->isJunctionSchema ? $this->junctions->junctionCols($this->schemaName) : [],
117+
'isNotDb' => $this->schema->isNonDb(),
118+
],
119+
]);
104120
}
105121

106122
/**
@@ -472,29 +488,8 @@ protected function resolvePropertyRef(PropertySchema $property, Attribute $attri
472488
}
473489

474490
/**
475-
* @throws InvalidDefinitionException
476491
* @throws InvalidConfigException
477492
*/
478-
public function createDbModel(): DbModel
479-
{
480-
return Yii::createObject(DbModel::class, [
481-
[
482-
'pkName' => $this->schema->getPkName(),
483-
'name' => $this->schemaName,
484-
'tableName' => $this->tableName,
485-
'description' => $this->schema->getDescription(),
486-
'attributes' => $this->attributes,
487-
'relations' => $this->relations,
488-
'nonDbRelations' => $this->nonDbRelations,
489-
'many2many' => $this->many2many,
490-
'indexes' => $this->prepareIndexes($this->schema->getIndexes()),
491-
//For valid primary keys for junction tables
492-
'junctionCols' => $this->isJunctionSchema ? $this->junctions->junctionCols($this->schemaName) : [],
493-
'isNotDb' => $this->schema->isNonDb(),
494-
],
495-
]);
496-
}
497-
498493
public function addInverseRelation(
499494
string $relatedClassName,
500495
Attribute $attribute,

src/lib/SchemaToDatabase.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ public function __construct(Config $config)
8080
*/
8181
public function prepareModels(): array
8282
{
83-
$models = $resolvers = [];
83+
/** @var DbModel[] $models */
84+
$models = [];
85+
86+
/** @var AttributeResolver[] $resolvers */
87+
$resolvers = [];
88+
8489
$openApi = $this->config->getOpenApi();
8590
$junctions = $this->findJunctionSchemas();
8691
foreach ($openApi->components->schemas as $schemaName => $openApiSchema) {
@@ -102,13 +107,10 @@ public function prepareModels(): array
102107

103108
// handle inverse relation
104109
foreach ($resolvers as $aResolver) {
105-
/** @var AttributeResolver $aResolver */
106-
if ($aResolver->inverseRelations) {
107-
foreach ($aResolver->inverseRelations as $name => $relations) {
108-
foreach ($relations as $relation) {
109-
/** @var AttributeRelation $relation */
110-
$models[$name]->inverseRelations[] = $relation;
111-
}
110+
foreach ($aResolver->inverseRelations as $name => $relations) {
111+
foreach ($relations as $relation) {
112+
/** @var AttributeRelation $relation */
113+
$models[$name]->inverseRelations[] = $relation;
112114
}
113115
}
114116
}

0 commit comments

Comments
 (0)