CodeIgniter Forums
How to insert null date using ActiveRecord? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to insert null date using ActiveRecord? (/showthread.php?tid=55864)



How to insert null date using ActiveRecord? - El Forum - 11-13-2012

[eluser]jprateragg[/eluser]
I need to insert a null date using ActiveRecord. I have a helper function that converts the date to the MySQL date format:

Code:
function convert_date($input) {
if(strlen($input) > 0) {
  $d = preg_split('#[-/:. ]#', $input);
  if (is_array($d) && count($d) == 3) {
   if (checkdate($d[0], $d[1], $d[2])) {
    $output = "$d[2]-$d[0]-$d[1]";
   }
  }
} else {
  $output = "NULL";
}
return $output;
}

Here is my model where I save the data:

Code:
public function save_analysis_history($data) {
$this->db->where('id', $this->analysis_id);
$this->db->update('analysis_history', $data);
}

This is the error I'm getting:

Code:
Error Number: 1292

Incorrect date value: 'NULL' for column 'bs_effective_date' at row 1

UPDATE `analysis_history` SET `bs_effective_date` = 'NULL' WHERE `id` = '156'

Is it possible to get ActiveRecord to not add single quotes to NULL?


How to insert null date using ActiveRecord? - El Forum - 11-13-2012

[eluser]PhilTem[/eluser]
Code:
function convert_date($input) {
if(strlen($input) > 0) {
  $d = preg_split('#[-/:. ]#', $input);
  if (is_array($d) && count($d) == 3) {
   if (checkdate($d[0], $d[1], $d[2])) {
    $output = "$d[2]-$d[0]-$d[1]";
   }
  }
} else {
  $output = NULL;
}
return $output;
}

Or you set the table field column to "DEFAULT NULL"


How to insert null date using ActiveRecord? - El Forum - 11-13-2012

[eluser]Aken[/eluser]
Just use PHP's standard NULL value, instead of passing a string "NULL".


How to insert null date using ActiveRecord? - El Forum - 11-14-2012

[eluser]jprateragg[/eluser]
@PhilTem and Aken--that fixed it. Thanks!