{"id":23843,"date":"2024-11-21T11:44:09","date_gmt":"2024-11-21T03:44:09","guid":{"rendered":"https:\/\/fwq.ai\/blog\/23843\/"},"modified":"2024-11-21T11:44:09","modified_gmt":"2024-11-21T03:44:09","slug":"%e4%bd%bf%e7%94%a8yii%e6%a1%86%e6%9e%b6%e5%88%9b%e5%bb%ba%e7%94%b5%e5%bd%b1%e7%bd%91%e7%ab%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/23843\/","title":{"rendered":"\u4f7f\u7528Yii\u6846\u67b6\u521b\u5efa\u7535\u5f71\u7f51\u7ad9"},"content":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u666e\u53ca\u4ee5\u53ca\u4eba\u4eec\u5bf9\u7535\u5f71\u7684\u70ed\u7231\uff0c\u7535\u5f71\u7f51\u7ad9\u6210\u4e3a\u4e86\u4e00\u4e2a\u53d7\u6b22\u8fce\u7684\u7f51\u7ad9\u7c7b\u578b\u3002\u5728\u521b\u5efa\u4e00\u4e2a\u7535\u5f71\u7f51\u7ad9\u65f6\uff0c\u4e00\u4e2a\u597d\u7684\u6846\u67b6\u662f\u975e\u5e38\u5fc5\u8981\u7684\u3002yii\u6846\u67b6\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684php\u6846\u67b6\uff0c\u6613\u4e8e\u4f7f\u7528\u4e14\u5177\u6709\u51fa\u8272\u7684\u6027\u80fd\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528yii\u6846\u67b6\u521b\u5efa\u4e00\u4e2a\u7535\u5f71\u7f51\u7ad9\u3002<\/p>\n<ol>\n<li>\u5b89\u88c5Yii\u6846\u67b6<\/li>\n<\/ol>\n<p>\u5728\u4f7f\u7528Yii\u6846\u67b6\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u6846\u67b6\u3002\u5b89\u88c5Yii\u6846\u67b6\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728\u7ec8\u7aef\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>composer create-project yiisoft\/yii2-app-basic<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8be5\u547d\u4ee4\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Yii2\u5e94\u7528\u7a0b\u5e8f\u3002\u73b0\u5728\u4f60\u5df2\u7ecf\u51c6\u5907\u597d\u5f00\u59cb\u521b\u5efa\u4f60\u7684\u7535\u5f71\u7f51\u7ad9\u4e86\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u6570\u636e\u5e93\u548c\u8868\u683c<\/li>\n<\/ol>\n<p>Yii\u6846\u67b6\u63d0\u4f9b\u4e86ActiveRecord\uff0c\u8fd9\u662f\u4e00\u79cd\u4f7f\u64cd\u4f5c\u6570\u636e\u5e93\u53d8\u5f97\u5bb9\u6613\u7684\u65b9\u5f0f\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amovies\u7684\u6570\u636e\u8868\uff0c\u8be5\u8868\u5305\u542b\u7535\u5f71ID\u3001\u6807\u9898\u3001\u5bfc\u6f14\u3001\u6f14\u5458\u3001\u5e74\u4efd\u3001\u7c7b\u578b\u548c\u8bc4\u5206\u7b49\u4fe1\u606f\u3002\u8981\u521b\u5efa\u8868\uff0c\u8bf7\u5728\u7ec8\u7aef\u4e2d\u8fdb\u5165\u5e94\u7528\u7a0b\u5e8f\u6839\u76ee\u5f55\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php yii migrate\/create create_movies_table<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u7136\u540e\u5c06\u751f\u6210\u7684\u8fc1\u79fb\u6587\u4ef6\u7f16\u8f91\u4e3a\u4ee5\u4e0b\u5185\u5bb9\uff1a<\/p>\n<pre>&lt;?php use yiidbMigration;\n\n\/**\n * Handles the creation of table `{{%movies}}`.\n *\/\nclass m210630_050401_create_movies_table extends Migration\n{\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function safeUp()\n    {\n        $this-&gt;createTable('{{%movies}}', [\n            'id' =&amp;gt; $this-&amp;gt;primaryKey(),\n            'title' =&amp;gt; $this-&amp;gt;string()-&amp;gt;notNull(),\n            'director' =&amp;gt; $this-&amp;gt;string()-&amp;gt;notNull(),\n            'actors' =&amp;gt; $this-&amp;gt;text()-&amp;gt;notNull(),\n            'year' =&amp;gt; $this-&amp;gt;integer()-&amp;gt;notNull(),\n            'genre' =&amp;gt; $this-&amp;gt;string()-&amp;gt;notNull(),\n            'rating' =&amp;gt; $this-&amp;gt;decimal(3,1)-&amp;gt;notNull(),\n        ]);\n    }\n\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function safeDown()\n    {\n        $this-&amp;gt;dropTable('{{%movies}}');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u73b0\u5728\u8fd0\u884c\u8fc1\u79fb\u4ee5\u521b\u5efamovies\u6570\u636e\u8868\u3002<\/p>\n<pre>php yii migrate<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521b\u5efa\u7535\u5f71\u6a21\u578b<\/li>\n<\/ol>\n<p>\u5728Yii\u6846\u67b6\u4e2d\uff0c\u4f7f\u7528ActiveRecord\u975e\u5e38\u5bb9\u6613\u5b9a\u4e49\u6570\u636e\u8868\u7684\u6a21\u578b\u3002\u6211\u4eec\u53ef\u4ee5\u5728models\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aMovie\u7684\u6a21\u578b\uff0c\u5e76\u5728\u6a21\u578b\u5b9a\u4e49\u4e2d\u6307\u5b9a\u8868\u683c\u540d\u548c\u5b57\u6bb5\u540d\u3002<\/p>\n<pre>&lt;?php namespace appmodels;\n\nuse yiidbActiveRecord;\n\nclass Movie extends ActiveRecord\n{\n    \/**\n     * {@inheritdoc}\n     *\/\n    public static function tableName()\n    {\n        return '{{%movies}}';\n    }\n\n    \/**\n     * {@inheritdoc}\n     *\/\n    public function rules()\n    {\n        return [\n            [['title', 'director', 'actors', 'year', 'genre', 'rating'], 'required'],\n            [['year'], 'integer'],\n            [['rating'], 'number'],\n            [['actors'], 'string'],\n            [['title', 'director', 'genre'], 'string', 'max' =&gt; 255],\n        ];\n    }\n\n    \/**\n    * {@inheritdoc}\n    *\/\n    public function attributeLabels()\n    {\n        return [\n            'id' =&amp;gt; 'ID',\n            'title' =&amp;gt; 'Title',\n            'director' =&amp;gt; 'Director',\n            'actors' =&amp;gt; 'Actors',\n            'year' =&amp;gt; 'Year',\n            'genre' =&amp;gt; 'Genre',\n            'rating' =&amp;gt; 'Rating'\n        ];\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u521b\u5efa\u7535\u5f71\u63a7\u5236\u5668<\/li>\n<\/ol>\n<p>\u7535\u5f71\u63a7\u5236\u5668\u5c06\u8d1f\u8d23\u5904\u7406\u6709\u5173\u7535\u5f71\u7684\u6240\u6709\u8bf7\u6c42\uff0c\u4f8b\u5982\u6dfb\u52a0\u3001\u7f16\u8f91\u3001\u5220\u9664\u548c\u663e\u793a\u7535\u5f71\u5217\u8868\u7b49\u8bf7\u6c42\u3002\u6211\u4eec\u53ef\u4ee5\u5728controllers\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3aMovieController\u7684\u63a7\u5236\u5668\uff0c\u5e76\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>&lt;?php namespace appcontrollers;\n\nuse Yii;\nuse yiiwebController;\nuse appmodelsMovie;\n\nclass MovieController extends Controller\n{\n    \/**\n     * Shows all movies.\n     *\n     * @return string\n     *\/\n    public function actionIndex()\n    {\n        $movies = Movie::find()-&gt;all();\n        return $this-&amp;gt;render('index', ['movies' =&amp;gt; $movies]);\n    }\n\n    \/**\n     * Creates a new movie.\n     *\n     * @return string|yiiwebResponse\n     *\/\n    public function actionCreate()\n    {\n        $model = new Movie();\n\n        if ($model-&amp;gt;load(Yii::$app-&amp;gt;request-&amp;gt;post()) &amp;amp;&amp;amp; $model-&amp;gt;save()) {\n            return $this-&amp;gt;redirect(['index']);\n        }\n\n        return $this-&amp;gt;render('create', [\n            'model' =&amp;gt; $model,\n        ]);\n    }\n\n    \/**\n     * Updates an existing movie.\n     *\n     * @param integer $id\n     * @return string|yiiwebResponse\n     * @throws yiiwebNotFoundHttpException\n     *\/\n    public function actionUpdate($id)\n    {\n        $model = $this-&amp;gt;findModel($id);\n\n        if ($model-&amp;gt;load(Yii::$app-&amp;gt;request-&amp;gt;post()) &amp;amp;&amp;amp; $model-&amp;gt;save()) {\n            return $this-&amp;gt;redirect(['index']);\n        }\n\n        return $this-&amp;gt;render('update', [\n            'model' =&amp;gt; $model,\n        ]);\n    }\n\n    \/**\n     * Deletes an existing movie.\n     *\n     * @param integer $id\n     * @return yiiwebResponse\n     * @throws yiiwebNotFoundHttpException\n     *\/\n    public function actionDelete($id)\n    {\n        $this-&amp;gt;findModel($id)-&amp;gt;delete();\n\n        return $this-&amp;gt;redirect(['index']);\n    }\n\n    \/**\n     * Finds the Movie model based on its primary key value.\n     * If the model is not found, a 404 HTTP exception will be thrown.\n     *\n     * @param integer $id\n     * @return ppmodelsMovie\n     * @throws NotFoundHttpException if the model cannot be found\n     *\/\n    protected function findModel($id)\n    {\n        if (($model = Movie::findOne($id)) !== null) {\n            return $model;\n        }\n\n        throw new NotFoundHttpException('The requested page does not exist.');\n    }\n}<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5176\u4e2d\uff0cactionIndex\u65b9\u6cd5\u5c06\u663e\u793a\u6240\u6709\u7535\u5f71\u7684\u5217\u8868\uff0cactionCreate\u548cactionUpdate\u65b9\u6cd5\u5c06\u7528\u4e8e\u521b\u5efa\u548c\u7f16\u8f91\u7535\u5f71\uff0cactionDelete\u65b9\u6cd5\u5c06\u5220\u9664\u7535\u5f71\u3002<\/p>\n<ol>\n<li>\u521b\u5efa\u7535\u5f71\u89c6\u56fe<\/li>\n<\/ol>\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u9700\u8981\u521b\u5efa\u89c6\u56fe\u6587\u4ef6\u6765\u663e\u793a\u7535\u5f71\u5217\u8868\u3001\u6dfb\u52a0\u7535\u5f71\u548c\u7f16\u8f91\u7535\u5f71\u7684\u8868\u5355\u3002\u5c06\u89c6\u56fe\u6587\u4ef6\u5b58\u50a8\u5728views\/movie\u76ee\u5f55\u4e2d\u3002<\/p>\n<ul>\n<li>index.php &#8211; \u7528\u4e8e\u663e\u793a\u7535\u5f71\u5217\u8868<\/li>\n<\/ul>\n<pre>&lt;?php use yiihelpersHtml;\nuse yiigridGridView;\n\n\/* @var $this yiiwebView *\/\n\/* @var $movies appmodelsMovie[] *\/\n\n$this-&gt;title = 'Movies';\n$this-&amp;gt;params['breadcrumbs'][] = $this-&amp;gt;title;\n?&amp;gt;\n\n&lt;h1&gt;= Html::encode($this-&amp;gt;title) ?&amp;gt;&lt;\/h1&gt;\n\n&lt;p&gt;\n    = Html::a('Create Movie', ['create'], ['class' =&amp;gt; 'btn btn-success']) ?&amp;gt;\n&lt;\/p&gt;\n\n= GridView::widget([\n    'dataProvider' =&amp;gt; new yiidataArrayDataProvider([\n        'allModels' =&amp;gt; $movies,\n        'sort' =&amp;gt; [\n            'attributes' =&amp;gt; [\n                'title',\n                'director',\n                'year',\n                'genre',\n                'rating',\n            ],\n        ],\n    ]),\n    'columns' =&amp;gt; [\n        ['class' =&amp;gt; 'yiigridSerialColumn'],\n\n        'title',\n        'director',\n        'actors:ntext',\n        'year',\n        'genre',\n        'rating',\n\n        ['class' =&amp;gt; 'yiigridActionColumn'],\n    ],\n]); ?&amp;gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>create.php &#8211; \u7528\u4e8e\u521b\u5efa\u65b0\u7684\u7535\u5f71<\/li>\n<\/ul>\n<pre>&lt;?php use yiihelpersHtml;\nuse yiiwidgetsActiveForm;\n\n\/* @var $this yiiwebView *\/\n\/* @var $model appmodelsMovie *\/\n\n$this-&gt;title = 'Create Movie';\n$this-&amp;gt;params['breadcrumbs'][] = ['label' =&amp;gt; 'Movies', 'url' =&amp;gt; ['index']];\n$this-&amp;gt;params['breadcrumbs'][] = $this-&amp;gt;title;\n?&amp;gt;\n&lt;h1&gt;= Html::encode($this-&amp;gt;title) ?&amp;gt;&lt;\/h1&gt;\n\n&lt;div class=\"movie-form\"&gt;\n\n    &lt;?php $form = ActiveForm::begin(); ?&gt;= $form-&amp;gt;field($model, 'title')-&amp;gt;textInput(['maxlength' =&amp;gt; true]) ?&amp;gt;\n\n    = $form-&amp;gt;field($model, 'director')-&amp;gt;textInput(['maxlength' =&amp;gt; true]) ?&amp;gt;\n\n    = $form-&amp;gt;field($model, 'actors')-&amp;gt;textarea(['rows' =&amp;gt; 6]) ?&amp;gt;\n\n    = $form-&amp;gt;field($model, 'year')-&amp;gt;textInput() ?&amp;gt;\n\n    = $form-&amp;gt;field($model, 'genre')-&amp;gt;textInput(['maxlength' =&amp;gt; true]) ?&amp;gt;\n\n    = $form-&amp;gt;field($model, 'rating')-&amp;gt;textInput() ?&amp;gt;\n\n    &lt;div class=\"form-group\"&gt;\n        = Html::submitButton('Save', ['class' =&amp;gt; 'btn btn-success']) ?&amp;gt;\n    &lt;\/div&gt;\n\n    &lt;?php ActiveForm::end(); ?&gt;\n&lt;\/div&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ul>\n<li>update.php &#8211; \u7528\u4e8e\u7f16\u8f91\u7535\u5f71<\/li>\n<\/ul>\n<pre>&lt;?php use yiihelpersHtml;\nuse yiiwidgetsActiveForm;\n\n\/* @var $this yiiwebView *\/\n\/* @var $model appmodelsMovie *\/\n\n$this-&gt;title = 'Update Movie: ' . $model-&amp;gt;title;\n$this-&amp;gt;params['breadcrumbs'][] = ['label' =&amp;gt; 'Movies', 'url' =&amp;gt; ['index']];\n$this-&amp;gt;params['breadcrumbs'][] = ['label' =&amp;gt; $model-&amp;gt;title, 'url' =&amp;gt; ['view', 'id' =&amp;gt; $model-&amp;gt;id]];\n$this-&amp;gt;params['breadcrumbs'][] = 'Update';\n?&amp;gt;\n&lt;h1&gt;= Html::encode($this-&amp;gt;title) ?&amp;gt;&lt;\/h1&gt;\n\n&lt;div class=\"movie-form\"&gt;\n\n    &lt;?php $form = ActiveForm::begin(); ?&gt;= $form-&amp;gt;field($model, 'title')-&amp;gt;textInput(['maxlength' =&amp;gt; true]) ?&amp;gt;\n\n    = $form-&amp;gt;field($model, 'director')-&amp;gt;textInput(['maxlength' =&amp;gt; true]) ?&amp;gt;\n\n    = $form-&amp;gt;field($model, 'actors')-&amp;gt;textarea(['rows' =&amp;gt; 6]) ?&amp;gt;\n\n    = $form-&amp;gt;field($model, 'year')-&amp;gt;textInput() ?&amp;gt;\n\n    = $form-&amp;gt;field($model, 'genre')-&amp;gt;textInput(['maxlength' =&amp;gt; true]) ?&amp;gt;\n\n    = $form-&amp;gt;field($model, 'rating')-&amp;gt;textInput() ?&amp;gt;\n\n    &lt;div class=\"form-group\"&gt;\n        = Html::submitButton('Save', ['class' =&amp;gt; 'btn btn-primary']) ?&amp;gt;\n    &lt;\/div&gt;\n\n    &lt;?php ActiveForm::end(); ?&gt;\n&lt;\/div&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<ol>\n<li>\u8fd0\u884c\u7535\u5f71\u7f51\u7ad9<\/li>\n<\/ol>\n<p>\u73b0\u5728\u6211\u4eec\u5df2\u7ecf\u5b8c\u6210\u4e86Yii\u6846\u67b6\u7535\u5f71\u7f51\u7ad9\u7684\u521b\u5efa\uff0c\u6240\u6709\u4ee3\u7801\u90fd\u5df2\u7ecf\u5c31\u7eea\u3002\u8981\u5728\u672c\u5730\u8fd0\u884c\u7535\u5f71\u7f51\u7ad9\uff0c\u8bf7\u5728\u7ec8\u7aef\u4e2d\u8fdb\u5165\u5e94\u7528\u7a0b\u5e8f\u6839\u76ee\u5f55\uff0c\u7136\u540e\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>php yii serve<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u5c06\u542f\u52a8\u4e00\u4e2a\u672c\u5730Web\u670d\u52a1\u5668\uff0c\u5e76\u5728\u7aef\u53e38000\u4e0a\u8fd0\u884c\u4f60\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u6253\u5f00http:\/\/localhost:8000\/\uff0c\u770b\u5230\u4f60\u7684\u7535\u5f71\u7f51\u7ad9\u4e86\u3002<\/p>\n<p>\u5728\u8fd9\u7bc7\u6587\u7ae0\u4e2d\uff0c\u6211\u4eec\u5df2\u7ecf\u6f14\u793a\u4e86\u5982\u4f55\u4f7f\u7528Yii\u6846\u67b6\u521b\u5efa\u7535\u5f71\u7f51\u7ad9\u3002\u4f7f\u7528Yii\u6846\u67b6\u4f1a\u52a0\u5feb\u4f60\u7684\u5f00\u53d1\u901f\u5ea6\uff0c\u5e76\u63d0\u4f9b\u5f88\u591a\u6709\u7528\u7684\u7279\u6027\uff0c\u4f8b\u5982ActiveRecord\u3001MVC\u67b6\u6784\u3001\u8868\u5355\u9a8c\u8bc1\u3001\u5b89\u5168\u6027\u7b49\u7b49\u3002\u8981\u6df1\u5165\u4e86\u89e3Yii\u6846\u67b6\uff0c\u8bf7\u67e5\u770b\u5176\u6587\u6863\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u4f7f\u7528Yii\u6846\u67b6\u521b\u5efa\u7535\u5f71\u7f51\u7ad9\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u7740\u4e92\u8054\u7f51\u7684\u666e\u53ca\u4ee5\u53ca\u4eba\u4eec\u5bf9\u7535\u5f71\u7684\u70ed\u7231\uff0c\u7535\u5f71\u7f51\u7ad9\u6210\u4e3a\u4e86\u4e00\u4e2a\u53d7\u6b22\u8fce\u7684\u7f51\u7ad9\u7c7b\u578b\u3002\u5728\u521b\u5efa\u4e00\u4e2a\u7535\u5f71\u7f51\u7ad9\u65f6\uff0c\u4e00\u4e2a\u597d\u7684\u6846\u67b6\u662f\u975e\u5e38\u5fc5\u8981\u7684\u3002yii\u6846\u67b6\u662f\u4e00\u4e2a\u9ad8\u6027\u80fd\u7684php\u6846\u67b6\uff0c\u6613\u4e8e\u4f7f\u7528\u4e14\u5177\u6709\u51fa\u8272\u7684\u6027\u80fd\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u63a2\u8ba8\u5982\u4f55\u4f7f\u7528yii\u6846\u67b6\u521b\u5efa\u4e00\u4e2a\u7535\u5f71\u7f51\u7ad9\u3002 \u5b89\u88c5Yii\u6846\u67b6 \u5728\u4f7f\u7528Yii\u6846\u67b6\u4e4b\u524d\uff0c\u9700\u8981\u5148\u5b89\u88c5\u6846\u67b6\u3002\u5b89\u88c5Yii\u6846\u67b6\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728\u7ec8\u7aef\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a composer create-project yiisoft\/yii2-app-basic \u767b\u5f55\u540e\u590d\u5236 \u8be5\u547d\u4ee4\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684Yii2\u5e94\u7528\u7a0b\u5e8f\u3002\u73b0\u5728\u4f60\u5df2\u7ecf\u51c6\u5907\u597d\u5f00\u59cb\u521b\u5efa\u4f60\u7684\u7535\u5f71\u7f51\u7ad9\u4e86\u3002 \u521b\u5efa\u6570\u636e\u5e93\u548c\u8868\u683c Yii\u6846\u67b6\u63d0\u4f9b\u4e86ActiveRecord\uff0c\u8fd9\u662f\u4e00\u79cd\u4f7f\u64cd\u4f5c\u6570\u636e\u5e93\u53d8\u5f97\u5bb9\u6613\u7684\u65b9\u5f0f\u3002\u5728\u672c\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3amovies\u7684\u6570\u636e\u8868\uff0c\u8be5\u8868\u5305\u542b\u7535\u5f71ID\u3001\u6807\u9898\u3001\u5bfc\u6f14\u3001\u6f14\u5458\u3001\u5e74\u4efd\u3001\u7c7b\u578b\u548c\u8bc4\u5206\u7b49\u4fe1\u606f\u3002\u8981\u521b\u5efa\u8868\uff0c\u8bf7\u5728\u7ec8\u7aef\u4e2d\u8fdb\u5165\u5e94\u7528\u7a0b\u5e8f\u6839\u76ee\u5f55\uff0c\u7136\u540e\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a php yii migrate\/create create_movies_table \u767b\u5f55\u540e\u590d\u5236 \u7136\u540e\u5c06\u751f\u6210\u7684\u8fc1\u79fb\u6587\u4ef6\u7f16\u8f91\u4e3a\u4ee5\u4e0b\u5185\u5bb9\uff1a &lt;?php use yiidbMigration; \/** * Handles the creation of table `{{%movies}}`. *\/ class m210630_050401_create_movies_table extends Migration { \/** * {@inheritdoc} *\/ public function safeUp() { $this-&gt;createTable(&#8216;{{%movies}}&#8217;, [ &#8216;id&#8217; =&amp;gt; $this-&amp;gt;primaryKey(), &#8216;title&#8217; =&amp;gt; $this-&amp;gt;string()-&amp;gt;notNull(), &#8216;director&#8217; =&amp;gt; $this-&amp;gt;string()-&amp;gt;notNull(), &#8216;actors&#8217; =&amp;gt; $this-&amp;gt;text()-&amp;gt;notNull(), [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-23843","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23843","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=23843"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/23843\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=23843"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=23843"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=23843"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}