[英]Magento collection - filter by several fields
Using Magentos collection models, how should I go about adding a query part/filter such as this: 使用Magentos集合模型,我该如何添加查询部分/过滤器,例如:
WHERE (main_table.x < 1 OR (main_table.x - main_table.y) >= 5)
Update I'm now running this: 更新我现在运行这个:
$this->getSelect()
->where('main_table.x < 1')
->orWhere('(main_table.x - main_table.y) >= :qty');
$this->addBindParam(':qty', $qty);
Result: 结果:
SELECT ... WHERE ... AND ... AND (main_table.x < 1) OR ((main_table.x - main_table.y) >= :qty) ORDER BY ...
The issue is that I can't seem to get to bind $qty
to :qty
问题是我似乎无法将
$qty
绑定到:qty
Update 2 I ended up with this, since I needed the OR
within parentheses 更新2我最终得到了这个,因为我在括号内需要
OR
$this->getSelect()->where('(main_table.x < 1 OR (main_table.x - main_table.y) >= ?)', $qty);
When you use the getSelect
method you're by-passing Magento's model collection interface. 当你使用
getSelect
方法时,你正在绕过Magento的模型集合接口。 Sometimes this is the only way to get the exact select query you want, but keep in mind it may not gel 100% with what the Magento model interface is doing. 有时这是获得所需精确选择查询的唯一方法,但请记住,它可能不会与Magento模型界面正在进行100%凝聚。
When you use the bindParamater
method you're using the Magento model interface. 当您使用
bindParamater
方法时,您正在使用Magento模型接口。 I can't speak to why it's not working, but I suspect the Zend select object and the Magento model collection objects bind their paramaters at different times, and in a different way. 我不能说为什么它不起作用,但我怀疑Zend选择对象和Magento模型集合对象在不同的时间以不同的方式绑定它们的参数。
To get the results you want, skip the bindParamater
method and use the simpler ?
要获得所需的结果,请跳过
bindParamater
方法并使用更简单的方法?
parameter replacement of the orWhere
method. 参数替换
orWhere
方法。
$this->getSelect()
->where('main_table.x < 1')
->orWhere('(main_table.x - main_table.y) >= ?',$qty);
Blockquote The issue is that I can't seem to get to bind $qty to :qty
Blockquote问题是我似乎无法将$ qty绑定到:qty
Well it's actually not an issue it's the way PDO/MySQL engine is working with query statement preparation and binding parameters - which are submitted separately - and query execution afterwards. 嗯,它实际上不是一个问题,它是PDO / MySQL引擎使用查询语句准备和绑定参数(单独提交)和后续查询执行的方式。
So it's not on the DB abstraction layer to generate the final query statement if you're using Bind Parameters 因此,如果您正在使用绑定参数,则不会在DB抽象层上生成最终查询语句
See this stackoverflow question and PDO manual . 请参阅此stackoverflow问题和PDO手册 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.