CodeIgniter - how to post value from form as NULL instead of 0

you could do, set it to NULL in case its empty, like:

$company_phone = $this->input->post('company_phone');

...
'company_phone'  => (!empty($company_phone)) ? $company_phone : NULL,
...

I just found an automatic solution. You simply should extend CI_Input class by overriding the post() method.

Create a MY_Input class in /application/core/MY_Input.php

class MY_Input extends CI_Input {
    public function post($index = NULL, $xss_clean = NULL)
    {
        $value= $this->_fetch_from_array($_POST, $index, $xss_clean);
        return $value === '' ? null : $value;
    }
}

From now on, every empty POST value will be converted to NULL.


//empty to null로
function empty2null($v){
    return empty($v) ? null : $v;
}