Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Different Methods addAttributeToSelect(), addAttributeToFilter() and addFieldToFilter() in Magento

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 8.71k
    Comment on it

    addAttributeToSelect() :

     

    addAttributeToSelect() method is used to select the attributes  that we want to retrieve from the database. If we want to load all attributes then we will use addAttributeToSelect('*').
    Example:

     

    collections = Mage::getModel('catalog/product')
           ->getCollection()
           ->addAttributeToSelect(array('name', 'price','sku'));
    
    

    In the above example we have created an object of the collection 'catalog/product' and then we defined the attributes that we need to get from the database using the method addAttributeToSelect().  

     

    addAttributeToFilter() :

    addAttributeToFilter() method is used to filter EAV collections in Magento and filter the products based on the attributes.

     

    $collections = Mage::getModel('catalog/product')
           ->getCollection()
           ->addAttributeToSelect(array('name', 'price','sku'))
           ->addAttributeToFilter('sku', array('like' => '%AM%'))
           ->load();

    Note: addFieldToSelect used for flat model,  addAttributeToSelect user for EAV model

     

    addFieldToFilter() :

    addFieldToFilter is a method in Magento used to a collection to perform the do AND and OR operations in the query,

     

    for example :

    $collection = Mage::getModel('catalog/product')->getCollection();
    
    $collection->addFieldToFilter(array(array('attribute' => 'name', 'like' => '%qwerty%')));
    $collection->addFieldToFilter(array(array('attribute' => 'description', 'like' => '%xyz%')));
    
    $collection->load(true);

     

    In the above example we have created a collection object $collection of collections of products of 'catalog/product' and then we have applied this addFieldToFilter() method of Magento to perform the filter in the first case we are selecting those results with name having the string 'qwerty' and then same in 'description' with 'xyz' and then loaded the collection.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: