我想將數據保存在兩個表中。正在使用的表單是帶有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形式已被保存後獲得兩個值,還是要這樣,我可以得到的值並保存在阿賈克斯的形式?
任何幫助,將不勝感激!謝謝
'$模型 - > Task_ID'和'$模型 - > Employee_ID'是空的,因爲高達該行'$ model'是空的。您剛剛創建了空模型_($ model = new Activity();)_並且沒有設置任何值!我不明白'從窗體'部分獲取兩個值'Task_ID'和'Employee_ID'。你想要他們在哪裏? – leninhasda
好的。我想要Form中的Task_ID和Employee_ID。當用戶嘗試創建一個新條目並單擊提交時,我希望表單中的這兩個值保存在$ empModel-> Task_ID和$ empModel-> Employee_ID – user2211486