model.php 2.12 KB
Newer Older
Qiang Xue committed
1 2 3 4
<?php
/**
 * This is the template for generating the model class of a specified table.
 */
Qiang Xue committed
5

6 7 8 9 10 11 12 13 14
/* @var $this yii\web\View */
/* @var $generator yii\gii\generators\model\Generator */
/* @var $tableName string full table name */
/* @var $className string class name */
/* @var $tableSchema yii\db\TableSchema */
/* @var $labels string[] list of attribute labels (name => label) */
/* @var $rules string[] list of validation rules */
/* @var $relations array list of relations (name => relation declaration) */

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

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

20 21
use Yii;

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

    /**
     * @return \yii\db\Connection the database connection used by this AR class.
     */
    public static function getDb()
    {
        return Yii::$app->get('<?= $generator->db ?>');
    }
<?php endif; ?>
Qiang Xue committed
54

55 56 57 58 59
    /**
     * @inheritdoc
     */
    public function rules()
    {
60
        return [<?= "\n            " . implode(",\n            ", $rules) . "\n        " ?>];
61
    }
62

63 64 65 66 67 68
    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
Qiang Xue committed
69
<?php foreach ($labels as $name => $label): ?>
70
            <?= "'$name' => " . $generator->generateString($label) . ",\n" ?>
Qiang Xue committed
71
<?php endforeach; ?>
72 73
        ];
    }
Qiang Xue committed
74 75
<?php foreach ($relations as $name => $relation): ?>

76 77 78 79 80 81 82
    /**
     * @return \yii\db\ActiveQuery
     */
    public function get<?= $name ?>()
    {
        <?= $relation[0] . "\n" ?>
    }
Qiang Xue committed
83
<?php endforeach; ?>
Qiang Xue committed
84
}