对于同一个函数中的一系列操作,我想从第一个数据表返回一行的最后一个插入id来发送第二个数据表。
我的数据库中有2个表。插入后,第一个表中的所有数据然后将一些数据发送到第二个表。从那里我拿着最后一个插入自动增量ID ,并希望使用该Id发送第二个数据表。为此,我有一个串行操作控制器功能。第1次操作也是第2次操作,其他数据成功插入第2表除了第1个表中的最后一个插入ID 。这里我展示了我的控制器功能和型号。请建议我。
模型文件名test_model.php
和控制器文件名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);
}
}
&#13;
模型功能1
public function create($data = [])
{
$this->db->insert($this->table,$data);
$last_insert_id = $this->db->insert_id();
return $last_insert_id;
}
&#13;
MODAL功能2&amp; 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);
}
&#13;
答案 0 :(得分:1)
您已经从last id
方法返回create()
,因此请检查此ID并直接在循环中使用它。没有必要这些线:
$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);
应该是这样的
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);
}
}