nette/database

Date from mysql is wrong converted to Nette/Utils/DateTime

Opened this issue · 2 comments

  • bug report? yes
  • feature request? no
  • version: 2.4.2

Description

In my application, I read date from database, and write it to other table.
Date "0000-00-00 00:00:00" is bad converted to php, and cannot be saved back to mysql.

Exception:
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '-0001-11-30 00:00:00' for column 'date' at row 1

Steps To Reproduce

In database I have date "0000-00-00 00:00:00", data type is datetime not null. InnoDB.
I use simple NDBT select

$row = $this->table(..)->wherePrimary(...)->fetch();
echo $row->date; // prints "-0001-11-30 00:00:00"
// and write it back
$this->table(...)->wherePrimary(...)->update(['date' => $row->date]); // it throws exception, mysql cannot save this date

PHP 7.1.2, mysql 5.6.22, I use InnoDB for all tables.

hrach commented

0000-00-00 00:00:00 is not a valid datetime; make to column nullable. such value may cause problems also directly in the mysql.

For interest Mysql 5.7 does not allow invalid datetime "0000-00-00 00:00:00". Let's prepare your databese :) Like @hrach recommended you.