要使用 Gii 工具来创建模型,可参考以下代码 −
<?php
namespace app\models;
use app\components\UppercaseBehavior;
use Yii;
/**
* This is the model class for table "user".
*
* @property integer $id
* @property string $name
* @property string $email
*/
class MyUser extends \yii\db\ActiveRecord {
/**
* @inheritdoc
*/
public static function tableName() {
return 'user';
}
/**
* @inheritdoc
*/
public function rules() {
return [
[['name', 'email'], 'string', 'max' => 255]
];
}
/**
* @inheritdoc
*/
public function attributeLabels() {
return [
'id' => 'ID',
'name' => 'Name',
'email' => 'Email',
];
}
}
?>
生成CRUD
现在为 MyUser 模型产生 CRUD 操作。
第1步 - 打开 CRUD 生成器界面,填写表格字段。
在这里点击 “Generate”,结果如下所示:

生成结果如下所示:


第3步 - 打开URL http://localhost:8080/index.php?r=my-user/create , 应该看到一个创建用户的表单。


生成结果如下所示:

第2步 - 打开URL http://localhost:8080/index.php?r=my-user , 然后单击 “Preview” 和 “Generate” 按钮。会看到所有用户的列表。

第3步 - 打开URL http://localhost:8080/index.php?r=my-user/create , 应该看到一个创建用户的表单。

上一篇:
Yii Gii使用
下一篇:
Yii Gii创建控制器
