Php 获取两种不同型号的数据-Yii2
我有一张表格,里面是这样的:Php 获取两种不同型号的数据-Yii2,php,model-view-controller,yii2,Php,Model View Controller,Yii2,我有一张表格,里面是这样的: <?= $form->field($model, 'comment')->textarea(['rows' => 6]) ?> <?= $form->field($presentation, 'attendance')->textInput(['maxlength' => true]) ?> <?= $form->field($presentation, 'couples')->tex
<?= $form->field($model, 'comment')->textarea(['rows' => 6]) ?>
<?= $form->field($presentation, 'attendance')->textInput(['maxlength' => true]) ?>
<?= $form->field($presentation, 'couples')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'hire_price')->textInput(['maxlength' => true]) ?>
但实际上,它保存了除“出席”和“夫妇”之外的所有内容,这些内容设置为0(在此表单之前,它们为空)
不管我用什么数字,我都得到零。它甚至不必是数字,因为验证根本不起作用 更改以下两行:
$pres->attendance = $presentation->load(Yii::$app->request->post('attendance'));
$pres->couples = $presentation->load(Yii::$app->request->post('couples'));
// to this
$pres->attendance = $presentation->attendance;
$pres->couples = $presentation->couples;
您已经在此处加载了
$presentation
值$presentation->load(Yii::$app->request->post())
,应该可以直接访问。更改以下两行:
$pres->attendance = $presentation->load(Yii::$app->request->post('attendance'));
$pres->couples = $presentation->load(Yii::$app->request->post('couples'));
// to this
$pres->attendance = $presentation->attendance;
$pres->couples = $presentation->couples;
您已经在此处加载了
$presentation
值$presentation->load(Yii::$app->request->post())
,应该可以直接访问。控制器代码有问题,请更改以下两行
if ($model->load(Yii::$app->request->post()) && $model->save() && $presentation->load(Yii::$app->request->post())) {
$post_data= Yii::$app->request->post();
$pres = $presentation->findOne($model->presentation_id);
/* change these two lines */
$pres->attendance = $post_data['Presentations']['attendance'];
$pres->couples =$post_data['Presentations']['couples'];
/* change these two lines */
$pres->save();
return $this->redirect(['view', 'id' => $model->id]);
}
else
{
return $this->render('create', [
'model' => $model,
'presentation' => $presentation,
]);
}
控制器代码有问题,请更改以下两行
if ($model->load(Yii::$app->request->post()) && $model->save() && $presentation->load(Yii::$app->request->post())) {
$post_data= Yii::$app->request->post();
$pres = $presentation->findOne($model->presentation_id);
/* change these two lines */
$pres->attendance = $post_data['Presentations']['attendance'];
$pres->couples =$post_data['Presentations']['couples'];
/* change these two lines */
$pres->save();
return $this->redirect(['view', 'id' => $model->id]);
}
else
{
return $this->render('create', [
'model' => $model,
'presentation' => $presentation,
]);
}
你这里的代码有点乱。首先,我建议您不要使用
load()
方法,但这是我个人的偏好。
此方法返回布尔值
,这就是在模型属性中获得0
的原因。通常,您的代码应该更像:
$model->load(Yii::$app->request->post());
if ($model->save()) {
$pres = $presentation->findOne($model->presentation_id);
$pres->attendance = $presentation->attendance;
//etc ...
$pres->save()
}
我不知道你的代码有什么意义,但这看起来有点毫无意义。尝试使用,它是所有模型属性的数组。或者手动分配模型想要的属性。这里的代码有点乱。首先,我建议您不要使用
load()
方法,但这是我个人的偏好。
此方法返回布尔值
,这就是在模型属性中获得0
的原因。通常,您的代码应该更像:
$model->load(Yii::$app->request->post());
if ($model->save()) {
$pres = $presentation->findOne($model->presentation_id);
$pres->attendance = $presentation->attendance;
//etc ...
$pres->save()
}
我不知道你的代码有什么意义,但这看起来有点毫无意义。尝试使用,它是所有模型属性的数组。或者手动分配模型想要的属性。哇,这太简单了,我在这里花了几个小时。现在我更清楚了,谢谢@lenishasdaI,我很高兴它能帮上忙=)@OlgaWow,这太简单了,我在这里呆了几个小时。现在我更清楚了,谢谢@lenishasdaI,我很高兴这有帮助=)@OlgaSeemed还好,但在这里对我不起作用。但我不知道如何通过数组访问post数据,我肯定会在另一个地方使用它:)在保存之前可以打印模型属性吗,echo“”;打印($pres->attributes);print_r($post_data)我已经发现@leninhasda的anwer在这里工作;)看起来不错,但在这里对我不起作用。但我不知道如何通过数组访问post数据,我肯定会在另一个地方使用它:)在保存之前可以打印模型属性吗,echo“”;打印($pres->attributes);print_r($post_data)我已经发现@leninhasda的anwer在这里工作;)