[ACCEPTED]-Magento addFieldToFilter allow NULLs-magento

Accepted answer
Score: 56

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

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

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

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

Hope this is still helpful...

Score: 25

This works for NOT NULL filters

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

0

Score: 19

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

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

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

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

Score: 7

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

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

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

You don't need to use addFieldToFilter.

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


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

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

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

app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php

More Related questions