Set field to null - CodeIgniter

1

Hello, beauty?

I am doing an update in a table and am sending the array of data. However, I have a date field. Every time I update, it sends the field as 'dtnascimento' = '' . When this data arrives in the database, my database hits the date as 0000-00-00 .

What I want to do: make a check if my the position of my array is null. If it is null, set the value to arrive in the database as (NULL) .

Below my current code:

function atualizarFuncionario($dados){

    if ($dados['dtnascimento'] === '') {
        $dados['dtnascimento'] = NULL;
    }

    $where = array(
     'idfuncionario'    => $dados['idfuncionario'],
     'idempresa'        => $dados['idempresa']
    );
    $this->db->update($this->tabela,html_escape($dados),$where);

}

If anyone knows, give a moral ai rs

    
asked by anonymous 22.09.2015 / 20:09

2 answers

1

Dude, why do not you set Active Record to the fields you want to update?

Particularly I prefer to update only the specific data:

Assuming that you will update only the name (which is filled in) and the date field, which you do not know if it is filled in ...

$this->db-trans_begin();
$this->db->set('Nome', $dados['Nome']);
if($dados['dtnascimento'] != ''){
   $this->db->set('Data', $dados['dtnascimento']);
}
$this->db->where('idfuncionario', $dados['idfuncionario']);
$this->db->where('idempresa', $dados['idempresa']);
$this->db->update('tabela');
if($this->db->trans_status() === true){
   $this->db->trans_commit();
}else{
   $this->db->trans_rollback();
}

So, if the date field is not populated, it does not change anything ... Hope this helps. Oss!

    
26.09.2015 / 00:04
0

I usually use unset ()

function atualizarFuncionario($dados){

    if ($dados['dtnascimento'] === '') {
        unset($dados['dtnascimento']);
    }

    $where = array(
     'idfuncionario'    => $dados['idfuncionario'],
     'idempresa'        => $dados['idempresa']
    );
    $this->db->update($this->tabela,html_escape($dados),$where);

}
    
22.09.2015 / 20:17