I came across what looks like an odd issue with either Zend_Db or PHP’s PDO MySQL driver, that perhaps stems from my lack of knowledge of those two.
Let’s assume I have a MySQL Table with a NULLable TIME field. If I run a query like this in MySQL client:
UPDATE mytable SET mytime = NULL WHERE id = 1;
Everything works as expected and mytime field will hold NULL as value after this query.
However, if I run the exact same query in PHP through the Zend_Db_Adapter, the mytime field is set to '0:0:0' after such query:
$db->getConnection()->exec('UPDATE mytable SET mytime = NULL WHERE id = 1');
How do I set that TIME field to NULL?
I’m using PHP5.3 with PDO MySQL driver, Zend Framework 1.11 and MySQL 5.1.
What you have should work, i.e.:
That should work. I tested it.
Caveat
If the data type time is
NOT NULL, then trying to set it toNULLwill cause the valueNULLto be defaulted to00:00:00, which may be unexpected behaviour e.g.:Trying to insert or update the time field above with
NULLwill cause the value00:00:00to be inserted.This is similar for date, datetime, and a few other data types. e.g. Trying to set a data type datetime which is
NOT NULLtoNULLwill default its value to0000-00-00 00:00:00.NOTE: Mysql will not throw an error when you try to set a
NULLvalue to aNUT NULLdata type, you can change this behaviour by setting MySQL’sSQL_MODEtoSTRICT_ALL_TABLES: see this stackoverflow question.The Fix
Change the field to allow
NULLand it should be fine:Now the time field can be set to
NULL.