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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值