要使用 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創建控制器
