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.
2 Answers
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 addFieldToFilter()
.
Edit:
You may have a look in this file 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()
vsaddAttributeToFilter()
? Oct 30, 2015 at 21:33 -
1The difference is the part of the SQL that is affacted:
addAttributeToSelect()
references to theSELECT
part of a query and selects only specific colums.addFAttributeToFilter()
is used to filter a collection and is therefor added to theWHERE
part. Nov 1, 2015 at 8:15
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.