addFieldToFilter 与 addAttributeToFilter使用总结
2014-10-31 15:27
429 查看
magento初学者在开发过程中,对addFieldToFilter和addAttributeToFilter的使用会比较迷惑,例如你在一个自定义模块中,在引用自己的table时,假如使用了addAttributeToFilter这个function,可能就会报错。 简单分析下原因: <p>我们在自定义模块里的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的继承关系):</p><p> </p><p><span style="font-size:18px;color:#FF0000;"><strong><span><span>addFieldToFilter</span></span></strong></span> </p> 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模型集合Model Collection中addFieldToFilter()方法常用的过滤条件。以下参数也同样适用于产品实体的addAttributeToFilter()方法。
http://fishpig.co.uk/blog/addattributetofilter-conditionals-in-magent
相关文章推荐
- addFieldToFilter 与 addAttributeToFilter使用总结
- Magento addFieldToFilter和addAttributeToFilter用法整理
- addAttributeToFilter Conditionals In Magento
- 使用Masonry蹦到布局崩到 NSAssert(NO, @"attempting to add unsupported attribute: %@", secondViewAttribute);
- 解决使用Struts2的s:textfield标签出现Unable to find setter method for attribute: style的问题
- Magento模型集合addFieldToFilter常用过滤条件
- addAttributeToFilter Conditionals In Magento
- magento 的一些关于addFieldToFilter的查询
- [Tips] How to Add New field into Shapefile attribute table in ArcGIS ?
- addAttributeToFilter condition in Magento
- Magento 条件筛选 addAttributeToFilter
- magento addattributetoselect使用
- addAttributeToFilter Conditionals In Magento
- sql server中分布式查询随笔(链接服务器(sp_addlinkedserver)和远程登录映射(sp_addlinkedsrvlogin)使用小总结)
- Linq To Sql常用方法使用总结
- AddFieldAsXml cannot be used to set the InternalName correctly
- 使用InternalsVisibleToAttribute控制internal成员的访问
- 使用InternalsVisibleToAttribute控制internal成员的访问
- sql server中分布式查询随笔(链接服务器(sp_addlinkedserver)和远程登录映射(sp_addlinkedsrvlogin)使用小总结)
- Linq To Sql常用方法使用总结【转】