1
我有一个_form
:如何在yii2中创建表单后使用更新?
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'result')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'test1')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'test2')->textInput(['maxlength' => true]) ?>
<?php ActiveForm::end(); ?>
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
我需要设置一个公式:结果=测试1 *测试2
使用原始的SQL我可以把它放在控制器:
public function actionIndex(){
Yii::$app->db->createCommand("UPDATE table1 SET result = test1*test2")->execute();
etc...
}
而且没关系。但我认为这是不正确的。因此,要做到这一点,最好的办法是:
public function actionTest($id)
{
$model = $this->findModel($id);
$model->result = $model->test1 * model->test2
}
后:
<?= Html::a('gotest', ['test', 'id' => $model->id], ['class' => 'btn btn-success']) ?>
但我怎么能得到$model->id
从形式创造的actionIndex?或者也许是其他选择?
在这种情况下,我需要结果在数据库中。 'afterFind()'工作得很好,我在一分钟前测试过,并且用'public $ result;'添加虚拟字段非常有趣,我认为这将是另一个任务所必需的。例如'checkboxList()'并插入数据库单元格(使用'implode')。所以你的回答将提高我对使用模型的理解。 –