Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6615063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:24:58+00:00 2026-05-25T20:24:58+00:00

What is the difference between addAttributeToFilter() and addFieldToFilter() when dealing with Magento collections? Does

  • 0

What is the difference between addAttributeToFilter() and addFieldToFilter() when dealing with Magento collections? Does it have anything to do with whether or not the value is an eav_attribute?

EDIT: According to http://www.magentocommerce.com/wiki/5_-_modules_and_development/catalog/using_collections_in_magento:

addFieldToFilter($attribute, $condition=null) — alias for addAttributeToFilter()

Can anybody confirm this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:24:58+00:00Added an answer on May 25, 2026 at 8:24 pm

    Apologies, there’s no short answer that doesn’t mislead or confuse things here.

    Important Point #1: The Magento core team is loath to remove or rename methods from the source tree. Startup culture meant they eschewed tests plus being a public project it meant they couldn’t control what people did with their product. Rather than remove methods and risk breaking things, they’ll leave methods in place that call the new method. This way, even if there’s old code out there that calls the method, they’re covered.

    Important Point #2: The collection inheritance chain is screwy, and has been inconsistently applied in certain parts of the codebase. This is being cleaned up, but it can still easily throw you for a loop.

    Important Point #3: I’m speculating on how a lot of this is meant to be used and happened. I am not the final authority here, I’m just someone trying to make sense of it. The specifics below refer to 1.6, but the concepts apply to all versions

    All collections inherit from the class Varien_Data_Collection_Db. This is the class that models the basic concept of “collecting a series of objects loaded from the database”. This class has a single method addFieldToFilter.

    public function addFieldToFilter($field, $condition=null)
    {
        $field = $this->_getMappedField($field);
        $this->_select->where($this->_getConditionSql($field, $condition), null, Varien_Db_Select::TYPE_CONDITION);
        return $this;
    }
    

    which is a simple implementation that adds a where clause to the theoretical query.

    Next up, there are two abstract classes that have Varien_Data_Collection_Db as an ancestor. Mage_Core_Model_Resource_Db_Collection_Abstract and Mage_Eav_Model_Entity_Collection_Abstract.

    Mage_Core_Model_Resource_Db_Collection_Abstract is the collection class for “regular, non EAV models”. It has neither a addFieldToFilter method or an addAttributeToFilter method. It relies on the implementation in on the base Varien_Data_Collection_Db class.

    Mage_Eav_Model_Entity_Collection_Abstract is the collection class for EAV models. It has an addAttributeToFilter method, which is more complex.

    public function addAttributeToFilter($attribute, $condition = null, $joinType = 'inner')
    {
        if ($attribute === null) {
            $this->getSelect();
            return $this;
        }
    
        if (is_numeric($attribute)) {
            $attribute = $this->getEntity()->getAttribute($attribute)->getAttributeCode();
        } else if ($attribute instanceof Mage_Eav_Model_Entity_Attribute_Interface) {
            $attribute = $attribute->getAttributeCode();
        }
    
        if (is_array($attribute)) {
            $sqlArr = array();
            foreach ($attribute as $condition) {
                $sqlArr[] = $this->_getAttributeConditionSql($condition['attribute'], $condition, $joinType);
            }
            $conditionSql = '('.implode(') OR (', $sqlArr).')';
        } else if (is_string($attribute)) {
            if ($condition === null) {
                $condition = '';
            }
            $conditionSql = $this->_getAttributeConditionSql($attribute, $condition, $joinType);
        }
    
        if (!empty($conditionSql)) {
            $this->getSelect()->where($conditionSql, null, Varien_Db_Select::TYPE_CONDITION);
        } else {
            Mage::throwException('Invalid attribute identifier for filter ('.get_class($attribute).')');
        }
    
        return $this;
    }
    

    That’s because an attribute query is not a straight “where” query. Also, this method was designed to take either an attribute name, or an attribute database ID, or an instantiated attribute object. You are not adding a field to the filter, you’re adding an attribute to the filter. So, depending on the implementation of EAV and which table the attribute is stored in, you need to add a different bit of SQL code (a straight where query on the main table, a where added with one of the join tables, etc.)

    This creates a problem. Because this EAV collection object inherits from the base collection object, the addFieldToFilter still exists, and would still add a basic where condition to the EAV query, which might confuse an end user by not doing what they thought. Therefore, the EAV collection class also has this

    public function addFieldToFilter($attribute, $condition = null)
    {
        return $this->addAttributeToFilter($attribute, $condition);
    }
    

    which wraps any call to addFieldToFilter to addAttributeToFilter (again, on an EAV model). So, if you have an EAV model, you can use addFieldToFilter or addAttributeToFilter. If you’re working with a regular model, you may only call addFieldToFilter, addAttributeToFilter doesn’t exist.

    Ideally, the method names would have been unified from the start, but once the split happened, the Magento team chose to continue supporting the split in favor of backwards compatibility.

    Wait, There’s More

    There’s two collections left in the codebase that inherit directly from Varien_Data_Collection_Db. These are Mage_Sales_Model_Resource_Sale_Collection and Mage_Review_Model_Resource_Review_Summary_Collection. This number is higher in versions of Magento CE prior to 1.6. While this doesn’t impact the question of filtering, it does confuse the inheritance chain, so you should watch out for it.

    Many non-EAV collections will implement their own addFieldToFilter to do sanity checks on variables, or jigger the query paramaters a little if they’re doing something a little non standard.

    The EAV collections get in on this act as well by redefining addAttributeToFilter. Again, this is done to add custom logic that doesn’t fit in with the basic Magento collection loading.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Difference between Map and Properties as both have key-value pair.
Is there a performance difference between i++ and ++i if the resulting value is
Whats difference between library EXT-JS , EXT-CORE , EXT-GWT as i have worked on
difference between web services clients results in one error in one success I have
The difference between reference types and value types is often confusing for beginners due
Does anyone know difference between Struts2 and Spring 3 MVC. I know the difference
Difference between value parameter and reference parameter ? This question is asked sometime by
Is there any difference between :key => value (hashrocket) and key: value (Ruby 1.9)
The difference between a JavaScript Array , and Object is not very big. In
Difference between a bus error and a segmentation fault? Can it happen that a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.