2010-09-20 45 views
5

要在Zend Framework的一個MySQL錶行更新使用where數據庫行我有類似:Zend框架 - 更新與WHERE和AND

public function updateBySiteId(array $data, $id) { 
     $table = $this->gettable(); 

     $where = $table->getAdapter()->quoteInto('site_id = ?', $id); 

     return $table->update($data, $where); 
    } 

而這一點,我希望,讓我像.. 。

UPDATE foo SET ponies = 'sparkly' WHERE site_id = '1' 

但是,如果我想創建以下內容:

UPDATE foo SET ponies = 'sparkly' WHERE site_id = '1' AND type = 'zombie' 

在手動我不明白如何用quoteInto(或引用或其他安全方法...這可能意味着我正在尋找錯誤的地方,但... 感嘆)。

回答

9

由於表更新()方法 代理到數據庫適配器 update()方法,第二個參數 可以是SQL表達式的陣列。 表達式組合爲 使用AND運算符的布爾項。

http://framework.zend.com/manual/en/zend.db.table.html

$data = array(
'updated_on'  => '2007-03-23', 
'bug_status'  => 'FIXED' 
); 
$where[] = "reported_by = 'goofy'"; 
$where[] = "bug_status = 'OPEN'"; 
$n = $db->update('bugs', $data, $where); 

產生的SQL是:

UPDATE "bugs" SET "update_on" = '2007-03-23', "bug_status" = 'FIXED' WHERE ("reported_by" = 'goofy') AND ("bug_status" = 'OPEN') 

http://framework.zend.com/manual/en/zend.db.adapter.html#zend.db.adapter.write.update

+2

這做到了。我在手冊中讀到了這一點,但是錯誤地創建了數組:$ where = $ table-> getAdapter() - > quoteInto(array('site_id =?'=> $ id,'foo'=> $ bar)); – Lothar 2010-09-20 23:55:45