2015-07-13 49 views
2

這裏是目標SQL查詢: ...... ORDER BY FIELD1 ASC,price_index.min_price遞減多個字段進行排序Magento的收集

這裏是我的代碼

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

然而,在我的結果價格始終是第一個訂購領域。任何人都可以幫助我,請嗎?謝謝你這麼多

回答

2

排序使用多個字段,你可以鏈調用集合的方法addAttributeToSort()

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

感謝你的答案。 Iam試圖獲取收集功能,但這是不正確的。因爲價格將首先歸檔結果。你對我有沒有其他建議?謝謝 –

0

使用:

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

然後:

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

該代碼將解決此問題^^

0

多個字段進行排序,你可以使用

$collection = Mage::getModel(‘module/model_name’)->getCollection() 
->addAttributeToSort(‘order’, ‘ASC’) 
->addAttributeToSort(‘last_name’, ‘ASC’) 
->addAttributeToSort(‘first_name’, ‘ASC’); 
+0

感謝您的回答。您的代碼與正常字段正確。但價格我必須重新從收集訂單,並再次訂購: 'productCollection-> getSelect() - > reset(Zend_Db_Select :: ORDER) - > order('filed1'') - > order('field2' ); ' –

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

或排序多個:自定義資源集合

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

,使用addOrder

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