簡體   English   中英

用於對magento集合進行排序的多個字段

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM