model and controller in codeigniter to do insert in firebird using generator

2

I'm new to codeigniter and I need to define my controller and model, so when calling the model inserir() , the controller takes the last ID of the generator of each table that wants to do the insert. Let me give you an example:

controller:

public function salvar(){
    if($this->input->post('action') == 'cadastrar'){
        $dados = array (
            'COD_CIDADES' => '',
            'DESCRICAO' => $this->input->post('cidade'),
            'UF' => $this->input->post('uf'),
            'CEP' => $this->input->post('cep'),         
            'COD_SITUACAO' => '0',
            'ISS' => 'null',
            'COD_PAIS' => '1',
            'COD_IBGE' => $this->input->post('ibge')
        );

        $this->crud_model->inserir($tabela, $dados);

        redirect('cidades/index');

    }

model:

public function inserir($tabela, $dados_banco){                     
    return $this->db->insert($tabela, $dados_banco);
}

In this case, the field COD_CIDADES would have to receive the last ID (GEN_CIDADES) , I know it would be possible to make an appointment like this: "select gen_id(GEN_CIDADES, 0) as COD from RDB"."$"."DATABASE ", but I do not know how to structure this so that I can use the insert() model for all my inserts.

    
asked by anonymous 13.01.2016 / 14:24

2 answers

0

Assuming you have an 'id' column in your bd. Add another argument to your function:

   public function inserir($tabela, $dados_banco ,$key ){                     

        $query = $this->get($table);
        $row = $query->last_row();

        $dados_banco[$key] = $row->id;

         return $this->db->insert($tabela, $dados_banco);

}
    
19.01.2016 / 00:15
0

I also work with Codeigniter and Firebird and the way I found to solve this situation is to create a method in the model that returns the last generator:

public function pega_prox_id()
{
    return $this->db->query("SELECT GEN_ID(GEN_TB_TV_PEDIDO_ID, 1) FROM RDB\$DATABASE;")->result();
}

The number 1 after the comma indicates that it has to add one more in the result that it took from the generator.

    
02.02.2016 / 16:54