Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter batch insert into specific columns

I have a CodeIgniter application and a MySQL table with the following structure:

Table shortlisted_candidates
id int PRIMARY KEY AUTO INCREMENT,
candidate_no int NOT NULL,
written_marks int,
viva_marks int

I want to do insert_batch into this table, but data will only be inserted in id and candidate_no columns.

I know Codeigniter Active Records Class provides the $this->db->insert_batch() function for batch insert but it actually inserts data in the entire table, whereas I want data to be inserted only into specific columns. How can I achieve this in CodeIgniter?

Note that id is an AUTO INCREMENT, PRIMARY KEY column.

My Controller code:

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

My Model code:

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?
    }
}
like image 871
Choudhury Saadmaan Mahmid Avatar asked Jul 14 '15 05:07

Choudhury Saadmaan Mahmid


2 Answers

you need to modify following controller function.

function add(){
    $data = array();
    if ($_POST) {

        $data[0]['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');
    }
  }

apart from this you need to modify your table schema such that either set default value of other field.

like image 193
shankar kumar Avatar answered Nov 04 '22 00:11

shankar kumar


You can define your column where you want to insert batch. So you have to make your array looks like

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

This will insert only specific column, not to entire table's column

like image 1
Rejoanul Alam Avatar answered Nov 03 '22 22:11

Rejoanul Alam