1

我正在使用多语言行为

但是,当单击更新按钮查看数据时,数据正在返回id = 1

这是我在控制器中的代码:

 public function actionUpdate($id)
{
    $model = Post::find()->multilingual()->one();
    //$model = Post::findOne($id);
    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

这是我的表单视图数据:

<?php $form = ActiveForm::begin(); ?>

<?= $form->field($model, 'title')->textInput() ?>
<?= $form->field($model, 'title_vi')->textInput() ?>
<?= $form->field($model, 'title_en')->textInput() ?>
<?= $form->field($model, 'created_at')->textInput() ?>

<?= $form->field($model, 'updated_at')->textInput() ?>

<?= $form->field($model, 'enabled')->textInput() ?>

<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>

这是我的模型:

class Post extends \yii\db\ActiveRecord
{
    public static function tableName()
    {
        return 'post';
    }
    public function behaviors()
    {
        return [
            'ml' => [
                'class' => MultilingualBehavior::className(),
                'languages' => [
                    'vi' => 'Vietnammese',
                    'en' => 'English',
              ],
                //'languageField' => 'language',
                //'localizedPrefix' => '',
                //'requireTranslations' => false',
                //'dynamicLangClass' => true',
                //'langClassName' => PostLang::className(), 
                //'defaultLanguage' => 'vi',
                'langForeignKey' => 'post_id',
                  'tableName' => "{{%postLang}}",
                    'attributes' => [
                    'title', 'content',
                ]
            ],
        ];
    }


     public static function find()
    {
    $q = new MultilingualQuery(get_called_class());
        return $q;
    }

    public static function with()
    {
        return ;
    }
}

和结果图像

结果图像

4

0 回答 0