I have three tables in my database:
At first, I only had the contratos
table and recently I needed to add the other two tables, as you will see, there is a field in pagos
which is related contratos
by fk
.
And I need that, when it's in my part to add a "contract", this saves your table ID ( contratos
) in the pagos
table. So it would be like sending to save data in two tables at once.
Here is my driver code:
function agregar_contrato()
{
$data = array(
'contratos' => $this->Pagos_model->getContratos(),
);
$this->load->view("layouts/header");
$this->load->view("layouts/aside");
$this->load->view("admin/clientes/agregar_contrato", $data);
$this->load->view("layouts/footer");
}
function registro_contrato()
{
$fecha_de_contrato = $this->input->post("fecha_de_contrato");
$fecha_de_instalacion = $this->input->post("fecha_de_instalacion");
$fecha_de_corte = $this->input->post("fecha_de_corte");
$fecha_de_pago = $this->input->post("fecha_de_pago");
$this->form_validation->set_rules("fecha_de_contrato", "\"Fecha del contrato\"", "trim|required");
if ($this->form_validation->run()) {
$data = array(
'fecha_de_contrato' => $fecha_de_contrato,
'fecha_de_instalacion' => $fecha_de_instalacion,
'fecha_de_corte' => $fecha_de_corte,
'fecha_de_pago' => $fecha_de_pago,
'borrado' => "1",
);
if ($this->Clientes_model->save_contrato($data)) {
redirect(base_url() . "clientes/clientes_controller"); //Me regresa a la vista principal
} else {
$this->session->set_flashdata("error", "No se pudo guardar la informacion");
redirect(base_url() . "clientes/clientes_controller/agregar_contrato");
}
} else {
$this->agregar_contrato();
}
}
And my model:
function getContratos()
{
$this->db->select("con.*, pag.*, fpagos.*");
$this->db->from("contratos con");
$this->db->join("pagos pag", "pag.contrato_id = con.id_contratos_d");
$this->db->join("fechas_de_pagos fpagos", "fpagos.pagos_id = pag.id_pagos");
$this->db->where("con.borrado", "1");
$resultados = $this->db->get();
return $resultados->result();
}
As I mentioned at the beginning contratos
was there, and all the data keeps well (here I only put 4 data, really are more than 20). But I ask for help, since I've tried it, I think it would be worse to put it here. So, to avoid worse criticism, I leave it as I originally had it.