Magento:使用分组子句过滤集合
我想用分组子句过滤集合。在 SQL 中,这看起来像:
SELECT * FROM `my_table` WHERE col1='x' AND (col2='y' OR col3='z')
如何将其“翻译”为使用 ->addFieldToFilter(...)
过滤集合?
谢谢!
I would like to filter a collection with grouped clauses. In SQL this would look something like:
SELECT * FROM `my_table` WHERE col1='x' AND (col2='y' OR col3='z')
How can I "translate" this to filtering a collection with ->addFieldToFilter(...)
?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的集合是 EAV 类型,那么这很有效:
但是,如果您坚持使用平面表,我认为
addFieldToFilter
的工作方式不完全相同。一种替代方法是直接使用选择对象。但这的失败在于运营商的命令。您将得到类似
SELECT * FROM my_table WHERE (col1='x') AND (col2='y') OR (col3='z')
的查询。 OR 在这里并不优先,绕过它意味着更具体......传递不带引号的值是不安全的,这里使用适配器来安全地引用它们。
最后,如果
col2
和col3
实际上是相同的,如果您对单个列中的值进行 OR 运算,那么您可以使用以下简写:If your collection is an EAV type then this works well:
However if you're stuck with a flat table I don't think
addFieldToFilter
works in quite the same way. One alternative is to use the select object directly.But the failing of this is the order of operators. You willl get a query like
SELECT * FROM my_table WHERE (col1='x') AND (col2='y') OR (col3='z')
. TheOR
doesn't take precedence here, to get around it means being more specific...It is unsafe to pass values unquoted, here the adapter is being used to safely quote them.
Finally, if
col2
andcol3
are actually the same, if you're OR-ing for values within a single column, then you can use this shorthand: