簡體   English   中英

使用Zend_Db時如何將值設置為NULL

[英]How to Set a Value to NULL when using Zend_Db

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

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

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

我總是能夠使用PHP的null來做到這一點:

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

正如Johrn我能用PHP的null值做到這一點:

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

但遇到數據庫本身將列設置為NOT NULL的情況,在這種情況下,該值確實轉換為空字符串或FLOAT為0.00。 我猜INT列最終會為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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM