I'm trying to get the ID of my master table to be inserted to my transaction table. The insert is happening inside a loop.
Behavior:
- Insert master
- Get id
- Insert Transaction
- Insert Master
- Get id
- Insert Transaction
Controller:
//Insert Master
foreach($arr_master as $res_master){
/*$data['ID_REQUIREMENT_TRANS'] this is an auto-increment column. Inserted automatically*/
$data['ID_REQUIREMENT'] = $res_master;
$data['SENT_BY_DATE'] = date("Y-m-d H:i:s");
$data['STATUS'] = 1;
$this->MAdmin->ins_assign_pic($data);
//Insert Trans
foreach($arr_trans as $res_trans){
$data_d['ID_REQUIREMENT_TRANS'] = $id; //Need to get the last inserted id of the master table
$data_d['RECEIVED_BY'] = $res_trans;
$data_d['RECEIVED_BY_DATE'] = NULL;
$data_d['STATUS'] = 1;
$this->MAdmin->ins_assign_pic_d($data_d);
}
}
Model:
function ins_assign_pic($data){
$q_ins_assign_pic = $this->db->insert('pm_requirement_assign_pic',$data);
return $q_ins_assign_pic;
$id = $this->db->insert_id();
return $id; //How to send this to my controller
return true;
}
function ins_assign_pic_d($data_d){
$q_ins_assign_pic_d = $this->db->insert('pm_requirement_assign_pic_d',$data_d);
return $q_ins_assign_pic_d;
}