2009-07-23 27 views
5

使用Zend_Db執行UPDATE和INSERT查詢時,我經常需要將值設置爲NULL(不是'')。然而,Zend_Db :: insert()和Zend_Db :: update()的默認行爲似乎是將空值轉換爲空字符串('')並放入數據庫中。如何在使用Zend_Db時將值設置爲NULL

有沒有人知道的方法來實際強制NULL值進入字段,如果值爲空的PHP?

回答

19

嘗試將受影響的字段設置爲:new Zend_Db_Expr('NULL')

+1

我喜歡的_abstraction_這種方法,這種方法給你 – 2009-07-24 02:14:29

4

我一直只是能做到這一點使用PHP的空值:

$toUpdate = array('nullValue' => null, 'otherValue' => 'something'); 
Zend_Db::update($table, $toUpdate, $where); 
1

由於Johrn我可以用PHP的空做到這一點值:

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

但遇到數據庫本身將列設置爲NOT NULL的情況,並且在這樣的c ases的值確實轉換爲空字符串或在FLOAT的情況下爲0.00。我猜INT列最終會成爲0 ;-)

0

雖然對於可能訪問此問題的人使用Zend 2.4.7,但我可以在William Lannen的答案中使用這個提供一些靈感。假設getTable()回報Zend\Db\TableGateway對象:

public function fetch() 
{ 
    $data['column_name1 = ?'] = 'column_value'; 
    $data[] = new \Zend\Db\Sql\Predicate\IsNull('column_name2'); 
    $resultSet = $this->getTable()->select($data); 
    if (0 === count($resultSet) { 
     return 'SomeExpectationOrException'; 
    } else { 
     return $resultSet; 
    } 
} 
相關問題