Codeigniter - Retrieve ID

2

I need to retrieve the last ID inserted into a table and pass this ID to the field of another function.

Is it possible to recover in this way?

$negociacao = array(
        'id'             => $neg->id,
        'dt_negociacao'  => $neg->dt_negociacao,
        'atualizar'      => $neg->atualizar,
        'contrato_id'    => $neg->contrato_id,
        'id_finalizacao' => $neg->id_finalizacao,
        'crud'           => "C",
    );
$this->db->insert('tbl_devedor_negociacao', $negociacao);
$negociacao_id = $this->db->insert_id(); //Armazenar ID recuperado
$this->set_negociacao_id($negociacao_id); // Setter váriável ID armazenado

// setter ID
private function set_negociacao_id($negociacao_id = null)
{       
    return $negociacao_id;
}

// getter ID
private function get_negociacao_id()
{
    $this->set_negociacao_id();
}

// Atribuir ID recuperado Aqui
'negociacao_id'  => $this->set_negociacao_id(),
    
asked by anonymous 08.11.2017 / 13:41

1 answer

1

Get methods should return the value of something and set write or assign a value to a class's parity.

// setter ID
private function set_negociacao_id($negociacao_id = null)
{       
    return $negociacao_id;
}


private function get_negociacao_id()
{
    $this->set_negociacao_id();
}

The following line returns the value that was passed as argument which does not make much sense since it is not assigned anywhere or worse because creating a method that returns the input itself?

$this->set_negociacao_id($negociacao_id); // Setter váriável ID armazenado

If I understood correctly, to solve the problem first, the set should write the value passed in a class property. The corresponding code is:

private $negociacao_id;
//código omitido...

public function set_negociacao_id($negociacao_id)
{       
   $this->negociacao_id = $negociacao_id;
}

The get should return the property value only:

public function get_negociacao_id()
{
    return $this->negociacao_id;
}
    
08.11.2017 / 13:56