我有一个表单,该表单具有一个paperDOI
textInput字段。它必须是唯一的。因此,我遵循以下形式:
<?php $form = ActiveForm::begin(['id' => $model->formName(),'enableAjaxValidation'=>true,'options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model->paper, 'paperDOI')->textInput(['maxlength' => true])->label(false) ?>
<?php ActiveForm::end(); ?>
在纸模型规则中,有一个类似[['paperDOI'], 'unique']
以及在我的控制器中:
use yii\widgets\ActiveForm;
public function actionCreate(){
$model = new PaperMagazine();
$paper = new Paper();
$requestPost = Yii::$app->request->post();
$model->populateRelation('paper', $paper);
if (Yii::$app->request->isAjax && $model->paper->load($requestPost) && $model->load($requestPost)) {
Yii::$app->response->format=Response::FORMAT_JSON;
return ActiveForm::validate($model->paper);
}
if ($model->paper->load($requestPost) && $model->load($requestPost))
{some code for save model...}
}
和actioUpdate:
public function actionUpdate($id)
{
$requestPost = Yii::$app->request->post();
$model = PaperMagazine::findOne($id);
if (Yii::$app->request->isAjax && $model->paper->load($requestPost) && $model->load($requestPost)) {
Yii::$app->response->format=Response::FORMAT_JSON;
return ActiveForm::validate($model->paper);
}
if ($model->load($requestPost) && $model->paper->load($requestPost)) {some codes for save ...}
在create动作中效果很好,并且如果paperDOI已经存在,则会显示验证错误。但以更新形式无效,并发生了奇怪的事件。第一次输入DB中存在的paperDOI时,它显示正确的验证错误。但是只有一次很有效,当我输入另一个代码并离开textInput时,它会抛出错误:
{"name":"PHP Notice","message":"Array to string conversion","code":8,"type":"yii\\base\\ErrorException","file":"D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\db\\ColumnSchema.php","line":157,"stack-trace":["#0 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\db\\QueryBuilder.php(375): yii\\db\\mysql\\ColumnSchema->dbTypecast()","#1 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\db\\mysql\\QueryBuilder.php(249): yii\\db\\QueryBuilder->prepareInsertValues()","#2 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\db\\QueryBuilder.php(345): yii\\db\\mysql\\QueryBuilder->prepareInsertValues()","#3 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\db\\Command.php(470): yii\\db\\QueryBuilder->insert()","#4 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\db\\Schema.php(432): yii\\db\\Command->insert()","#5 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\db\\ActiveRecord.php(549): yii\\db\\Schema->insert()","#6 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\db\\ActiveRecord.php(515): yii\\db\\ActiveRecord->insertInternal()","#7 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\db\\BaseActiveRecord.php(670): yii\\db\\ActiveRecord->insert()","#8 D:\\Project Files\\Git\\SAME\\backend\\models\\Log.php(37): yii\\db\\BaseActiveRecord->save()","#9 D:\\Project Files\\Git\\SAME\\common\\OOP\\CommonController.php(124): backend\\models\\Log::Log()","#10 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\base\\Controller.php(159): common\\OOP\\CommonController->afterAction()","#11 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\base\\Module.php(528): yii\\base\\Controller->runAction()","#12 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\web\\Application.php(103): yii\\base\\Module->runAction()","#13 D:\\Project Files\\Git\\SAME\\vendor\\yiisoft\\yii2\\base\\Application.php(386): yii\\web\\Application->handleRequest()","#14 D:\\Project Files\\Git\\SAME\\backend\\web\\index.php(17): yii\\base\\Application->run()","#15 {main}"]}
是什么?为什么在actionCreate上不能正常工作,而在actionUpdate上却不能正常工作?有什么问题吗?