(official version v1.5.3—not fixed)
function _where($key, $value = NULL, $type = 'AND ')
{
if ( ! is_array($key))
{
$key = array($key => $value);
}
foreach ($key as $k => $v)
{
$prefix = (count($this->ar_where) == 0) ? '' : $type;
if ( ! is_null($v))
{
if ( ! $this->_has_operator($k))
{
$k .= ' =';
}
$v = ' '.$this->escape($v);
}
$this->ar_where[] = $prefix.$k.$v;
}
return $this;
}
I actually challenge the design of this function. NULL is a valid value for a database column, but this code is concatenating together the $k and the $v to form (part of) the WHERE condition. It’s painfully clear how the problem described in this thread can be introduced, by the very design of the function’s prototype (with $value defaulting to NULL). The SQL version of “null” is not gracefully converted from the PHP version, but I think it should.
Query: “Find all products where price is not yet set”
SELECT * FROM products WHERE price IS NULL
I don’t use SQL directly anymore, as my database layer writes it for, so how does one create the WHERE condition of “price IS NULL”?
Like this?
$this->db->where('price', 'IS NULL');
I would prefer smarter handling and auto-conversion from the PHP NULL value:
$this->db->where('price', NULL);
I’m sure the db experts out there have something to say about that, so chime into this exciting thread of discussion. 