Welcome Guest, Not a member yet? Register   Sign In
How to insert null date using ActiveRecord?
#1

[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?
#2

[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"
#3

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

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




Theme © iAndrew 2016 - Forum software by © MyBB