Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Set a Value to NULL when using Zend_Db

When performing UPDATE and INSERT queries using Zend_Db, I frequently need to set values equal to NULL (not ''). However, the default behavior of Zend_Db::insert() and Zend_Db::update() seems to be that values that are empty are translated into empty strings ('') and put into the database as such.

Does anyone know of way to actually force a NULL value to go into fields if the value is empty in php?

like image 483
Noah Goodrich Avatar asked Jul 23 '09 18:07

Noah Goodrich


3 Answers

Try setting the affected fields to: new Zend_Db_Expr('NULL')

like image 106
William Avatar answered Sep 21 '22 03:09

William


I've always just been able to do this using PHP's null:

$toUpdate = array('nullValue' => null, 'otherValue' => 'something');
Zend_Db::update($table, $toUpdate, $where);
like image 41
Johrn Avatar answered Sep 22 '22 03:09

Johrn


As Johrn I was able to do this with PHP's null value:

$value = null;
$what = array($columnName => $value);
$where = $this->_dbTableName->getAdapter()->quoteInto('Id = ?', $dbRecord->Id);
$this->_dbTableName->update($what, $where);

but encountered situations where the database itself had the column set to NOT NULL and in such cases the value was indeed converted to either empty string or in case of FLOATs to 0.00. I guess INT column would end up as 0 ;-)

like image 26
silverdr Avatar answered Sep 21 '22 03:09

silverdr