跨境电商
经验交流分享

Magento模型集合addFieldToFilter常用过滤条件

记录一下Magento模型集合Model Collection中addFieldToFilter()方法常用的过滤条件。以下参数也同样适用于产品实体的addAttributeToFilter()方法。

[php]
//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%’));

//Not Like – nlike
$_products->addAttributeToFilter(‘sku’, array(‘nlike’ => ‘err-prod%’));

//In – in
$_products->addAttributeToFilter(‘id’, array(‘in’ => array(1,4,98)));

//Not In – nin
$_products->addAttributeToFilter(‘id’, array(‘nin’ => array(1,4,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));

[/php]

SQL的DEBUG方法

[php]
// 第一种,该方法货执行load的过程
Mage::getModel(‘catalog/product’)->getCollection()->load(true);

// 第二种,该方法不会执行load过程,只把sql语句转换为string对象。
$collection = Mage::getModel(‘catalog/product’)->getCollection();
echo $collection->getSelect();
[/php]

懒省事了,转载地址:
http://fishpig.co.uk/blog/addattributetofilter-conditionals-in-magento.html

赞(0)
未经允许不得转载:锐想 » Magento模型集合addFieldToFilter常用过滤条件
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

锐想电商 - 跨境电商经验交流分享

锐想无限