Search code examples
magento

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 value is assigned to the attribute.


Solution

  • 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