本文介紹了magento 從類別中獲取產品,按 rand() 排序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有以下幾點:
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSort('id', 'RAND()')
->addAttributeToSelect('small_image')
->addCategoryFilter(Mage::getModel('catalog/category')->load($catId));
但我需要按 id RAND()
訂購,我該怎么做?(代碼顯示了我如何嘗試但沒有運氣)
But I need to order by id RAND()
, how can I do this? (The code shows how I've tried with no luck)
推薦答案
Magento 集合不接受選定屬性之一以外的參數.在這種情況下,您應該獲得 Zend_Db_Select
對象并向其添加訂單指令.
Magento collection do not accept parameters other then one of selected attribute. In this case you should get Zend_Db_Select
object and add order instruction to it.
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSort()
->addAttributeToSelect('small_image')
->addCategoryFilter(Mage::getModel('catalog/category')->load());
$products->getSelect()->order(new Zend_Db_Expr('RAND()'));
要查看將執行什么查詢,您可以使用此構造
To see what query will be executed you may use this construnction
$products->load(true, true); // first parameter show sql query in output, second show sql query in var/log/syslog
這篇關于magento 從類別中獲取產品,按 rand() 排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!