Yii2-一种形式的两个模型-值未保存在第二个模型中

时间:2019-06-11 18:07:54

标签: yii2

我有一个名为Patientinformation.php的模型和一个名为AlternativeInformation.php的模型。我想在Patientinformation_form.php

中使用这两个模型

患者信息模型:
-id(PK)
-Patient_id
-Patient_initials
-collection_site
-癌症
-学习(FK学习模型)

AlternativeInformation模型:
-Alternative_id(PK)
-Patient_patient_id(从FK到Patientinformation模型的ID)
-Alternative_study(用于学习模型的FK)
-Alternative_patient_id

我已经更新了Patientinformation控制器以及create.php文件和_form.php文件。

如果我在Patientinformation form.php中输入值,我将被重定向到Patientinformation视图站点。 Patientinformation模型的值(id,Patient_id,Patient_initials,collection_site,癌症和研究)保存在数据库中。

但是,AlternativeInformation模型的值(alternative_study和Alternative_ Patient_id)未保存在数据库中。

这是 PatientinformationController.php 文件:

<?php

namespace app\controllers;

use Yii;
use app\models\Patientinformation;
use app\models\AlternativeInformation;
use app\models\PatientinformationSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

    /**
     * Creates a new Patientinformation model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Patientinformation();
        $modelAlternative = new AlternativeInformation();     


        if ($model->load(Yii::$app->request->post()) && $modelAlternative->load(Yii::$app->request->post())){
            $model->save();
            $modelAlternative->patients_patient_id = $model->id;
            $modelAlternative->save();

            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
            'modelAlternative' => $modelAlternative,
        ]);
    } 

这是 create.php 文件:

<?php

use yii\helpers\Html;

/* @var $this yii\web\View */
/* @var $model app\models\Patientinformation */
/* @var $modelAlternative app\models\AlternativeInformation */


$this->title = 'Add patient';
$this->params['breadcrumbs'][] = ['label' => 'Patients', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="patientinformation-create">

    <h1><?= Html::encode($this->title) ?></h1>

    <?= $this->render('_form', [
        'model' => $model,
        'modelAlternative' => $modelAlternative,

    ]) ?>

</div>

这是 _form.php 文件:

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use app\models\CollectionSiteInformation;
use app\models\AlternativeInformation;
use app\models\StudyInformation;
use app\models\CancerInformation;
use unclead\multipleinput\MultipleInput;
use kartik\select2\Select2;



/* @var $this yii\web\View */
/* @var $model app\models\Patientinformation */
/* @var $modelAlternative app\models\AlternativeInformation */
/* @var $form yii\widgets\ActiveForm */
?>

<div class="patientinformation-form">

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

    <p>
    Please note that all fields marked with an asterisk (<font color="red">*</font>) are required.
    </p>

    <?= $form->field($model, 'study')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(StudyInformation::find()->all(),'id','study'),
        'language' => 'en',
        'options' => ['placeholder' => 'Please select a study'],
        ]); 
    ?>

    <?= $form->field($model, 'patient_id')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'patient_initials')->textInput(['maxlength' => true]) ?>

    <?= $form->field($model, 'collection_site')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(CollectionSiteInformation::find()->all(),'id','collection_site'),
        'language' => 'en',
        'options' => ['placeholder' => 'Please select a collection site'],
        ]); 
    ?>

    <?= $form->field($model, 'cancer')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(CancerInformation::find()->all(),'id','cancer'),
        'language' => 'en',
        'options' => ['placeholder' => 'Please select a cancer type'],
        ]); 
    ?>

    <?= $form->field($modelAlternative,'alternative_study')->widget(MultipleInput::class,[
    'max' => 6,
    'allowEmptyList'    => false,
    'enableGuessTitle'  => true,
    'columns' => [
        [
            'name'  => 'alternative_study',
            'type'  => 'dropDownList',
            'title' => 'Alternative Study',
            'items' => ArrayHelper::map(studyInformation::find()->asArray()->all (),'id','study'),
            'options' => ['prompt' => 'Please select an alternative study if applicable'],
        ],
    ]
    ])
    ->label(false);
    ?>

    <?= $form->field($modelAlternative,'alternative_patient_id')->widget(MultipleInput::class,[
        'max' => 6,
        'allowEmptyList'    => false,
        'enableGuessTitle'  => true,
        'columns' => [
            [
                'name'  => 'alternative_patient_id',
                'title' => 'Alternative patient ID',
                'options' => ['prompt' => 'Please select an alternative study if applicable'],
            ],
        ]
        ])
        ->label(false);
    ?>

    <div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

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

</div>

如果我在_form.php中不使用unclead \ multipleinput \ MultipleInput,则AlternativeInformation模型的所有值(alternative_study和Alternative_ Patient_id)都将保存到模型/数据库中。

所以我假设我没有将任何项目传递给MultipleInput小部件。

这是AlternativeInformation模型文件:

/**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['patients_patient_id', 'alternative_study'], 'required'],
            [['patients_patient_id', 'alternative_study'], 'integer'],
            [['alternative_patient_id'], 'string', 'max' => 265],
            [['patients_patient_id'], 'exist', 'skipOnError' => true, 'targetClass' => PatientInformation::className(), 'targetAttribute' => ['patients_patient_id' => 'id']],
            [['alternative_study'], 'exist', 'skipOnError' => true, 'targetClass' => StudyInformation::className(), 'targetAttribute' => ['alternative_study' => 'id']],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'alternative_id' => 'Alternative ID',
            'patients_patient_id' => 'Patients Patient ID',
            'alternative_study' => 'Alternative Study',
            'alternative_patient_id' => 'Alternative Patient ID',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getPatientsPatient()
    {
        return $this->hasOne(PatientInformation::className(), ['id' => 'patients_patient_id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getAlternativeStudy()
    {
        return $this->hasOne(StudyInformation::className(), ['id' => 'alternative_study']);
    }
}

有人对如何解决此问题有任何建议吗?如果使用MultipleInput小部件,“ alternative_study”和“ alternative_ Patient_id”的值将如何保存到AlternativeInformation模型中?

我真的需要表格中的选项来输入否,一项或多项替代研究以及替代患者编号。

我非常感谢我能解决此问题的任何帮助。

谢谢:)

2 个答案:

答案 0 :(得分:0)

您可能会遇到一些看不见的验证错误,因为即使view模型存在验证错误,您也将重定向到AlternativeInformation操作。

寻找解决方案的第一步是确保,如果AlternativeInformation不保存,您将再次显示该表单,这样您将看到验证错误。

将您的控制器代码更新为此:

public function actionCreate()
{
    $model = new Patientinformation();
    $modelAlternative = new AlternativeInformation();     


    if ($model->load(Yii::$app->request->post()) 
        && $modelAlternative->load(Yii::$app->request->post())
        && $model->save()) {

        $modelAlternative->patients_patient_id = $model->id;

        if ($modelAlternative->save()) {

            return $this->redirect(['view', 'id' => $model->id]);

        }
    }

    return $this->render('create', [
        'model' => $model,
        'modelAlternative' => $modelAlternative,
    ]);
}

这应该可以帮助您找到问题,并且还可以向用户提供有关将来可能发生的任何错误的信息。

验证问题可能在alternative_patient_id上,似乎您没有将任何items传递给MultipleInput小部件。

答案 1 :(得分:0)

使用以下代码更新您的控制器。

public function actionCreate()
    {
        $model = new Patientinformation();
        $modelAlternative = new AlternativeInformation();     


        if ($model->load(Yii::$app->request->post()) &&    $modelAlternative->load(Yii::$app->request->post())){
            if($model->save()){
                $modelAlternative->patients_patient_id = $model->id;
                $modelAlternative->save();
                return $this->redirect(['view', 'id' => $model->id]);
            }
        }

        return $this->render('create', [
            'model' => $model,
            'modelAlternative' => $modelAlternative,
        ]);
    }