Magento Collection Filter
Magento Collection Filter : Magento collection is basically model type which contains the others models. Collections are used for loading data from data. You can join tables, add filter to them. You can perform joinInner(), joinLeft(), joinRight(), joinFull(), joinCross(),joinNatural() using collections.Varien_Data_Collection is used for collections in magento. Here in this tutorial we are going to explain how to use collection for various purpose efficiently. We will cover collection with syntax, example and uses.
Magento Collection Filter
Here are the details about the magento collection-
Magento get Collection
Magento Get Custom Collection Example:
$collection = Mage::getModel('ModuleName/ModelName')->getCollection(); |
Where ModuleName – is name of module and ModelName is name of model you want to load.
Magento collection addfieldtofilter
Here a simple example to add the filter to the field –
Magento collection addfieldtofilter:
$sku = 'testsku'; $collection = Mage::getModel('catalog/product')->getCollection() ->addFieldToFilter('sku', $sku); $data = $collection->getData(); print_r($data); |
When you will run the above example it will give you the product detail. The above example will produce the following query –
SELECT `e`.* FROM `catalog_product_entity` AS `e` WHERE (`e`.`sku` = ‘testsku’)
Magento collection addAttributeToFilter
Here a simple example to add the filter to the field –
magento collection add attribute to filter:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('status', 1); foreach($collection as $product){ echo $product->getId()."<br>"; echo $product->getSku()."<br>"; } |
The example will give you all products where status is 1 ie. they are enabled.
Magento addAttributeToFilter Conditions
Let have look over other conditions of addAttributeToFilter in magento.
Magento addAttributeToFilter Equal : eq (=)
Here is an example to add the equal condition in magento collection.
Magento addAttributeToFilter Equal Condition:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('status', array('eq' => 1)); // With operator $collection->addAttributeToFilter('status', 1); // Without operator |
The above example will give the results where status = 1;
Magento addAttributeToFilter Not Equal : neq (!=)
Here is an example to add the not equal condition in magento collection.
Magento add Attribute To Filter Not Equal Condition:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('status', array('neq' => 1)); // With operator |
The above example will give the results where status != 1;
Magento addAttributeToFilter Like : like(%%)
Here is an example to add the like filter in magento collection.
Magento add Attribute To Filter like condition example:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('sku', array('like' => '%abc%')); // With operator |
The above example is used for magento collection like filter query. This example will give the results which fulfills the like condition.
Magento addAttributeToFilter Not Like : nlike(%%)
Here is an example to add the not like filter in magento collection.
Magento add Attribute To Filter Not like condition example:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('sku', array('nlike' => '%abc%')); // With operator |
The above example is used for magento collection Not like filter query. This example will give the results which fulfills the not like condition.
Magento addAttributeToFilter In : in
Magento collection in clause can be generated simply as below-
Magento add Attribute To Filter in condition example:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('id', array('in' => array('1','2','3'))); // With operator |
The above magento collection in clause will give you the results where id is equal to 1, 2 and 3.
Magento addAttributeToFilter Not In : nin
Magento collection not in clause can be generated simply as below-
Magento add Attribute To Filter not in condition example:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('id', array('nin' => array('1','2','3'))); // With operator |
The above magento collection not in clause will give you the results where id is not equal to 1, 2 and 3.
Magento addAttributeToFilter NUll : null
Magento collection null clause can be generated simply as below-
Magento add Attribute To Filter null condition example:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('short_description', array('null' => true)); // With operator |
The above magento collection null clause will give you the results short_description is null.
Magento addAttributeToFilter NOT NUll : notnull
Magento collection not null clause can be generated simply as below-
Magento add Attribute To Filter not null condition example:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('short_description', array('notnull' => true)); // With operator |
The above magento collection not null clause will give you the results short_description is not null.
Magento addAttributeToFilter Less Than : lt(<)
Magento collection less than clause can be generated simply as below-
Magento addAttributeToFilter Less Than : lt condition example:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('id', array('lt' => 100)); // With operator |
The above magento collection less than clause will give you the results where id < 100.
Magento addAttributeToFilter Less Than Or Equals To : lteg(<=)
Magento collection less than or equals to clause can be generated simply as below-
Magento addAttributeToFilter Less Than Or Equals to : lteq condition example:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('id', array('lteq' => 100)); // With operator |
The above magento collection less than or equals to clause will give you the results where id <= 100.
Magento addAttributeToFilter Greater Than : gt(>)
Magento collection greater than clause can be generated simply as below-
Magento addAttributeToFilter greater Than : gt condition example:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('id', array('gt' => 100)); // With operator |
The above magento collection greater than clause will give you the results where id > 100.
Magento addAttributeToFilter greater Than Or Equals To : gteg(>=)
Magento collection greater than or equals to clause can be generated simply as below-
Magento addAttributeToFilter greater Than equals to : gteq condition example:
$collection = Mage::getModel('catalog/product')->getCollection(); $collection->addAttributeToFilter('id', array('gteq' => 100)); // With operator |
The above magento collection greater than or equals to clause will give you the results where id >= 100.
Advertisements
Add Comment
📖 Read More
- 1. Magento Get Payment Information
- 2. Magento check if customer is logged in
- 3. Magento check if admin user logged in or not
- 4. Magento category ID from product ID
- 5. Magento Create Helper