forked from cebe/yii2-openapi
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdbmodel.php
More file actions
161 lines (137 loc) · 5.42 KB
/
dbmodel.php
File metadata and controls
161 lines (137 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
/**
* @var \cebe\yii2openapi\lib\items\DbModel $model
* @var string $namespace
* @var string $relationNamespace
**/
use yii\helpers\Inflector;
use yii\helpers\VarDumper;
?>
<?= '<?php' ?>
/**
* This file is generated by Gii, do not change manually!
*/
namespace <?= $namespace ?>;
/**
*<?= $model->getModelClassDescription() ?>
*
<?php foreach ($model->dbAttributes() as $attribute): ?>
* @property <?= $attribute->getPropertyAnnotation() ?>
<?php endforeach; ?>
*
<?php foreach ($model->relations as $relationName => $relation): ?>
<?php if ($relation->isHasOne()):?>
* @property \<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?> $<?= Inflector::variablize($relation->getName()) ?>
<?php else:?>
* @property array|\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>[] $<?= Inflector::variablize($relation->getName()) ?>
<?php endif?>
<?php endforeach; ?>
<?php foreach ($model->nonDbRelations as $relationName => $relation): ?>
<?php if ($relation->isHasOne()):?>
* @property \<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?> $<?= Inflector::variablize($relation->getName()) ?>
<?php else:?>
* @property array|\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>[] $<?= Inflector::variablize($relation->getName()) ?>
<?php endif?>
<?php endforeach; ?>
<?php foreach ($model->many2many as $relation): ?>
* @property array|\<?= trim($relationNamespace, '\\') ?>\<?= $relation->relatedClassName ?>[] $<?= Inflector::variablize($relation->name) ?>
<?php endforeach; ?>
*/
abstract class <?= $model->getClassName() ?> extends \yii\db\ActiveRecord
{
<?php if ($scenarios = $model->getScenarios()):
foreach ($scenarios as $scenario): ?>
/**
*<?= $scenario['description'] ?>
*/
public const <?= $scenario['const'] ?> = '<?= $scenario['name'] ?>';
<?php endforeach; ?>
<?php endif ?>
<?php if (count($model->virtualAttributes())):?>
protected $virtualAttributes = ['<?=implode("', '", array_map(function ($attr) {
return $attr->columnName;
}, $model->virtualAttributes()))?>'];
<?php foreach ($model->virtualAttributes() as $attribute): ?>
/**
* @var <?=$attribute->phpType.PHP_EOL?>
*/
public $<?= $attribute->columnName ?>;
<?php endforeach; ?>
<?php endif;?>
public static function tableName()
{
return <?= var_export($model->getTableAlias()) ?>;
}
<?php if ($scenarios): ?>
/**
* Automatically generated scenarios from the model 'x-scenarios'.
* @return array a list of scenarios and the corresponding active attributes.
*/
public function scenarios()
{
$parentScenarios = parent::scenarios();
/**
* Each scenario is assigned attributes as in the 'default' scenario.
* The advantage is that the scenario can be used immediately.
* This can be overridden in the child model if needed.
*/
$default = $parentScenarios[self::SCENARIO_DEFAULT];
return [
<?php foreach ($scenarios as $scenario): ?>
self::<?= $scenario['const'] ?> => $default,
<?php endforeach; ?>
/**
* The 'default' scenario and all scenarios mentioned in the rules() using 'on' and 'except'
* are automatically included in the scenarios() function for the model.
*/
...$parentScenarios,
];
}
<?php endif;?>
<?php if (count($model->virtualAttributes())):?>
public function attributes()
{
return array_merge(parent::attributes(), $this->virtualAttributes);
}
public function afterFind()
{
parent::afterFind();
foreach ($this->virtualAttributes as $attr) {
$this->$attr = $this->getAttribute($attr);
}
}
<?php endif;?>
public function rules()
{
return <?=$model->getValidationRules()?>;
}
<?php foreach ($model->relations as $relationName => $relation): ?>
public function get<?= $relation->getCamelName() ?>()
{
return $this-><?= $relation->getMethod() ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>::class, <?php
echo $relation->linkToString()?>)<?= $relation->getInverse() ? '->inverseOf(\''.$relation->getInverse().'\')' : '' ?>;
}
<?php endforeach; ?>
<?php foreach ($model->many2many as $relation): ?>
public function get<?= $relation->getCamelName() ?>()
{
return $this->hasMany(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->relatedClassName ?>::class, <?php
echo $relation->linkToString($relation->link)?>)
<?php if (!$relation->hasViaModel):?>
->viaTable(<?=VarDumper::export($relation->viaTableName)?>, <?=$relation->linkToString($relation->viaLink)?>);
<?php else:?>
->via('<?=lcfirst($relation->getViaRelationName())?>');
<?php endif;?>
}
<?php endforeach; ?>
<?php $usedRelationNames = [];
foreach ($model->belongsToRelations as $relationName => $relation): ?><?php
$i = in_array($relation->getCamelName(), $usedRelationNames) ? array_count_values($usedRelationNames)[$relation->getCamelName()]+1 : 1 ?>
# belongs to relation
public function get<?= $relation->getCamelName() . ($i === 1 ? '' : $i) ?>()
{
return $this-><?= $relation->getMethod() ?>(\<?= trim($relationNamespace, '\\') ?>\<?= $relation->getClassName() ?>::class, <?php
echo $relation->linkToString() ?>);
}
<?php $usedRelationNames[] = $relation->getCamelName(); endforeach; ?>
}