简体   繁体   English

Zend Framework 1.12 Db Select - 在条件中使用多个问号值的嵌套 WHERE

[英]Zend Framework 1.12 Db Select - nested WHEREs using multiple values for question marks in condition

I'm using Zend Framework 1.12.3.我正在使用 Zend 框架 1.12.3。 I need to create a query that hasnested WHEREs, such as我需要创建一个包含 WHERE 的查询,例如

SELECT * FROM table1
WHERE (id = 'foo' AND name = 'bar') OR (grade = 123)

Here's my attempt这是我的尝试

$this->getDbTable()->select()
->from($this->getDbTable(), array('*')
->where('id = ? AND name = ?', $foo, $bar)
->orWhere('grade = ?', $grade);

However, the outcome is然而,结果是

SELECT * FROM table1
WHERE (id = 'foo' AND name = 'foo') OR (grade = 123)

instead of name = 'bar'而不是name = 'bar'

Basically, I cannot use multiple ?基本上,我不能使用多个? s assigning each ? s 分配每个? a different value.不同的值。 Do you know any solution?你知道有什么解决办法吗?

Thanks谢谢

LE: using a WHERE condition such as ->where("id = $foo and name = $bar") does work, however it doesn't prevent injection attacks like the ? LE:使用 WHERE 条件,例如->where("id = $foo and name = $bar")确实有效,但是它不能防止像? does

Looking at the code, I see no way to do it, the where clause only works with a condition, but I think that adding parentheses, you can manage the AND and OR with the right priorities.查看代码,我看不出有什么办法,where 子句仅适用于条件,但我认为添加括号,您可以以正确的优先级管理 AND 和 OR。

Try this:尝试这个:

this->getDbTable()->select()
    ->from($this->getDbTable(), array('*')
    ->where('(id = ?', $foo) // add '(' before condition
    ->where('name = ?)', $bar) // add ')' after condition
    ->orWhere('grade = ?', $grade);

I would use named params for binding, like so:我会使用命名参数进行绑定,如下所示:

    $sql = $this->getDbTable()->select()
        ->from($this->_name, array('*'))
        ->where('id = :foo AND name = :bar')
        ->orWhere('grade = ?', 'grade');
    $this->getDbTable()->fetchAll($sql, array('foo' => 'foo', 'bar' => 'bar'));

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

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