2013-05-16 15 views
0

我想在滿足2個條件時更新表中的一個條目。如何在有兩個子句時使用zend_db_select來更新

我有這個說法,但它只是一個where條件

$this->dbo->update('mytable', $data, $this->dbo->quoteInto('id= ?', $id)); 

而不是隻檢查其中的只是「身份證」,我想檢查用戶ID也..

任何欣賞幫幫我。

回答

2

類似的東西這個應該爲$在哪裏工作參數將接受並解析一個數組,引用_whereExpr()方法Zend_Db_Adapter_Abstract有關$where ARG是如何處理的代碼:

$this->dbo->update('mytable', $data, array('id= ?'=> $id, 'user_id=?'=>$userId)); 

我要去建議您可能希望改變您的方法並使用Zend_Db_Table_Row的save()方法而不是更新。這是一個例子。

public function saveUser($id, array $userData, $userId = null) 
    { 
     //$this->getDbAdapter is a placeholder for your choice of db adapters, I suggest a DbTable model that extends Zend_Db_Table_Abstract 
     $select = $this->getDbAdapter()->select(); 
     //if userId is not null build a where clause 
     if (!is_null($userId)) {   
      $select->where('user_id = ?', $userId); 
     } 
     //build where clause for primary key, two where() in select() will be 'AND' use orWhere() for 'OR' 
     $select->where('id = ?', $id); 
     //fetch the row you wish to alter 
     $row = $this->getDbAdapter()->fetchRow($select); 
     //assign the data to the row, you can update any or all columns 
     $row->username = $userData[username]; 
     $row->user_id = $userData[user_id]; 
     //ect... 

     //save the new data to the row, will only update changed coluns 
     $row->save(); 
     //return the whole for use in other ways, save() typically only returnbs the primary key. 
     return $row; 
    } 

是的這種方法稍微冗長些,也許觸摸更復雜。然而,當你開始碰撞'INSERT'和'UPDATE'的限制時,save()可以提供一些有用的功能。

相關問題