ZF-3389: Zend_Db_Adapter_Pdo_Mysql::_quote() (possibly others) improperly handles null values.

Description

Zend_Db_Adapter_Pdo_Abstract::_quote(NULL) returns an empty string instead of the unquoted string NULL.

This is important when inserting/updating data in a column that allows NULLs. Its especially important when the column is part of a foreign key relation and inserting an empty string instead of NULL results in a SQL error. Other adapters may be affected by this as well, I only use PDO_MYSQL.

A fix is to insert the following code into the Zend_Db_Adapter_Pdo_Abstract class:

protected function _quote($value) { if ($value === NULL) { return 'NULL'; } return parent::_quote($value); }

Comments

Actually, the proposed solution is insufficient to fix this problem as this will still generate an invalid query:

(field = NULL) ≠ (field IS NULL)

At least in MySQL > 5.1.x, I can attest. In our projects we have overridden the WHERE clause builder in Zend_Db_Table_Abstract to produce the correct query strings for set values, such that PHP's NULL value produces SQL's "IS NULL" expression, PHP arrays produce SQL "IN()" expressions, etc.

I am not sure how we'd go about fixing this, you are making the assumption that the database driver contextually understand what you mean by _quote(null).

Perhaps this is a "Won't Fix"?

Will attempt to address this API inconsistency in ZF2