簡體   English   中英

PHP postgres使用WHERE'AND'子句的Zend Framework Select查詢

[英]PHP postgres Zend Framework Select query with WHERE 'AND' clause

好吧我還是Zend的新手,並且試圖找到ORM如何一般地工作,似乎是一項艱巨的任務(任何人都知道他們在哪里有一個特定的文件)。 除了我想要做的是接受現有的查詢並向其添加“AND”子句。 只是不確定如何去做,因為我發現的其他例子看起來不像這個,我寧願避免破壞這個

$select = $this->select()
            ->from('offer_term')
            ->setIntegrityCheck(false)
            ->joinUsing('term_mapping', 'term_id')
            ->where('promo_id = ?', $promo_id);
        return $this->fetchAll($select);

嘗試:

$select = $this->select()
    ->from('offer_term')
    ->setIntegrityCheck(false)
    ->joinUsing('term_mapping', 'term_id')
    ->where('promo_id = ?', $promo_id);
    ->where('name = ?', $name); // WHERE promo_id = $promo_id AND name = $name
return $this->fetchAll($select);

並記得使用手冊

實際上,一旦你開始理解Zend_db是如何工作的,這真的很容易。

您的查詢:

$select = $this->select()
            ->from('offer_term')
            ->setIntegrityCheck(false)
            ->joinUsing('term_mapping', 'term_id')
            ->where('promo_id = ?', $promo_id);
        return $this->fetchAll($select);

你已經在使用select()對象來執行查詢,所以嘗試重構這樣的事情(我將包括如何處理條件):

$select = $this->select()->setIntegrityCheck(false);
$select->from('offer_term');//if query is inside of a DbTable model the from() can be omitted
$select->joinUsing('term_mapping', 'term_id');
$select->where('promo_id = ?', $promo_id);//Every time you add a where(), select() adds the param using the AND operator
$select->where('something_new = ?', $new_param);//if you need to add a param using OR you can use the orWhere() method.
if (TRUE) {
    $select->orWhere('something = ?',$parameter);//This will add an OR WHERE to the query if the condition is true.
}
return $this->fetchAll($select);

只需在鏈中添加另一個where()即可在select()查詢中添加AND 這可以通過鏈接原始查詢來完成,也可以像我一樣使用單獨的語句來完成。 如果需要在select()查詢中使用OR運算符,則可以使用orWhere()方法。

您可以根據需要混合鏈接和單獨的語句,這使得添加條件非常容易。

注意: Zend_Db不是ORM,它是表網關和表行模式的實現(我希望我的名字正確)。 所以請不要期望完整的ORM功能。

希望這可以幫助。

暫無
暫無

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

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