在一个表单中更新不同的模型,同时在yii2中在同一表单上创建不同模型的新记录

在一个表单中更新不同的模型,同时在yii2中在同一表单上创建不同模型的新记录,yii2,Yii2,我有一个名为Houses的模型,其中有一个名为status的字段,用于跟踪已占用/未占用的房屋。还有另一种模式叫入住。当一个租户被分配一栋新房子时,我想在入住表上将该特定房子的价值从“未入住”更改为“已入住”。 这是我一直在使用的控制器的一部分 namespace app\controllers; use Yii; use app\models\Occupancypayments; use app\models\Houses;

我有一个名为Houses的模型,其中有一个名为status的字段,用于跟踪已占用/未占用的房屋。还有另一种模式叫入住。当一个租户被分配一栋新房子时,我想在入住表上将该特定房子的价值从“未入住”更改为“已入住”。 这是我一直在使用的控制器的一部分

  namespace app\controllers;

        use Yii;
        use app\models\Occupancypayments; 
        use app\models\Houses;
        use app\models\OccupancypaymentsSearch;
        use yii\web\Controller;

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

            $model = new Occupancypayments;

            if (isset($_POST['houseNumber'])) {

                $hse = $_POST['houseNumber'];
                }

            $house = Houses::find()->where(['houseNo'=>$hse])->one();

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

                      $model->save();
               return $this->redirect(['view', 'id' =>$model->transactionNo]);
            } else {
                return $this->render('create', [ 
                        'model' => $model,
                        'house' => $house,
                 ]);
            }
        }
这是入住表

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




 <?=  $form->field($model, 'propertyID')->widget(Select2::classname(), [
        'data' => ArrayHelper::map(Propertydetails::find()->all(), 'propertyId', 'propertyName'),
        'language' => 'en',
        'options' => ['placeholder' => 'choose property ...','id'=>'propertyId'],
        'pluginOptions' => [
            'allowClear' => true
        ],
   ]);?>

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

   <?= $form->field($model, 'houseNumber')->widget(DepDrop::classname(), [
    'options'=>['id'=>'houseNumber'], 
    'pluginOptions'=>[
    'depends'=>['propertyId'], // the id for cat attribute
    'placeholder'=>'Select house...',
    'url'=> Url::to(['occupancypayments/subcat']) 
    ]
    ]); ?>

    <!--set the selected  house number to occupied -->
    <?= $form->field($house, 'status')->dropDownList([ 'ocupied' => 'Ocupied', 'not ocupied' => 'Not ocupied', ], ['prompt' => '']) ?>

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

并将其分配给一个变量,然后在生成$house对象时使用它,它将失败。

首先不要直接引用超级变量$\u GET或$\u POST use

$post = Yii::$app->request->post();
其次,我猜数据是通过post方法提交的,然后数据值存储在:

$post['Houses']
$post['Occupancypayments']

非常感谢,但我得到了这个错误。未定义的索引职业支付。即使我使用$post['houseNumber'],也会出现同样的错误。未定义的索引房屋编号在访问post数据之前,必须测试
if(isset($post['Houses'])
。如果帖子没有设置是因为这些值没有设置,你必须测试它,当你收到帖子的答案时,你可以访问这些值。我有这个,如果(isset($POST['OccupncyPayments']){$hse=$POST['OccupncyPayments']['houseNumber'],给我一个空白页$house=Houses::find()->where(['houseNo'=>$hse])->one();使用echo检查$hse的内容,如果为空,则表示不提交相应的值,如果为空,则检查数据以供查询
$post['Houses']
$post['Occupancypayments']