Add Multiple Field Filter (addFieldToFilter) and Multiple Attribute Filter (addAttributeToSelect) in Magento.

Add Multiple Field Filter (addFieldToFilter) and Multiple Attribute Filter (addAttributeToSelect) in Magento.
Facebooktwitterredditpinterestlinkedinmail

There are the following difference between
addAttributeToSelect() : As you know that the magento prducts are having many attributes, so the addAttributeToSelect() method is used to select the attributes that we want to use. Its retrive only attributes its not filter the collection, however when we use addAttributeToSelect(‘*’) its select all attriutes of the products.

addAttributeToFilter() : This method is used to filter the attributes, its used to filter the collection based on the specific conditional parametters.

addFieldToFilter() : This method is used to filter collection but non-EAV models.

Example :

In below mentioned code we have used product collection and selected name and price attribute with addAttributeToSelect() method and filter the collection with greater than equal to 5 with addAttributeToFilter() method.

$collections = Mage::getModel('catalog/product')
       ->getCollection()
       ->addAttributeToSelect(array('name', 'price'))
       ->addAttributeToFilter('price',array('eq' => 5.00))
       ->load();

Below is the list of conditions that you use to filter the collection:

Attribute Code SQL Equivalent Example
eq =
      $collections->addAttributeToFilter('price',
      array('eq' => 5.00));
      
neq !=
      $collections->addAttributeToFilter('price',
      array('neq' => 5.00));
      
like LIKE
      $collections->addAttributeToFilter('name',
      array('like' => '%Pattern%'));
      
nlike NOT LIKE
      $collections->addAttributeToFilter('name',
      array('nlike' => '%Pattern%'));
      
in IN ()
      $collections->addAttributeToFilter('id',
      array('in' => array(1,2,3,4,5)));
      
nin NOT IN ()
      $collections->addAttributeToFilter('id',
      array('nin' => array(1,2,3,4,5)));
      
is IS
notnull IS NOT NULL
$collections->addAttributeToFilter('description', 'notnull');
null IS NULL
$collections->addAttributeToFilter('description',
      'null'); 
moreq >=
$collections->addAttributeToFilter('price',
      array('moreq' => 5.00));
gt >
$collections->addAttributeToFilter('price',
      array('gt' => 5.00));
lt <
$collections->addAttributeToFilter('price',
      array('lt' => 5.00));
gteq >=
$collections->addAttributeToFilter('price',
      array('gteq' => 5.00));
lteq <=
$collections->addAttributeToFilter('price',
      array('lteq' => 5.00));
finset FIND_IN_SET()
$collections->addAttributeToFilter('custom',
      array('finset' => '1'));
from >=
$collection->addAttributeToFilter('created_at',
      array(
      'from' => '10 December 2014',
      'to' => '15 December 2014',
      'date' => true
      ));
to <=
date
datetime
$collection->addAttributeToFilter('created_at', array(
      'from' => '2014-10-27 00:00:00',
      'to' => '2014-12-27 00:00:00',
      'datetime' => true
      ));

There are few below mentioned examples :

Add Multiple Field Filter.

<?php
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->addFieldToFilter('status',array('complete','pending','pending_payment','pending_paypal','processing'));
?>

Add Single Field Filter.

<?php
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->addFieldToFilter('status','complete');
?>

Add Multiple Attribute Filter.

<?php
$collection = Mage::getModel('sales/order')->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter(
            array(
                array('attribute'=>'status','eq'=>'complete'),
                array('attribute'=>'status', 'eq'=>'processing')
            )
        );
?>

Add Single Attribute Filter.

<?php
$collection = Mage::getModel('sales/order')->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('status',array('eq'=>'complete'));
?>
Facebooktwitterredditpinterestlinkedinmail