Yii Gii建立模型


要使用 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 生成器介面,填寫表格欄位。
Yii Gii創建模型

在這裡點選 「Generate」,結果如下所示:

生成結果如下所示:


第2步 - 開啟URL http://localhost:8080/index.php?r=my-user , 然後單擊 「Preview」 和 「Generate」 按鈕。會看到所有使用者的列表。


第3步 - 開啟URL http://localhost:8080/index.php?r=my-user/create , 應該看到一個建立使用者的表單。