model.php 1.63 KB
Newer Older
Qiang Xue committed
1 2 3
<?php
/**
 * This is the template for generating the model class of a specified table.
Qiang Xue committed
4
 *
Alexander Makarov committed
5
 * @var yii\web\View $this
Qiang Xue committed
6
 * @var yii\gii\generators\model\Generator $generator
Qiang Xue committed
7 8
 * @var string $tableName full table name
 * @var string $className class name
Qiang Xue committed
9
 * @var yii\db\TableSchema $tableSchema
Qiang Xue committed
10 11 12
 * @var string[] $labels list of attribute labels (name=>label)
 * @var string[] $rules list of validation rules
 * @var array $relations list of relations (name=>relation declaration)
Qiang Xue committed
13
 */
Qiang Xue committed
14 15

echo "<?php\n";
Qiang Xue committed
16
?>
Qiang Xue committed
17

Alexander Makarov committed
18
namespace <?= $generator->ns ?>;
Qiang Xue committed
19 20

/**
Alexander Makarov committed
21
 * This is the model class for table "<?= $tableName ?>".
Qiang Xue committed
22
 *
Qiang Xue committed
23
<?php foreach ($tableSchema->columns as $column): ?>
Alexander Makarov committed
24
 * @property <?= "{$column->phpType} \${$column->name}\n" ?>
Qiang Xue committed
25
<?php endforeach; ?>
Qiang Xue committed
26 27 28
<?php if (!empty($relations)): ?>
 *
<?php foreach ($relations as $name => $relation): ?>
Alexander Makarov committed
29
 * @property <?= $relation[1] . ($relation[2] ? '[]' : '') . ' $' . lcfirst($name) . "\n" ?>
Qiang Xue committed
30 31
<?php endforeach; ?>
<?php endif; ?>
Qiang Xue committed
32
 */
Alexander Makarov committed
33
class <?= $className ?> extends <?= '\\' . ltrim($generator->baseClass, '\\') . "\n" ?>
Qiang Xue committed
34 35
{
	/**
Qiang Xue committed
36
	 * @inheritdoc
Qiang Xue committed
37
	 */
Qiang Xue committed
38
	public static function tableName()
Qiang Xue committed
39
	{
Alexander Makarov committed
40
		return '<?= $tableName ?>';
Qiang Xue committed
41 42
	}

43
	/**
Qiang Xue committed
44
	 * @inheritdoc
45 46 47
	 */
	public function rules()
	{
Alexander Makarov committed
48
		return [<?= "\n\t\t\t" . implode(",\n\t\t\t", $rules) . "\n\t\t" ?>];
49 50
	}

Qiang Xue committed
51
	/**
Qiang Xue committed
52
	 * @inheritdoc
Qiang Xue committed
53 54 55
	 */
	public function attributeLabels()
	{
Alexander Makarov committed
56
		return [
Qiang Xue committed
57
<?php foreach ($labels as $name => $label): ?>
Alexander Makarov committed
58
			<?= "'$name' => '" . addslashes($label) . "',\n" ?>
Qiang Xue committed
59
<?php endforeach; ?>
Alexander Makarov committed
60
		];
Qiang Xue committed
61
	}
Qiang Xue committed
62 63 64
<?php foreach ($relations as $name => $relation): ?>

	/**
65
	 * @return \yii\db\ActiveQuery
Qiang Xue committed
66
	 */
Alexander Makarov committed
67
	public function get<?= $name ?>()
Qiang Xue committed
68
	{
Alexander Makarov committed
69
		<?= $relation[0] . "\n" ?>
Qiang Xue committed
70 71
	}
<?php endforeach; ?>
Qiang Xue committed
72
}