[英]Multiple fields to sort a magento collection
这是目标sql查询:......按field1 asc,price_index.min_price desc排序
这是我的代码
$productCollection->getCollection()
->setOrder('field1', 'asc')
->setOrder('price', 'desc')
但是在我的结果价格总是第一个订购字段。 有人可以帮我吗,拜托? 非常感谢
$collection->getSelect()
->order('field1 asc');
或按多个排序:
$collection->getSelect()
->order(array('field1 asc', 'price desc'));
要使用多个字段进行排序,可以将调用链接到Collection的方法addAttributeToSort()
$productCollection->getCollection()
->addAttributeToSort('field1', 'asc')
->addAttributeToSort('price', 'desc');
在自定义资源集合上,使用addOrder
:
Mage::getModel('module/model')->getCollection()
->addOrder('first', 'ASC')
->addOrder('second', 'DESC')
->addOrder('other', 'DESC');
使用:
productCollection->getSelect()->reset(Zend_Db_Select::ORDER);
然后:
productCollection->getSelect()
->order(.......)
该代码将解决此问题^ ^
要对可以使用的多个字段进行排序
$collection = Mage::getModel(‘module/model_name’)->getCollection()
->addAttributeToSort(‘order’, ‘ASC’)
->addAttributeToSort(‘last_name’, ‘ASC’)
->addAttributeToSort(‘first_name’, ‘ASC’);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.