neat-php / object

Neat Object components
MIT License
4 stars 1 forks source link

Recognize 0000-00-00 as NULL instead of DateTime(Immutable) object #34

Open annavanbiemen opened 4 years ago

annavanbiemen commented 4 years ago

When database content has '0000-00-00' or '0000-00-00 00:00:00' values that are to be loaded in DateTime properties, this poses a problem because these entities cannot be stored without causing a database error. The DateTime object, when loaded contains an invalid value:

>>> new DateTime('0000-00-00')
=> DateTime @-62169987208 {#2421
     date: -0001-11-30 00:00:00.0 Europe/Berlin (+00:53),
   }

When tryin to store an entity containing this value in MySQL or MariaDB, you'll get:

Neat\Database\QueryException: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '-0001-11-30 00:00:00' for column 

In all likelyhood, 0000-00-00 was never meant to be a valid date, but a way to express "empty" or "not set". Because PHP's null value is a more appropriate way to represent this, I propose to change the behaviour of DateTime(Immutable) properties to load a value as NULL instead of an invalid DateTime object.

baukevdw commented 4 years ago

You might want to consider fixing the database content instead of making this assumption.

Reasons you want to do this: