CodeIgniter批处理插入特定列

Cho*_*mid 8 php mysql codeigniter

我有一个CodeIgniter应用程序和一个具有以下结构的MySQL表:

Table shortlisted_candidates
id int PRIMARY KEY AUTO INCREMENT,
candidate_no int NOT NULL,
written_marks int,
viva_marks int
Run Code Online (Sandbox Code Playgroud)

我想在insert_batch这个表中做,但数据只会插入id和candidate_no列.

我知道Codeigniter Active Records Class提供了$this->db->insert_batch()批量插入的功能,但它实际上是在整个表中插入数据,而我希望数据只插入到特定的列中.我怎样才能在CodeIgniter中实现这一目标?

请注意,这id是一个AUTO INCREMENT,PRIMARY KEY列.

我的控制器代码:

class Shortlisted_candidates extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->model('Shortlisted_candidate');
        $this->load->helper('url');
        $this->load->helper('html');
        $this->load->helper('form');
        $this->output->enable_profiler(false);
    }
    function add(){
        $data = array();
        if ($_POST) {

            $data['candidate_no'] = $this->input->post('candidate_no');
            $this->Shortlisted_candidate->add_candidate_to_shortlist($data);
            $this->session->set_flashdata('message', 'Data Successfully Added');
            redirect('shortlisted_candidates/add');
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我的型号代码:

class Shortlisted_candidate extends CI_Model
{

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function add_candidate_to_shortlist($data){
        //Following code inserts data in ALL COLUMNS
        $this->db->insert_batch('shortlisted_candidates', $data);
        //How to write active record batch insert query for inserting data in only `id` and `candidate_no` column?
    }
}
Run Code Online (Sandbox Code Playgroud)

Rej*_*lam 1

您可以定义要插入批次的列。所以你必须让你的数组看起来像

$data = array(
    array(
            'id' => $id,
            'candidate_no' => $candidate_no
    ),
    array(
            'id' => $id,
            'candidate_no' => $candidate_no
    )
);

//now in controller

 $this->Shortlisted_candidate->add_candidate_to_shortlist($data);
Run Code Online (Sandbox Code Playgroud)

这将仅插入特定列,而不是整个表的列