简体   繁体   English

用于对magento集合进行排序的多个字段

[英]Multiple fields to sort a magento collection

Here is target sql query: ...... order by field1 asc, price_index.min_price desc 这是目标sql查询:......按field1 asc,price_index.min_price desc排序

And here is my code 这是我的代码

$productCollection->getCollection()
         ->setOrder('field1', 'asc')
         ->setOrder('price', 'desc')

However in my result price always is first ordering field. 但是在我的结果价格总是第一个订购字段。 can anyone help me, Please ? 有人可以帮我吗,拜托? Thank you so much 非常感谢

$collection->getSelect()
    ->order('field1 asc');

or sort by multiple: 或按多个排序:

 $collection->getSelect()
    ->order(array('field1 asc', 'price desc'));

To sort using multiple Fields, you can chain calls to the Collection's method addAttributeToSort() 要使用多个字段进行排序,可以将调用链接到Collection的方法addAttributeToSort()

$productCollection->getCollection()
         ->addAttributeToSort('field1', 'asc')
         ->addAttributeToSort('price', 'desc');

on custom resource collection, use addOrder : 在自定义资源集合上,使用addOrder

Mage::getModel('module/model')->getCollection()
    ->addOrder('first', 'ASC')
    ->addOrder('second', 'DESC')
    ->addOrder('other', 'DESC');

Using: 使用:

productCollection->getSelect()->reset(Zend_Db_Select::ORDER); 

Then: 然后:

productCollection->getSelect()
            ->order(.......) 

That code will resolve this problem ^ ^ 该代码将解决此问题^ ^

To sort multiple fields you can use 要对可以使用的多个字段进行排序

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM