addFiledToFilter Condition In Magento

As I already mentioned on the previous post that we only can give the condition of attribute name to filter the result. There is another method to filter result that you can use in your customer Module. Let you are having a customer module which is responsible to show some banner on the homepage. For that let you have some place to upload image and also have feature to give sort order and enable disable feature. Here in the below code I will show how to filter the result with addFieldtoFilter

$banner = Mage::getModel('mymodulename/banner')->getCollection();

Now I wish to show banner whose status are active. I already have a field name/column name 'status' in my custom table.

$banner -> addFieldToFilter('status', array('eq' => 1));

Now I will sort the result according to sort order in my table. I already have a field name 'sort_order' in my table. To sort according to that column I will write

$banner -> setOrder('sort_order', 'DESC');

addAttributeToFilter Condition In Magento

addAttributeToFilter Condition is used to Filter the collection query of magento . we can use to to filter the result of category collection, Product Collection, Customer collection, Order collection etc.

When you use this condition into the collection make sure your filterable option should be an attribute not should be a table column or fieldname.

Below is the example to fetch all products and using addAttributeToFilter to filter the result

$collection = Mage::getModel('catalog/product')->getCollection();

Below are all types of condition which you can use To Filter the result

$collection -> addAttributeToFilter('status', array('eq' => 1));
$collection -> addAttributeToFilter('status', array('neq' => 0));
$collection -> addAttributeToFilter('id' , array('in' => array(1,2,3,4)));
$collection -> addAttributeToFilter('id' , array('nin' => array(5,6)));
$collection -> addAttributeToFilter('sku' , array('like' => "%a%"));
$collection -> addAttributeToFilter('sku' , array('nlike' => "test%"));
$collection -> addAttributeToFilter('weight', array('gt' => 10));
$collection -> addAttributeToFilter('weight', array('lt' => 1));
$collection -> addAttributeToFilter('name' , 'notnull');
$collection -> addAttributeToFilter('sort_description' , 'null');