PHP postgres Zend Framework Select query with WHERE 'AND


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);
实际上,

一旦您开始了解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。这可以通过像原始查询一样链接或使用单独的语句来完成,就像我所做的那样。如果需要在查询中使用 OR 运算符select()可以使用 orWhere() 方法.

您可以根据需要混合链接和单独的语句,这使得添加条件非常容易.

注意:Zend_Db不是ORM,它是表网关和表行模式的实现(我希望我的名字正确)。所以请不要期望完整的ORM功能。

希望这有帮助。

尝试:

$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);

并记住使用手册。