21

I'm bit of confused what is the difference of addAttributeToFilter() and addFieldToFilter() functions. I checked the core files in magento. But I could not figured out a clear idea. Because for some of the collections in magento has used addAttributeToFilter() and some have used addFieldToFilter(). Please any clarification on this would be a great help. Thanks in advance.

44

addAttributeToFilter() is used to filter EAV collections.

addFieldToFilter() is used to filter Non-EAV collections.

EAV-models are for example product, customer, sales, etc so you can use use addAttributeToFilter() for those entities.

addFieldToFilter() is mapped to addAttributeToFilter() for EAV entities. So you can just use addFieldToFiler().

Edit:

You can have a look in app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php where the Mapping is done:

public function addFieldToFilter($attribute, $condition = null) {
    return $this->addAttributeToFilter($attribute, $condition);
}
  • excellent answer. bravo. it allows me to figure out where things are. it informs me that one is usable with EAV even if it can also handle non EAV. it's actionable, and it gives the tools for readers to figure out more, not just the answer to this particular question. Would you mind answering a question about the difference between addAttributeToSelect() vs addAttributeToFilter()? – ahnbizcad Oct 30 '15 at 21:33
  • 1
    The difference is the part of the SQL that is affacted: addAttributeToSelect() references to the SELECT part of a query and selects only specific colums. addFAttributeToFilter() is used to filter a collection and is therefor added to the WHERE part. – Anna Völkl Nov 1 '15 at 8:15
3

With regards to EAV there is no difference.

/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php
public function addFieldToFilter($attribute, $condition = null)
{
    return $this->addAttributeToFilter($attribute, $condition);
}

My assumption, though I could be wrong, is that for EAV they use the attribute naming as each attribute to filter on is in itself a stand alone item that has its own settings etc. Where as when not using EAV it is just another column on a database table and so the name Field works well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.