addAttributeToFilter Conditionals In Magento
2014-12-07 20:50
295 查看
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.
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
Not Equals - neq
Like - like
One thing to note about like is that you can include SQL wildcard characters such as the percent sign.
Not Like - nlike
In - in
When using in, the value parameter accepts an array of values.
Not In - nin
NULL - null
Not NULL - notnull
Greater Than - gt
Less Than - lt
Greater Than or Equals To- gteq
Less Than or Equals To - lteq
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.
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!
补充:如果是需要or的操作,写法如下
$_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)); $_products->addAttributeToFilter('status', 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', array('null'=> true));
Not NULL - notnull
$_products->addAttributeToFilter('description', array('notnull'=> true));
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!
补充:如果是需要or的操作,写法如下
$collection->addAttributeToFilter( array( array('attribute'=> 'someattribute','like' => 'value'), array('attribute'=> 'otherattribute','like' => 'value'), array('attribute'=> 'anotherattribute','like' => 'value'), ) ); // 于上写法不同 $collection->->addFieldToFilter( array('title', 'content'), array( array('like'=>'%$titlesearchtext%'), array('like'=>'%$contentsearchtext%') ))
相关文章推荐
- addAttributeToFilter Conditionals In Magento
- addAttributeToFilter Conditionals In Magento
- addAttributeToFilter condition in Magento
- Magento addFieldToFilter和addAttributeToFilter用法整理
- Magento 条件筛选 addAttributeToFilter
- addFieldToFilter 与 addAttributeToFilter使用总结
- How to know whether the attribute belongs to one types of productin Magento?
- The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the ser
- Magento模型集合addFieldToFilter常用过滤条件
- magento 的一些关于addFieldToFilter的查询
- Magento add product attribute and assign to all group
- How to add custom fields to Customer Registration and Account page in magento
- How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/
- How do I add multiple arguments to my custom template filter in a django template? - Stack Overflow
- magento如何获取产品属性值 How to Obtain Custom Attribute Values in Magento
- How to Programmatically Add/Delete Custom Options in Magento? - See more at: http://apptha.com/blog/
- magento addattributetoselect使用
- How to change a product dropdown attribute to a multiselect in Magento
- magento如何获取产品属性值 How to Obtain Custom Attribute Values in Magento
- Magento1.9 add attribute to catalog product & assign to all attribute set general group