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

时间:2015-05-30 07:38:33

标签: yii2

我有一个名为Houses的模型,其中包含一个名为status的字段,用于跟踪占用/未占用的房屋。和另一个名为占用的模型。当一个房客被分配一个新房子时,我想改变那个房子的价值,而不是被占用的房子。被占领的#39;在入住的形式。 这是我一直使用的控制器部分

  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() ?>

我想更改特定选定房屋的房屋状态。 现在的问题是,当我硬编码房子没有在控制器工作正常。但是当我尝试使用

来选择所选的房子时
     $_GET[houseNumber]

并将其分配给变量然后在制作$ house对象时使用它,它失败了。

1 个答案:

答案 0 :(得分:0)

Firts不直接引用超级变量$ _GET或$ _POST代替

$post = Yii::$app->request->post();

其次我猜数据是通过post方法提交的,然后数据值存储在:

$post['Houses']
$post['Occupancypayments']
相关问题