簡體   English   中英

當有兩個子句時如何使用zend_db_select進行更新

[英]How to use zend_db_select to update when there is two clause

我希望在滿足2個條件時更新表中的條目。

我有這個陳述,但它只適用於條件

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

而不只是檢查只是“id”的位置,我想檢查用戶ID也..

感謝任何幫助。

類似於此的東西應該作為$ where參數將接受並解析數組,在Zend_Db_Adapter_Abstract引用_whereExpr()方法,以獲取有關如何處理$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()可以提供一些有用的功能。

暫無
暫無

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

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