I have the general table
I also have the line table
And finally I have the table company_line
What I want to do is that when I insert in the general table, the id_general of the new record is passed to the company_line table and also put in the same table and the same record the id_line that I already get from the form
My Controller with the data I want to insert as an example
public function add_empresa_linea(){//aqui debo de insertar en dos tablas
$data = array(
'nombre' => $this->input->post('txt_nombre'),
'id_servicio' => 1,
);
$id_linea = $this->input->post('combo_linea');
$insert = $this->reg_calls_model->add_empresa_linea($data);
if ($insert) {
echo json_encode(array("status" => TRUE));
}
}
My model
function add_empresa_linea($data){
try {
$result = $this->db->insert('general',$data);
return $result;
} catch(Exception $e) {
show_error($e->getMessage() . ' --- ' . $e->getTraceAsString());
}
}
What you can do is open a transaction with BEGIN , do all the INSERT , UPDATE , DELETE , even the SELECT that you need, and once you consider that everything is fine, do COMMIT and that's it, this way you have what you need. You'll have to find the equivalent of this in your framework, while I give you an example in SQL:
I clarify that the three points (...) is to indicate the rest of the columns & values that must be completed, this is an example.