Reputation: 41
For a series of operation within the same function, I want to return last insert id of a row from a 1st data table to send 2nd data table.
I have 2 Table in my database. After insert, all data in a 1st table then send some data to the 2nd table. And from there I am holding the last insert Auto increment Id and want to used that Id to send 2nd data table. For that, I had a serial operation Controller function. 1st operate in performing well as also for 2nd operation other data successfully inserted to 2nd table except the last insert id from 1st table. Here I showing my controller function and model. Please suggest me.
Model file name test_model.php
and Controller file name test.php
CONTROLLER :
public function create()
{
$data['test'] = (object)$postData = [
'test_id' => $this->input->post('test_id',true),
'test_department_id' =>$this->input->post('test_department_id',true),
'test_catgory_id' =>$this->input->post('test_catgory_id',true),
'test_name' =>$this->input->post('test_name',true),
'test_descrip' =>$this->input->post('test_descrip',true),
'test_price' =>$this->input->post('test_price',true),
'test_report_method' =>$this->input->post('test_report_method',true),
'test_report_day' =>$this->input->post('test_report_day',true),
'test_dr_comi_amnt' =>$this->input->post('test_dr_comi_amnt',true),
'test_dr_comi_percnt' =>$this->input->post('test_dr_comi_percnt',true),
'test_ref_comi_amnt' =>$this->input->post('test_ref_comi_amnt',true),
'test_ref_comi_percnt' =>$this->input->post('test_ref_comi_percnt',true),
'status' => $this->input->post('status',true)
];
if ($this->form_validation->run() === true) {
if (empty($postData['test_id'])) {
if ($this->test_model->create($postData)) {
/*------FOR 2nd Operation------------- START -------*/
//----- data from Model ---------
$stest_id_1 = $this->test_model->getLast_InsertId();
$stest_id = $this->input->post($stest_id_1);
$sFranId = $this->input->post('frn_id');
$sm_updated_test_price = $this->input->post('test_price');
$test_forFran = array();
for ($i=1; $i < sizeof($sFranId); $i++)
{
if(!empty($sFranId[$i]))
$test_forFran[$i] = array(
'm_test_id' => $stest_id,
'm_updated_test_price' => $sm_updated_test_price,
'm_fran_id' => $sFranId[$i]
);
}
if($this->test_model->testInsert_PriceMaster($test_forFran)){
$this->session->set_flashdata('message', display('save_successfully'));
}
/*------FOR 2nd Operation------------- END -------*/
$this->session->set_flashdata('message', display('save_successfully'));
} else {
#set exception message
$this->session->set_flashdata('exception',display('please_try_again'));
}
redirect('medical_diagnosis/test/create');
}
} else {
$data['franceschi_list'] = $this->franceschi_model->franceschi_list_forId();
$data['department_list'] = $this->test_department_model->department_list();
$data['catagory_list'] = $this->test_catagory_model->catagory_list();
$data['content'] = $this->load->view('medical_diagnosis/test_form',$data,true);
$this->load->view('layout/main_wrapper',$data);
}
}
MODEL Function 1
public function create($data = [])
{
$this->db->insert($this->table,$data);
$last_insert_id = $this->db->insert_id();
return $last_insert_id;
}
MODAL Function 2 & 3
public function getLast_InsertId($last_insert_id = null)
{
$last_insert_id = $this->session->userdata('last_insert_id');
}
public function testInsert_PriceMaster($test_forFran)
{
return $this->db->insert_batch($this->fran_test_pricemaster,$test_forFran);
}
Upvotes: 0
Views: 277
Reputation: 9717
You are already returning last id
from create()
method so just check for this id and use it directly in your loop. there is no need of these line :
$stest_id_1 = $this->test_model->getLast_InsertId();
/*really don't understand below line of code (pls tell)*/
$stest_id = $this->input->post($stest_id_1);
It should be like this
public function create()
{
$data['test'] = (object)$postData = [
'test_id' => $this->input->post('test_id',true),
'test_department_id' =>$this->input->post('test_department_id',true),
'test_catgory_id' =>$this->input->post('test_catgory_id',true),
'test_name' =>$this->input->post('test_name',true),
'test_descrip' =>$this->input->post('test_descrip',true),
'test_price' =>$this->input->post('test_price',true),
'test_report_method' =>$this->input->post('test_report_method',true),
'test_report_day' =>$this->input->post('test_report_day',true),
'test_dr_comi_amnt' =>$this->input->post('test_dr_comi_amnt',true),
'test_dr_comi_percnt' =>$this->input->post('test_dr_comi_percnt',true),
'test_ref_comi_amnt' =>$this->input->post('test_ref_comi_amnt',true),
'test_ref_comi_percnt' =>$this->input->post('test_ref_comi_percnt',true),
'status' => $this->input->post('status',true)
];
if ($this->form_validation->run() === true) {
if (empty($postData['test_id'])) {
$insert_id = $this->test_model->create($postData);
if (! empty($insert_id)) {
/*------FOR 2nd Operation------------- START -------*/
//----- data from Model ---------
//$stest_id_1 = $this->test_model->getLast_InsertId();
$stest_id = $insert_id;
$sFranId = $this->input->post('frn_id');
$sm_updated_test_price = $this->input->post('test_price');
$test_forFran = array();
for ($i=1; $i < sizeof($sFranId); $i++)
{
if(!empty($sFranId[$i]))
$test_forFran[$i] = array(
'm_test_id' => $stest_id,
'm_updated_test_price' => $sm_updated_test_price,
'm_fran_id' => $sFranId[$i]
);
}
print_r($test_forFran);die;
if($this->test_model->testInsert_PriceMaster($test_forFran)){
$this->session->set_flashdata('message', display('save_successfully'));
}
/*------FOR 2nd Operation------------- END -------*/
$this->session->set_flashdata('message', display('save_successfully'));
} else {
#set exception message
$this->session->set_flashdata('exception',display('please_try_again'));
}
redirect('medical_diagnosis/test/create');
}
} else {
$data['franceschi_list'] = $this->franceschi_model->franceschi_list_forId();
$data['department_list'] = $this->test_department_model->department_list();
$data['catagory_list'] = $this->test_catagory_model->catagory_list();
$data['content'] = $this->load->view('medical_diagnosis/test_form',$data,true);
$this->load->view('layout/main_wrapper',$data);
}
}
Upvotes: 1