Difference Between Field and Attribute in Magento

collection;eav

In Magento terminlogoy, some times variables are named attribute and sometimes name.

While the case of attribute is clear and it means the attribute in the EAV model, the case of a field is not clear.

For example, in the file app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php, there is a function named joinAttribute and there is another joinField, which has two completely different implementations.

But, for example in this file this is the method addFieldToFilter:

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

which seemingly, means that the two are the same.

What is the different between a field and an attribute?

Best Answer

Difference b/w Field and Attribute

There are two kind of models in Magento. 1. Eav entity 2. Flat Entity

Eav entity (Entity-Attribute-Value) uses multiple tables in order to function. Here attribute acts as a middle agent as you can see in the full definition. ie attribute connects an entity to it's real data. In order to achieve this, at-least 3 tables needed. One for entity, another for attribute and finally one for storing real data (ie values). However in case of Magento, this is much more complicated (it involves more than 3 tables) which I dont want to dive into. They key point here is, for an eav entity, attributes is also works like fields. Field means a "space" to hold "something" (in our case, it is values). In that sense, eav attributes are fields of eav entity. Customer, products, categories are examples of eav entity.

Flat entities are normal entities. They use one entity - one table relations. In flat entity table, each column can be treated as fields. So for a flat entity, relation with data is straight forward. It's data scope is somewhat constant and not "BIG" and hence can be manage it's properties and data relation using a single table.

Regarding addFieldToFilter() which you can see in eav collection implmentation.

What you see there is, addFieldToFilter() just act as a wrapper of addAttributeToFilter(). Also it is vital here to read the documentation provided on that method. It says:

Wrapper for compatibility with Varien_Data_Collection_Db

So it gives you a first hint that, Varien_Data_Collection_Db which is the parent class of Mage_Eav_Model_Entity_Collection_Abstract (which is also parent collection class of flat models) also defines addFieldToFilter(). If you look into Varien_Data_Collection_Db::addFieldToFilter(), you can easily find it's implementation is for dealing with flat entities collection filtering, which do not suit for an eav entity filter.

So that method is there for two reasons:

  1. To completely remove conventional collection filtering for eav entities and hence it just wrap the collection filtering method which is dedicated for eav entities, ie addAttributeToFilter().

  2. It also allow us to use both addAttributeToFilter() and addFieldToFilter() for eav entities collection filter.

Hope that clarifies your doubts.