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 生成器介面,填寫表單。

Crud Generator Interface

步驟 2 - 然後,點選“預覽”按鈕和“生成”。訪問 URL https://:8080/index.php?r=my-user,您將看到所有使用者的列表。

Click Preview Button

步驟 3 - 開啟 URL https://:8080/index.php?r=my-user/create。您應該會看到一個使用者建立表單。

User Create Form
廣告