Insert data in two different tables with MVC

0

I have two tables that are related, one of them depends on the information of the 1st table (payments depend on contracts). I just have to take a form to save the information.

This is how I have the registry function in the Controller:

public function registro_contrato()
{
    $id_client = $this->Clientes_model->lastID_Cliente();
    $clienteid = ($id_client[0]->id_cliente);

    $id_dom_instalacion = $this->Clientes_model->lastID_Domicilio();
    //Omicion de datos para facil lectura....
    $tres_pago       = $this->input->post("tres_pago");

    //Datos a guardar en la otra tabla 
    $costo_instalacion   = $this->input->post("costo_instalacion");
    $monto_anticipo      = $this->input->post("monto_anticipo");
    $forma_de_pago       = $this->input->post("forma_de_pago");
    $restante            = $this->input->post("restante");
    $tres_pagos          = $this->input->post("tres_pagos");

    if ($this->form_validation->run()) {
        $data = array(
            'fecha_de_contrato'               => $fecha_de_contrato,
            //Omicion de datos para facil lectura....
            'tres_pago'                   => $tres_pago,

            //Datos a guardar en la otra tabla 
            'costo_instalacion'               => $costo_instalacion,
            'monto_anticipo'                  => $monto_anticipo,
            'forma_de_pago'                   => $forma_de_pago,
            'restante'                        => $restante,
            'tres_pagos'                      => $tres_pagos,
        );

        if ($this->Clientes_model->save_contrato($data)) {

            redirect(base_url() . "clientes/clientes_controller");

        } else {
            $this->session->set_flashdata("error", "No se pudo guardar la informacion"); 
            redirect(base_url() . "clientes/clientes_controller/agregar_contrato");
        }
    } else {
        $this->agregar_contrato();
    }
}

The function in the Model:

public function save_contrato($data)
{
    return $this->db->insert("contratos", $data);
}

Currently it works perfect for the saved in the "Contracts" table but I have no idea how to implement the saving of the last 5 registers (and the id as the primary key) in the "Payments" table. I would appreciate a help with it ... Thanks in advance.

    
asked by González 26.02.2018 в 09:30
source

1 answer

1

You already have it (more or less).

You should modify the function save_contrato to keep something like:

public function save_contrato($data)
{
    return $this->db->insert('pagos', $data) && $this->db->insert('contratos', $data);
}

That I trust that it will give you an idea of where to go. But keep in mind that more logic may be necessary depending on how you want to make insertions in the table pagos , example: if the id related in the table pagos does not exist you must create it previously (make the inserts in pagos , note that id have been generated and act accordingly on contratos ). Or if you need to make multiple inserts (5 records, as you mention).

It would be, then, easier to keep the inserts separated into different functions, something like:

public function save_contrato($data)
{
    return save_pagos($data) && $this->db->insert('contratos', $data);
}

public function save_pagos($data)
{
    //lógica para inserción múltiple, filtrado de datos, etc.
    return $this->db->insert('contratos', $data);
}
    
answered by 26.02.2018 в 11:46