Skip to content

Commit 4d32a93

Browse files
committed
Merge branch '90-implement-belongs-to-relations-in-models' of github.com:php-openapi/yii2-openapi into fix-conflicting-pull-requests
2 parents dd34cdb + c4c9467 commit 4d32a93

File tree

41 files changed

+406
-4
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+406
-4
lines changed

src/generator/default/dbmodel.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,14 @@ public function get<?= $relation->getCamelName() ?>()
146146
<?php endif;?>
147147
}
148148
<?php endforeach; ?>
149+
<?php $i = 1; $usedRelationNames = [];
150+
foreach ($model->belongsToRelations as $relationName => $relation): ?><?php $number = in_array($relation->getCamelName(), $usedRelationNames) ? $i : '' ?>
151+
152+
# belongs to relation
153+
public function get<?= $relation->getCamelName() . ($number) ?>()
154+
{
155+
return $this-><?= $relation->getMethod() ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>::class, <?php
156+
echo $relation->linkToString() ?>);
157+
}
158+
<?php $i++; $usedRelationNames[] = $relation->getCamelName(); endforeach; ?>
149159
}

src/lib/AttributeResolver.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ class AttributeResolver
3737
*/
3838
public array $relations = [];
3939

40+
/**
41+
* @var array keys contains class names and value contains array of belongs to relations
42+
*/
43+
public array $belongsToRelations = [];
44+
4045
/**
4146
* @var NonDbRelation[]|array
4247
*/
@@ -273,6 +278,13 @@ protected function resolveProperty(
273278
$relation->onDeleteFkConstraint = $property->onDeleteFkConstraint;
274279
if ($property->isRefPointerToSelf()) {
275280
$relation->asSelfReference();
281+
} else { # belongs to relations https://github.com/php-openapi/yii2-openapi/issues/90
282+
$belongsToRelation = Yii::createObject(
283+
AttributeRelation::class,
284+
[$this->schemaName, $this->tableName, $this->schemaName]
285+
)
286+
->asHasOne([$attribute->columnName => $fkProperty->getName()]);
287+
$this->belongsToRelations[$property->getRefClassName()][] = $belongsToRelation;
276288
}
277289
$this->relations[$property->getName()] = $relation;
278290
}

src/lib/SchemaToDatabase.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ public function prepareModels(): array
110110
$models[$schemaName] = $resolvers[$schemaName]->resolve();
111111
}
112112

113+
// handle belongs to relation
114+
foreach ($resolvers as $aResolver) {
115+
foreach ($aResolver->belongsToRelations as $name => $relations) {
116+
/** @var AttributeRelation[] $relations */
117+
$models[$name]->belongsToRelations = [...$models[$name]->belongsToRelations, ...$relations];
118+
}
119+
}
120+
113121
foreach ($models as $model) {
114122
foreach ($model->many2many as $relation) {
115123
if (isset($models[$relation->viaModelName])) {

src/lib/items/DbModel.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ class DbModel extends BaseObject
8686
public bool $descriptionIsComment = false;
8787

8888
/**
89-
* @var array Automatically generated scenarios from the model 'x-scenarios'.
89+
* @var AttributeRelation[] belongs to relations
9090
*/
91-
private $_scenarios;
91+
public array $belongsToRelations = [];
9292

9393
/**
9494
* @var bool
@@ -98,6 +98,11 @@ class DbModel extends BaseObject
9898
*/
9999
public $drop = false;
100100

101+
/**
102+
* @var array Automatically generated scenarios from the model 'x-scenarios'.
103+
*/
104+
private $_scenarios;
105+
101106
public function getTableAlias(): string
102107
{
103108
return '{{%' . $this->tableName . '}}';

tests/specs/blog/models/base/Category.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@ public function getPosts()
3838
{
3939
return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
4040
}
41+
42+
# belongs to relation
43+
public function getPost()
44+
{
45+
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id']);
46+
}
4147
}

tests/specs/blog/models/base/Post.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,10 @@ public function getComments()
6161
{
6262
return $this->hasMany(\app\models\Comment::class, ['post_id' => 'uid'])->inverseOf('post');
6363
}
64+
65+
# belongs to relation
66+
public function getComment()
67+
{
68+
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'uid']);
69+
}
6470
}

tests/specs/blog/models/base/User.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,16 @@ public function rules()
4444
'email_unique' => [['email'], 'unique'],
4545
];
4646
}
47+
48+
# belongs to relation
49+
public function getPost()
50+
{
51+
return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id']);
52+
}
53+
54+
# belongs to relation
55+
public function getComment()
56+
{
57+
return $this->hasOne(\app\models\Comment::class, ['author_id' => 'id']);
58+
}
4759
}

tests/specs/blog_v2/models/base/Category.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@ public function getPosts()
3838
{
3939
return $this->hasMany(\app\models\Post::class, ['category_id' => 'id'])->inverseOf('category');
4040
}
41+
42+
# belongs to relation
43+
public function getPost()
44+
{
45+
return $this->hasOne(\app\models\Post::class, ['category_id' => 'id']);
46+
}
4147
}

tests/specs/blog_v2/models/base/Post.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,10 @@ public function getTags()
7373
return $this->hasMany(\app\models\Tag::class, ['id' => 'tag_id'])
7474
->viaTable('posts2tags', ['post_id' => 'id']);
7575
}
76+
77+
# belongs to relation
78+
public function getComment()
79+
{
80+
return $this->hasOne(\app\models\Comment::class, ['post_id' => 'id']);
81+
}
7682
}

tests/specs/blog_v2/models/base/User.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ public function rules()
4747
'email_unique' => [['email'], 'unique'],
4848
];
4949
}
50+
51+
# belongs to relation
52+
public function getPost()
53+
{
54+
return $this->hasOne(\app\models\Post::class, ['created_by_id' => 'id']);
55+
}
56+
57+
# belongs to relation
58+
public function getComment()
59+
{
60+
return $this->hasOne(\app\models\Comment::class, ['user_id' => 'id']);
61+
}
5062
}

0 commit comments

Comments
 (0)