简体   繁体   English

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

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

Ok Im still new to Zend, and trying to find how the ORM works in general for it, seems to be posing a difficult task (anyone know where they have a specific document for it). 好吧我还是Zend的新手,并且试图找到ORM如何一般地工作,似乎是一项艰巨的任务(任何人都知道他们在哪里有一个特定的文件)。 That aside what I am trying to do is take an existing query and add a "AND" clause to it. 除了我想要做的是接受现有的查询并向其添加“AND”子句。 Just not sure how to go about doing that, as the examples I have found else where dont look like this one, and I'd rather avoid breakage on this 只是不确定如何去做,因为我发现的其他例子看起来不像这个,我宁愿避免破坏这个

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

Try: 尝试:

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

And remember to using manual . 并记得使用手册

Actually this is really easy once you start understand how Zend_db works. 实际上,一旦你开始理解Zend_db是如何工作的,这真的很容易。

your query: 您的查询:

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

you are already using the select() object for performing the query, so try refactoring to something like this(I'll include how do it with a conditional as well): 你已经在使用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);

Adding an AND in a select() query is done just by adding another where() to the chain. 只需在链中添加另一个where()即可在select()查询中添加AND This can be done by chaining as your original query does or by using separate statements as I have done. 这可以通过链接原始查询来完成,也可以像我一样使用单独的语句来完成。 If you need to use the OR operator in select() query you can use the orWhere() method. 如果需要在select()查询中使用OR运算符,则可以使用orWhere()方法。

You can mix chaining and separate statements as needed, which makes adding a conditional pretty easy. 您可以根据需要混合链接和单独的语句,这使得添加条件非常容易。

NOTE: Zend_Db is not an ORM, it is an implementation of The table gateway and table row patterns (I hope I got the names correct). 注意: Zend_Db不是ORM,它是表网关和表行模式的实现(我希望我的名字正确)。 So please do not expect full ORM functionality. 所以请不要期望完整的ORM功能。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM