`
天梯梦
  • 浏览: 13630295 次
  • 性别: Icon_minigender_2
  • 来自: 洛杉矶
社区版块
存档分类
最新评论

Magento: addAttributeToFilter 和 addFieldToFilter 的区别 Difference between addAttri

阅读更多

 addAttributeToFilter is used to filter EAV collections and will filter the products based on the attributes that you've included in your collection.

EAV-models: product, customer, sales, etc.

addFieldToFilter is used to filter Non-EAV collections and will filter the products based on columns in the database from the table catalog_product_entity.

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().

 

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);
}

 

addAttributeToSelect() : addAttributeToSelect() is used to filter the attributes fromthe magento database

$collections = Mage::getModel('catalog/product')
       ->getCollection()
       ->addAttributeToSelect(array('name', 'price','sku'));
	   
//If we use addAttributeToSelect('*'), then all product attributes will be loaded.

 

addAttributeToFilter : addAttributeToFilter() is used to filter EAV collections in Magento,and filter the products based on the attributes that you have included in your collection.

 

addAttributeToFilter add a condition to the WHERE part of the MySQL query, used to extract a product collection from the database using specified condition.

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

 

The above code would get a product collection, with each product having it’s name, price AND sku. The product collection would be filtered and contain only products that have an SKU WITH with AM.
If we use addAttributeToSelect(‘*’), then all product attributes will be loaded.

 

addFieldToFilter : addFieldToFilter will filter the products based on columns in the database from the table catalog_product_entity. and used for flat model.it is defined in Mage_Core_Model_Resource_Db_Collection_Abstract class so it is available in all its child classes.

$collection = Mage::getModel('news/post')->getCollection();
$collection->addFieldToFilter('file_id',3);
$collection->addFieldToFilter('list_id',array('in'=>array(1,2,3)));

 

 

更多参考:

  1. Magento模型集合addFieldToFilter常用过滤条件
  2. difference between addAttributeToSelect(), addAttributeToFilter() and addFieldToFilter() methods
  3. Magento Collection Filtering functions

 

原文/转自: Magento: addAttributeToFilter 和 addFieldToFilter 的区别 Difference between addAttributeToFilter and addFieldToFilter

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics