addFieldToFilter 与 addAttributeToFilter使用总结

magento初学者在开发过程中,对addFieldToFilter和addAttributeToFilter的使用会比较迷惑,例如你在一个自定义模块中,在引用自己的table时,假如使用了addAttributeToFilter这个function,可能就会报错。
简单分析下原因:
我们在自定义模块里的collection类,因为没有涉及到EAV模型,一般都是继承Mage_Core_Model_Mysql4_Collection_Abstract这个类,而在Mage_Core_Model_Mysql4_Collection_Abstract(继承自Varien_Data_Collection_Db包含很多常用function)和它的父类里,是没有addAttributeToFilter这个function的,如果一定要使用addAttributeToFilter可以在collection加上如下function(或者改变collection的继承关系):
    public function addAttributeToFilter($attribute, $condition = null)
    {
        $this->addFieldToFilter($this->_attributeToField($attribute), $condition);
        return $this;
    }

    /**
     * Check if $attribute is Mage_Eav_Model_Entity_Attribute and convert to string field name
     *
     * @param string|Mage_Eav_Model_Entity_Attribute $attribute
     * @return string
     */
    protected function _attributeToField($attribute)
    {
        $field = false;
        if (is_string($attribute)) {
            $field = $attribute;
        } elseif ($attribute instanceof Mage_Eav_Model_Entity_Attribute) {
            $field = $attribute->getAttributeCode();
        }
        if (!$field) {
            Mage::throwException(Mage::helper('yourmode')->__('Cannot determine the field name.'));
        }
        return $field;
    }
magento自带的模块涉及到EAV模型的collection最终都是继承自Mage_Eav_Model_Entity_Collection_Abstract,如catalog,customer模块等,这个类定义了很多有用的function,包括addAttributeToFilter等,读者可自行去深入了解。
总结:addFieldToFilter基本上是addAttributeToFilter的别名,很多时候能通用;对用自定义的模块,一般使用addFieldToFilter就能满足需求。对于EAV模型,大多都是系统自带模块,addAttributeToFilter就显得很有用。


2012-5-26日更新:加一些例子如下,下次用到时不用再费劲找了:

Equals: eq
1
$_products ->addAttributeToFilter( 'status' , array ( 'eq' => 1));
Not Equals - neq
1
$_products ->addAttributeToFilter( 'sku' , array ( 'neq' => 'test-product' ));
Like - like
1
$_products ->addAttributeToFilter( 'sku' , array ( 'like' => 'UX%' ));

One thing to note about like is that you can include SQL wildcard characters such as the percent sign.

Not Like - nlike
1
$_products ->addAttributeToFilter( 'sku' , array ( 'nlike' => 'err-prod%' ));
In - in
1
$_products ->addAttributeToFilter( 'id' , array ( 'in' => array (1,4,74,98)));

When using in, the value parameter accepts an array of values.

Not In - nin
1
$_products ->addAttributeToFilter( 'id' , array ( 'nin' => array (1,4,74,98)));
NULL - null
1
$_products ->addAttributeToFilter( 'description' , 'null' );
Not NULL - notnull
1
$_products ->addAttributeToFilter( 'description' , 'notnull' );
Greater Than - gt
1
$_products ->addAttributeToFilter( 'id' , array ( 'gt' => 5));
Less Than - lt
1
$_products ->addAttributeToFilter( 'id' , array ( 'lt' => 5));
Greater Than or Equals To- gteq
1
$_products ->addAttributeToFilter( 'id' , array ( 'gteq' => 5));
Less Than or Equals To - lteq
1
$_products ->addAttributeToFilter( 'id' , array ( 'lteq' => 5));

原创文章,转载请注明出处!

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值