繁体   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