Magento 集合中的 addAttributeToFilter 和 OR 条件

发布于 2024-10-22 01:04:52 字数 503 浏览 2 评论 0原文

我想根据不同属性的几个标准来选择产品。

我知道如何使用 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

老街孤人 2024-10-29 01:04:52

是的。

$collection->addAttributeToFilter(
    array(
        array('attribute' => 'someattribute', 'like' => 'value'),
        array('attribute' => 'otherattribute', 'like' => 'value'),
        array('attribute' => 'anotherattribute', 'like' => 'value'),
    )
);

yes it is.

$collection->addAttributeToFilter(
    array(
        array('attribute' => 'someattribute', 'like' => 'value'),
        array('attribute' => 'otherattribute', 'like' => 'value'),
        array('attribute' => 'anotherattribute', 'like' => 'value'),
    )
);
淡看悲欢离合 2024-10-29 01:04:52

如果您希望对不使用 EAV 的集合使用 addFieldToFilter 函数相同的内容,则可以使用以下格式:

$collection->addFieldToFilter(
array(
   'someattribute',
   'otherattribute',
   'anotherattribute',
),
array(
    array('like' => 'value'),
    array('like' => 'value'),
    array('like' => 'value'),
));

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:

$collection->addFieldToFilter(
array(
   'someattribute',
   'otherattribute',
   'anotherattribute',
),
array(
    array('like' => 'value'),
    array('like' => 'value'),
    array('like' => 'value'),
));
我早已燃尽 2024-10-29 01:04:52

实现“OR”的另一件事是:

->addFieldToFilter(
     'type_id',
     ['in' => ['simple', 'configurable']]
)

Another thing to look at to achieve 'OR" is:

->addFieldToFilter(
     'type_id',
     ['in' => ['simple', 'configurable']]
)
我还不会笑 2024-10-29 01:04:52

对于添加属性过滤器:

$collections = Mage::getModel('sales/order')->getCollection()
             ->addAttributeToFilter('increment_id', array('in' => $sellerIncrementIds))
             ->addAttributeToFilter('status', ['in' => ['pending','pending_seller_confirmation']]);

For addAttributeToFilter:

$collections = Mage::getModel('sales/order')->getCollection()
             ->addAttributeToFilter('increment_id', array('in' => $sellerIncrementIds))
             ->addAttributeToFilter('status', ['in' => ['pending','pending_seller_confirmation']]);
何以笙箫默 2024-10-29 01:04:52

addAttributeToFilter 条件
SQL 中有很多运算符,只要使用正确的语法,addAttributeToFilter 就会接受所有运算符。我在下面列出了它们并提供了示例。

等于:eq
这是默认运算符,不需要指定。您可以在下面看到如何使用该运算符,以及如何跳过它并仅输入您正在使用的值。

$_products->addAttributeToFilter('status', array('eq' => 1)); // Using the operator
$_products->addAttributeToFilter('status', 1); // Without using the operator

不等于 - neq

$_products->addAttributeToFilter('sku', array('neq' => 'test-product'));

Like - like

$_products->addAttributeToFilter('sku', array('like' => 'UX%'));

关于 like 需要注意的一件事是,您可以包含 SQL 通配符,例如百分号,它可以匹配任何字符。

Not Like - nlike

$_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));
In - in
$_products->addAttributeToFilter('id', array('in' => array(1,4,98)));
When using in, the value parameter accepts an array of values.

Not In - nin

$_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));
NULL - null
$_products->addAttributeToFilter('description', array('null' => true));

Not NULL - notnull

$_products->addAttributeToFilter('description', array('notnull' => true));
Greater Than - gt
$_products->addAttributeToFilter('id', array('gt' => 5));

小于 - lt

$_products->addAttributeToFilter('id', array('lt' => 5));
Greater Than or Equals To- gteq
$_products->addAttributeToFilter('id', array('gteq' => 5));

小于或等于 - lteq

$_products->addAttributeToFilter('id', array('lteq' => 5));

调试 SQL 查询
在 Magento 中加载集合时,有两种方法可以调试正在执行的查询。

/*
 * This is the better way to debug the collection
 */
$collection->load(true);

/*
 * This works but any extra SQL the collection object adds before loading
 * may not be included
 */
echo $collection->getSelect();

参考链接 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.

$_products->addAttributeToFilter('status', array('eq' => 1)); // Using the operator
$_products->addAttributeToFilter('status', 1); // Without using the operator

Not Equals - neq

$_products->addAttributeToFilter('sku', array('neq' => 'test-product'));

Like - like

$_products->addAttributeToFilter('sku', array('like' => 'UX%'));

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

$_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));
In - in
$_products->addAttributeToFilter('id', array('in' => array(1,4,98)));
When using in, the value parameter accepts an array of values.

Not In - nin

$_products->addAttributeToFilter('id', array('nin' => array(1,4,98)));
NULL - null
$_products->addAttributeToFilter('description', array('null' => true));

Not NULL - notnull

$_products->addAttributeToFilter('description', array('notnull' => true));
Greater Than - gt
$_products->addAttributeToFilter('id', array('gt' => 5));

Less Than - lt

$_products->addAttributeToFilter('id', array('lt' => 5));
Greater Than or Equals To- gteq
$_products->addAttributeToFilter('id', array('gteq' => 5));

Less Than or Equals To - lteq

$_products->addAttributeToFilter('id', array('lteq' => 5));

Debugging The SQL Query
There are two ways to debug the query being executed when loading a collection in Magento.

/*
 * This is the better way to debug the collection
 */
$collection->load(true);

/*
 * This works but any extra SQL the collection object adds before loading
 * may not be included
 */
echo $collection->getSelect();

Reference Link https://fishpig.co.uk/magento/tutorials/addattributetofilter/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文