model and controller in codeigniter for making Insert in Firebird using Generator

Asked

Viewed 362 times

2

I am new to codeigniter and need to define my controller and model, so when calling the model inserir(), the controller takes the last Generator ID of each table you want to do Insert. I’ll 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 that case, the field COD_CIDADES would have to receive the last ID (GEN_CIDADES), I know I could do an appointment like this: "select gen_id(GEN_CIDADES, 0) as COD from RDB"."$"."DATABASE", but I don’t know how to structure this so that I can use the model insert() for all my Insert s.

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);

}

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 he has to add one more in the result he took from Generator.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.