English 中文(简体)
Magento addFieldToFilter allow NULLs
原标题:
  • 时间:2009-11-20 03:20:53
  •  标签:
  • magento

When using the Magento collection method addFieldToFilter is it possible to allow filtering by NULL values? I want to select all the products in a collection that have a custom attribute even if no value is assigned to the attribute.

最佳回答

You don t need to use addFieldToFilter.

now the solution:
attributes name is known as code in magento, you just need to use the code below to get all of the products which have a specific attribute as an array


$prodsArray=Mage::getModel( catalog/product )->getCollection()
                                  ->addAttributeToFilter( custom_attribute_code )
                                  ->getItems();

you can also specify certain conditions for attribute s value in addAttributeToFilter in the second parameter of addAttributeToFilter.

you can find this method in this file (for further study):

app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php
问题回答

I see you already found a solution, but there is also this option:

$collection->addFieldToFilter( parent_item_id , array( null  => true));

But if you want to use "NULL" => false, which DOESN T WORK. (and I noticed you can use elements such as "in", "nin", "eq", "neq", "gt"), you can do this:

$collection->addFieldToFilter( parent_item_id , array( neq  =>  NULL  ));

Hope this is still helpful...

This works for NOT NULL filters

$collection->addFieldToFilter( parent_item_id , array( notnull  => true));

Filtering a product collection by null/empty attributes has two possible solutions. Magento uses an INNER JOIN to grab the values of the attributes to filter. BUT if the product attribute is not assigned a value the join will fail, as a database table / relationship is missing.

Solution #1: Use addAttributeToFilter() and change the join type from "inner" (the default) to "left":

$collection = Mage::getModel( catalog/product )->getCollection();
$collection->addAttributeToFilter( custom_attribute , array( ... condition options ..),  left );

Solution #2: Make sure your custom attribute has a default value. Magento is conservative in this regard. Magento will only create the relationship between an attribute and a product if a value is given for the attribute. So in the absence of user-specified value or a default value the attribute will not be accessible for filtering a product even if the attribute appears in the product detail view under the admin panel.

Because the question does not match exactly the title of the question and I found the them multiple times by searching for a condition like: special VALUE OR NULL

If you want to filter a collection matching a VALUE OR NULL, then you can use:

$collection->addFieldToFilter( <attribute> , array(
  array( eq  =>  <value> ),
  array( null  => true)
));




相关问题
Magento addFieldToFilter allow NULLs

When using the Magento collection method addFieldToFilter is it possible to allow filtering by NULL values? I want to select all the products in a collection that have a custom attribute even if no ...

Get product link from Magento API

I am new to Magento and using their API. I need to be able to get the product url from the API call. I see that I can access the url_key and url_path, but unfortunately that s not necessarily what ...

magento extension installation

I want to install a Magento extension in WAMP, but not from the Magento connect system. How can I do this? I have the module (extension) code and I already installed the sample data in the Magento ...

Link to a specific step in onepage checkout

Is it possible to redirect the browser to nth step in the onepage checkout? If so, how would one go about doing it? I m working on a payment module and have a sort of "cancel" action that i would ...

How do I filter a collection by a YesNo type attribute?

I have a ‘featured’ attribute, which has a Yes/No select-list as the admin input. I presume that the values for Yes and No are 1 and 0, as they are for every other Yes/No list. However, if I try and ...

热门标签