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

+10
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

+12
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

+8
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

+7-2
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

+6
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

+6
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

+12
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

+6
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

+6
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

+12
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
}

tests/specs/fk_col_name/app/models/base/Delivery.php

+6
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ public function rules()
2727
'title_string' => [['title'], 'string'],
2828
];
2929
}
30+
31+
# belongs to relation
32+
public function getWebhook()
33+
{
34+
return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id']);
35+
}
3036
}

tests/specs/fk_col_name/app/models/base/User.php

+6
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ public function rules()
2828
'name_string' => [['name'], 'string'],
2929
];
3030
}
31+
32+
# belongs to relation
33+
public function getWebhook()
34+
{
35+
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id']);
36+
}
3137
}

tests/specs/fk_col_name_index/app/models/base/Delivery.php

+12
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,16 @@ public function rules()
2727
'title_string' => [['title'], 'string'],
2828
];
2929
}
30+
31+
# belongs to relation
32+
public function getWebhook()
33+
{
34+
return $this->hasOne(\app\models\Webhook::class, ['redelivery_of' => 'id']);
35+
}
36+
37+
# belongs to relation
38+
public function getWebhook2()
39+
{
40+
return $this->hasOne(\app\models\Webhook::class, ['rd_abc_2' => 'id']);
41+
}
3042
}

tests/specs/fk_col_name_index/app/models/base/User.php

+6
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ public function rules()
2828
'name_string' => [['name'], 'string'],
2929
];
3030
}
31+
32+
# belongs to relation
33+
public function getWebhook()
34+
{
35+
return $this->hasOne(\app\models\Webhook::class, ['user_id' => 'id']);
36+
}
3137
}

tests/specs/issue_fix/159_bug_giiapi_generated_rules_emailid/maria/models/base/Mailing.php

+6
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ public function rules()
3030
'paymentMethodName_string' => [['paymentMethodName'], 'string'],
3131
];
3232
}
33+
34+
# belongs to relation
35+
public function getContact()
36+
{
37+
return $this->hasOne(\app\models\Contact::class, ['mailing_id' => 'id']);
38+
}
3339
}

tests/specs/issue_fix/162_bug_dollarref_with_x_faker/app/models/base/Invoice.php

+6
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@ public function rules()
2323
{
2424
return [];
2525
}
26+
27+
# belongs to relation
28+
public function getOrder()
29+
{
30+
return $this->hasOne(\app\models\Order::class, ['invoice_id' => 'id']);
31+
}
2632
}

tests/specs/issue_fix/175_bug_allof_with_multiple_dollarrefs/pgsql/models/base/Account.php

+6
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ public function rules()
3030
'paymentMethodName_string' => [['paymentMethodName'], 'string'],
3131
];
3232
}
33+
34+
# belongs to relation
35+
public function getContact()
36+
{
37+
return $this->hasOne(\app\models\Contact::class, ['account_id' => 'id']);
38+
}
3339
}

tests/specs/issue_fix/25_generate_inverse_relations/mysql/models/base/User.php

+18
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,22 @@ public function getAccounts()
3434
{
3535
return $this->hasMany(\app\models\Account::class, ['user_id' => 'id'])->inverseOf('user');
3636
}
37+
38+
# belongs to relation
39+
public function getAccount()
40+
{
41+
return $this->hasOne(\app\models\Account::class, ['user_id' => 'id']);
42+
}
43+
44+
# belongs to relation
45+
public function getAccount2()
46+
{
47+
return $this->hasOne(\app\models\Account::class, ['user2_id' => 'id']);
48+
}
49+
50+
# belongs to relation
51+
public function getAccount3()
52+
{
53+
return $this->hasOne(\app\models\Account::class, ['user3' => 'id']);
54+
}
3755
}

tests/specs/issue_fix/29_extension_fk_column_name_cause_error_in_case_of_column_name_without_underscore/mysql/models/base/User.php

+6
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ public function rules()
2727
'name_string' => [['name'], 'string'],
2828
];
2929
}
30+
31+
# belongs to relation
32+
public function getPost()
33+
{
34+
return $this->hasOne(\app\models\Post::class, ['user' => 'id']);
35+
}
3036
}

tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Animal.php

+6
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ public function rules()
2727
'name_string' => [['name'], 'string'],
2828
];
2929
}
30+
31+
# belongs to relation
32+
public function getInvoice()
33+
{
34+
return $this->hasOne(\app\models\Invoice::class, ['animal_id' => 'id']);
35+
}
3036
}

tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/Fruit.php

+6
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ public function rules()
2727
'name_string' => [['name'], 'string'],
2828
];
2929
}
30+
31+
# belongs to relation
32+
public function getInvoice()
33+
{
34+
return $this->hasOne(\app\models\Invoice::class, ['fruit_id' => 'id']);
35+
}
3036
}

tests/specs/issue_fix/52_bug_dependenton_allof_with_x_faker_false/mysql/models/base/User.php

+12
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,16 @@ public function rules()
2727
'name_string' => [['name'], 'string'],
2828
];
2929
}
30+
31+
# belongs to relation
32+
public function getInvoice()
33+
{
34+
return $this->hasOne(\app\models\Invoice::class, ['user_id' => 'id']);
35+
}
36+
37+
# belongs to relation
38+
public function getInvoice2()
39+
{
40+
return $this->hasOne(\app\models\Invoice::class, ['user_2_id' => 'id']);
41+
}
3042
}

tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/index.yaml

-2
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,3 @@ components:
2929
type: string
3030
address:
3131
$ref: '#/components/schemas/Address'
32-
33-

tests/specs/issue_fix/88_in_case_of_updating_a_model_generator_creates_redundant_inverse_relations/mysql/models/base/Address.php

+6
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,10 @@ public function rules()
2727
'name_string' => [['name'], 'string'],
2828
];
2929
}
30+
31+
# belongs to relation
32+
public function getHuman()
33+
{
34+
return $this->hasOne(\app\models\Human::class, ['address_id' => 'id']);
35+
}
3036
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/90_implement_belongs_to_relations_in_models/index.yaml',
5+
'generateUrls' => false,
6+
'generateModels' => true,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => false,
11+
'generateMigrations' => false,
12+
'generateModelFaker' => false, // `generateModels` must be `true` in order to use `generateModelFaker` as `true`
13+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: \#90
5+
paths:
6+
/:
7+
get:
8+
responses:
9+
'200':
10+
description: The information
11+
12+
components:
13+
schemas:
14+
User:
15+
type: object
16+
properties:
17+
id:
18+
type: integer
19+
20+
Address:
21+
type: object
22+
properties:
23+
id:
24+
type: integer
25+
user:
26+
$ref: '#/components/schemas/User'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
class Address extends \app\models\base\Address
6+
{
7+
8+
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
class User extends \app\models\base\User
6+
{
7+
8+
9+
}
10+

0 commit comments

Comments
 (0)