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

Magento条件筛选 addAttributeToFilter Conditionals In Magento

 
阅读更多

addAttributeToFilter is a function that can be called on a product collection in Magento. In short, it adds a condition to the WHERE part of the MySQL query used to extract a product collection from the database.

$_products = Mage::getModel('catalog/product')->getCollection()
   ->addAttributeToSelect(array('name', 'product_url', 'small_image'))
   ->addAttributeToFilter('sku', array('like' => 'UX%'))
    ->load();
 

The above code would get a product collection, with each product having it's name, url, price and small image loaded in it's data array. The product collection would be filtered and contain only products that have an SKU starting with UX.

 

addAttributeToFilter Conditionals

 

Notice above, I used the LIKE operator? There are many more operators in SQL and addAttributeToFilter will accept them all. I include them below as well as a reference for you. Hopefully this will save you some time.

 

Equals: eq

$_products->addAttributeToFilter('status', array('eq' => 1));
 

Not Equals - neq

$_products->addAttributeToFilter('sku', array('neq' => 'test-product'));
 

Like - like

$_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

$_products->addAttributeToFilter('sku', array('nlike' => 'err-prod%'));
 

In - in

$_products->addAttributeToFilter('id', array('in' => array(1,4,74,98)));
 

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

 

Not In - nin

$_products->addAttributeToFilter('id', array('nin' => array(1,4,74,98)));
 

NULL - null

$_products->addAttributeToFilter('description', 'null');
 

Not NULL - notnull

$_products->addAttributeToFilter('description', 'notnull');
 

Greater Than - gt

$_products->addAttributeToFilter('id', array('gt' => 5));
 

Less Than - lt

$_products->addAttributeToFilter('id', array('lt' => 5));
 

Greater Than or Equals To- gteq

$_products->addAttributeToFilter('id', array('gteq' => 5));
 

Less Than or Equals To - lteq

$_products->addAttributeToFilter('id', array('lteq' => 5));
 

addFieldToFilter()

 

As far as I'm aware, addAttributeToFilter only works with products in Magento. When I first found out this fact I was not only shocked, I was worried! I thought that without it, I would have to custom craft all of my SQL queries. After scouring the Magento core code one night, I found addFieldToFilter(). This functions works in the exact same way and takes the same paramters, however it works on ALL collections and not just on products!

 

Debugging The SQL Query

 

There are two ways to debug the query being executed when loading a collection in Magento.

// Method 1
Mage::getModel('catalog/product')->getCollection()->load(true);
 
// Method 2 (Quicker, Recommended)
$collection = Mage::getModel('catalog/product')->getCollection();
echo $collection->getSelect();
 

Both method 1 and method 2 will print out the query but both will do it in slightly different ways. Method 1 prints the query out as well as loading the products while method 2 will just convert the query object to a string (ie. will print out the SQL). The second method is definitely better as it will be executed much quicker but I include them both here for reference.

 

On a side note, I will soon be writing an article on the getSelect() function as it opens up a door in Magento Collections that gives them (and you) true power!

 

 

来源:http://fishpig.co.uk/blog/addattributetofilter-conditionals-in-magento.html

 

 

 

 

 

分享到:
评论

相关推荐

    magento创建自定义页面 (Bestseller实例) Bestseller products in Magento

    这通常涉及加载`Mage::getModel('catalog/product')`,然后应用过滤条件,如`->addAttributeToFilter('sales_rank', array('order' => 'desc'))`来获取销量最高的产品。 6. **传递数据到视图**:将获取到的...

    Magento: 后台添加预览按钮 View product in frontend from Magento admin

    这篇博客“Magento:后台添加预览按钮 View product in frontend from Magento admin”主要探讨了如何通过自定义开发来实现这一功能,让管理员能够快速检查商品在网站前台的显示状态。 在Magento中,通常管理员需要...

    magento二次开发大全

    Magento是一款强大的开源电子商务平台,以其高度可定制性和灵活性著称。在进行Magento的二次开发时,你需要理解并掌握以下几个核心概念和技术: 1. **MVC架构**:Magento基于Model-View-Controller(MVC)设计模式...

    magento-java-master.zip_magento

    这个“magento-java-master.zip_magento”压缩包可能是为了提供一个Java连接Magento源码的示例或者库,帮助开发者实现Java与Magento系统的交互。 在Java中与Magento进行交互通常涉及到以下几个关键知识点: 1. **...

    Magento深入理解Magento

    ### Magento深入理解——强大配置系统解析 #### 一、引言 Magento是一款极其灵活且功能丰富的电子商务平台,其核心竞争力之一在于其强大的配置系统。这一系统不仅为开发者提供了极高的定制化能力,还确保了平台的...

    Magento 2 Beginners Guide

    Magento 2 Beginners Guide by Gabriel Guarino English | 14 Mar. 2017 | ASIN: B01MS81BQX | 442 Pages | AZW3 | 31....He is also a moderator in Magento forums, and he is a frequent speaker at Magento events.

    magento官方文档翻译超好

    - **数据筛选**:支持灵活的条件筛选功能,可以按需对集合中的数据进行过滤。 - **数据排序**:可以根据指定的字段对集合中的数据进行排序。 ##### 3.2 Varien_Data_Collection的具体应用 为了更好地理解`Varien_...

    magento数据结构分析

    标题:“Magento数据结构分析” 描述:“Magento数据字典”提供了对Magento系统中各种数据库表的深入理解,这对于理解和优化Magento的性能至关重要。 一、Magento数据结构解析 Magento是一款功能强大的电子商务...

    Magento-SMTP-Email

    Magento是开源的电子商务平台,广泛用于在线商店的建设。SMTP(Simple Mail Transfer Protocol)是用于发送电子邮件的标准协议。在Magento中,SMTP插件扮演着关键角色,它允许商家通过更安全、可靠的SMTP服务器发送...

    magento入门学习资料

    Magento是一款强大的开源电子商务平台,以其高度可定制性和灵活性著称。作为一款基于PHP开发的系统,它为商家提供了丰富的功能,包括商品管理、订单处理、客户管理、营销工具等。以下将详细介绍`magento入门学习资料...

    magento图片延时加载插件

    Magento是一款强大的开源电子商务平台,它的灵活性和可扩展性使得开发者能够根据需求定制各种功能。在电商网站中,图片是至关重要的元素,它们可以展示产品细节,吸引顾客注意力。然而,大量的图片也会对网站性能...

    magento商城数据库

    Magento 商城数据库是一个关键组成部分,它是 Magento 电子商务平台的核心,负责存储所有商品信息、客户数据、订单记录以及网站配置等重要信息。Magento 是一个开源的电子商务解决方案,以其强大的功能和高度可定制...

    Magento插件开发手册 Magento Extension Developers Guide

    Magento是一款强大的开源电子商务平台,为开发者提供了广泛的定制和扩展能力。《Magento插件开发手册》是一份详尽的指南,旨在帮助开发者理解Magento的核心架构、编码标准以及如何创建和部署自定义插件。 ### ...

    magento2 developers cookbook

    根据给定文件信息,以下为《Magento 2 Developer's Cookbook》一书中的知识点介绍。 首先,《Magento 2 Developer's Cookbook》是一本针对Magento 2开发的指导手册,它向开发者提供了实用的食谱来解决在Magento 2...

    magik shoes magento 模板, magento 1.7 模板

    3. **过滤和排序**:用户可以按照品牌、价格、颜色、尺码等条件过滤和排序商品,快速找到他们感兴趣的产品。 4. **快速查看**:用户可以无需进入产品详情页面就能预览鞋子的基本信息,如图片、价格和简短描述。 5....

    Magento 常用方法和插件

    Magento是一款强大的开源电子商务平台,以其高度可定制性和灵活性著称。在Magento的开发过程中,掌握常用的方法和插件是提高工作效率的关键。这篇博客"Magento 常用方法和插件"可能涵盖了以下方面: 首先,关于...

    Magento se105 精美兰亭更新模板 9种颜色

    Magento是一款强大的开源电子商务平台,专为在线商家设计,提供丰富的功能和高度的可定制性。在本资源中,我们关注的是“Magento se105 精美兰亭更新模板”,这是一款专为Magento设计的视觉效果优秀的主题模板,特别...

    The Definitive Guide to Magento (Apress出品 Magento权威指南)

    ### Magento权威指南 #### 书籍概述 《Magento权威指南》是由Adam McCombs与Robert Banh共同编著的一本深入探讨Magento电商平台的技术手册。该书由Apress出版社于2009年出版发行,旨在为读者提供一个全面、系统的...

    magento中文手册.pdf

    Magento是基于PHP开发的电子商务解决方案,由Magento Inc.公司创建。它以其灵活的架构、丰富的功能集和开放源代码特性而闻名,为各种规模的企业提供了一个全面的网上商店建设工具。Magento分为两个主要版本:Magento...

    开源网店系统 Magento

    Magento是一款强大的开源电子商务平台,由Varien公司开发,并在2008年首次发布。它以其高度可定制性、丰富的功能集以及灵活的架构而受到全球电商从业者的广泛青睐。这款网店系统的出现,为中小型企业提供了与大型...

Global site tag (gtag.js) - Google Analytics