通过 insert_id() 而不是 Return 将数据库的最后一个 ID table 从 Codeigniter 中的模型插入控制器文件

Last insert id of a database table by insert_id() not Return to Controller file from Model in Codeigniter

对于同一个函数内的一系列操作,我想return最后插入第一个数据行的id table以发送第二个数据table。

我的数据库中有 2 个 Table。插入后,第一个 table 中的所有数据然后将一些数据发送到第二个 table。从那里我持有最后一个插入 自动递增 Id 并想使用该 Id 发送第二个数据 table。为此,我有一个串行操作控制器功能。第一个操作和第二个操作一样,其他数据成功插入到第二个 table 除了来自第一个 table 的最后插入 ID 之外。在这里,我展示了我的控制器功能和模型。请建议我。

模型文件名test_model.php和控制器文件名test.php

控制器:

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);
}
}

模型函数 1

public function create($data = [])
{  
$this->db->insert($this->table,$data);
$last_insert_id = $this->db->insert_id(); 
return $last_insert_id;
}

模态函数 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);
}

您已经从 create() 方法返回 last id,因此只需检查此 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);
}
}