Magento 集合中的 addAttributeToFilter 和 OR 条件
我想根据不同属性的几个标准来选择产品。
我知道如何使用 $collection->addAttributeToFilter('someattribute', array('like' => '%'));
但我想使用几个属性 OR 条件。
例如:
$collection->addAttributeToFilter('someattribute', array('like' => 'value'));`
或
$collection->addAttributeToFilter('otherattribute', array('like' => 'value'));`
获取“someattribute”OR“otherattribute”设置为“value”的产品
是否可能?
I'd like to select products depending on several criteria from different attribute.
I know how to user $collection->addAttributeToFilter('someattribute', array('like' => '%'));
But I'd like to use several attribute for OR condition.
Like:
$collection->addAttributeToFilter('someattribute', array('like' => 'value'));`
OR
$collection->addAttributeToFilter('otherattribute', array('like' => 'value'));`
To get products which either 'someattribute' OR 'otherattribute' set to 'value'
Is it possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。
yes it is.
如果您希望对不使用 EAV 的集合使用 addFieldToFilter 函数相同的内容,则可以使用以下格式:
In case you wish to use the same thing for addFieldToFilter function for collections that are not using the EAV, then you can using the following format:
实现“OR”的另一件事是:
Another thing to look at to achieve 'OR" is:
对于添加属性过滤器:
For addAttributeToFilter:
addAttributeToFilter 条件
SQL 中有很多运算符,只要使用正确的语法,addAttributeToFilter 就会接受所有运算符。我在下面列出了它们并提供了示例。
等于:eq
这是默认运算符,不需要指定。您可以在下面看到如何使用该运算符,以及如何跳过它并仅输入您正在使用的值。
不等于 - neq
Like - like
关于 like 需要注意的一件事是,您可以包含 SQL 通配符,例如百分号,它可以匹配任何字符。
Not Like - nlike
Not In - nin
Not NULL - notnull
小于 - lt
小于或等于 - lteq
调试 SQL 查询
在 Magento 中加载集合时,有两种方法可以调试正在执行的查询。
参考链接 https://fishpig.co.uk/magento/tutorials/addattributetofilter/
addAttributeToFilter Conditionals
There are many operators in SQL and addAttributeToFilter will accept all of them, as long as you use the correct syntax. I've listed them all below and provided examples.
Equals: eq
This is the default operator and does not need to be specified. Below you can see how to use the operator, but also how to skip it and just enter the value you're using.
Not Equals - neq
Like - like
One thing to note about like is that you can include SQL wildcard characters such as the percent sign, which matches any characters.
Not Like - nlike
Not In - nin
Not NULL - notnull
Less Than - lt
Less Than or Equals To - lteq
Debugging The SQL Query
There are two ways to debug the query being executed when loading a collection in Magento.
Reference Link https://fishpig.co.uk/magento/tutorials/addattributetofilter/