2016-08-31 72 views
0

我想將數據保存在兩個表中。正在使用的表單是帶有Ajax請求的ajax表單。以ajax格式保存數據yii2

我想從表單中獲取兩個值'Task_ID'和'Employee_ID',然後將其保存到另一個表中。

控制器代碼如下

public function actionCreate() 
{ 
    $request = Yii::$app->request; 
    $model = new Activity(); 
    $empModel = new Tasksemp(); 
    //$tasksModel = new Tasks(); 

    if($request->isAjax){ 
     /* 
     * Process for ajax request 
     */ 
     Yii::$app->response->format = Response::FORMAT_JSON; 
     if($request->isGet){ 



      return [ 
       'title'=> "Create new Activity", 
       'content'=>$this->renderAjax('create', [ 

        'model' => $model, 

        $model->finish_date = date('y-m-d'), //give default date as current along with date picker option 

        $empModel->Task_ID = $model->Task_ID, //<----Error here. The value of Task_ID and Employee_ID is null. 
        $empModel->Employee_ID = $model->Employee_ID, 
        $empModel->save(false), 

       ]), 
       'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]). 
          Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"]) 

      ]; 





     }else if($model->load($request->post()) && $model->save()){ 
      return [ 
       'forceReload'=>'#crud-datatable-pjax', 
       'title'=> "Create new Activity", 
       'content'=>'<span class="text-success">Create Activity success</span>', 
       'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]). 
         Html::a('Create More',['create'],['class'=>'btn btn-primary','role'=>'modal-remote']) 

      ];   
     }else{   
      return [ 
       'title'=> "Create new Activity", 
       'content'=>$this->renderAjax('create', [ 
        'model' => $model, 
        'empModel' => $empModel, 
       ]), 
       'footer'=> Html::button('Close',['class'=>'btn btn-default pull-left','data-dismiss'=>"modal"]). 
          Html::button('Save',['class'=>'btn btn-primary','type'=>"submit"]) 

      ];   
     }   
    }else{ 
     /* 
     * Process for non-ajax request 
     */ 
     if ($model->load($request->post()) && $model->save()) { 
      return $this->redirect(['view', 'id' => $model->Activity_ID]); 
     } else { 
      return $this->render('create', [ 
       'model' => $model, 
      ]); 
     } 
    } 

} 

我需要的Ajax形式已被保存後獲得兩個值,還是要這樣,我可以得到的值並保存在阿賈克斯的形式?

任何幫助,將不勝感激!謝謝

+0

'$模型 - > Task_ID'和'$模型 - > Employee_ID'是空的,因爲高達該行'$ model'是空的。您剛剛創建了空模型_($ model = new Activity();)_並且沒有設置任何值!我不明白'從窗體'部分獲取兩個值'Task_ID'和'Employee_ID'。你想要他們在哪裏? – leninhasda

+0

好的。我想要Form中的Task_ID和Employee_ID。當用戶嘗試創建一個新條目並單擊提交時,我希望表單中的這兩個值保存在$ empModel-> Task_ID和$ empModel-> Employee_ID – user2211486

回答

0

我想你寫了不正確的代碼。 不建議(嚴格)在[ ... ]聲明中執行「=」運算符和方法。 然後你不加載任何值到Activity模型。

if ($request->isGet){ 

    $model->finish_date = date('y-m-d'); //give default date as current along with date picker option 

    // loading values from form 

    $empModel->Task_ID  = $request->get('name_of_form_input_that_contains_Task_ID'); 
    $empModel->Employee_ID = $request->get('name_of_form_input_that_contains_Employee_ID'); 

    $empModel->save(false); 

    return [ 
     'title' => "Create new Activity", 
     'content' => $this->renderAjax('create', [ 
      'model' => $model, 
      // any other data 
     ]), 
     'footer' => '' // .. 
    ]; 

} 
+0

中我嘗試了您的建議,但出現錯誤: - 「name」:「PHP Warning」,「message」:「從空值創建默認對象」,「code」:2,「type」:「yii \\ base \\ ErrorException」 – user2211486

+0

這不是合併代碼。這只是你的代碼的一部分(「if語句」)。 – IStranger

+0

是的,但我沒有在代碼的其他部分爲任何模型分配更多值。你建議我應該怎麼做才能做到這一點? – user2211486