問題描述
我為我們的 Magento 商店開發(fā)了一個自定義搜索引擎,我正在嘗試以非常特定的順序加載產(chǎn)品系列(我已經(jīng)根據(jù)我設計的算法對結(jié)果進行了排名).
I have developed a custom search engine for our Magento store and I am trying to load the product collection in a very specific order (I have ranked the results according to an algorithm I designed).
我可以正確加載產(chǎn)品集合,但它不是我想要的順序.現(xiàn)在它的工作原理如下:
I can load the product collection correctly, however it is not in the order that I would like it to be in. Here is basically how it is working now:
我的數(shù)據(jù)庫查詢基本上返回一個包含產(chǎn)品 ID 的 PHP 數(shù)組.對于這個例子,假設它看起來像這樣:
My database query basically comes back with a PHP array of product IDs. For this example lets say it looks like this:
$entity_ids = array(140452, 38601 );
現(xiàn)在我可以調(diào)換 140452 和 38601,并且產(chǎn)品集合每次都以相同的順序返回.我希望產(chǎn)品集合與實體 ID 的 ID 順序相同.
Now I can transpose the 140452 and the 38601 and the product collection comes back in the same order each time. I would like the product collection to be in the same order as the ID of the entity ids.
我用來創(chuàng)建我的收藏的代碼如下:
The code I am using to create my collection is as follows:
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $entity_ids))
->setPageSize($results_per_page)
->setCurPage($current_page)
->load();
有沒有辦法將排序順序設置為 $entity_ids 數(shù)組的順序?
Is there a way to set the sort order to be the order of the $entity_ids array?
推薦答案
集合繼承自類
Varien_Data_Collection_Db
該類中有一個名為 addOrder
的方法.
There's a method named addOrder
on that class.
public function addOrder($field, $direction = self::SORT_ORDER_DESC)
{
return $this->_setOrder($field, $direction);
}
所以,你會認為這樣的東西應該適用于基本的訂購
So, you'd think something like this should work for basic ordering
Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addOrder('entity_id');
然而,事實并非如此.由于 EAV 集合中涉及的復雜連接,有一種特殊的方法用于向 order 子句添加屬性
However, it doesn't. Because of the complex joining involved in EAV Collections, there's a special method used to add an attribute to the order clause
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection::addAttributeToSort
然而,這只能用于添加簡單的屬性.要創(chuàng)建任意排序,您需要直接操作 Zend_Select
對象.我不喜歡這個,也不喜歡使用自定義的 mysql 函數(shù)來實現(xiàn),但它似乎是唯一的方法
However again, this can only be used to add simple attributes. To create an arbitrary sort, you'll need to manipulate the Zend_Select
object directly. I'm not a big fan of this, and I'm not a big fan of using custom mysql functions to achieve things, but it appears it's the only way to do this
我在股票安裝上測試了以下代碼并獲得了預期的結(jié)果.您應該能夠使用它來獲得您想要的東西.
I tested the following code on a stock install and got the desired results. You should be able to use it to get what you want.
$ids = array(16,18,17,19);
$products = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id',$ids);
//shakes fist at PDO's array parameter
$ids = array_map('intval', $ids);
$products->getSelect()->order("find_in_set(e.entity_id,'".implode(',',$ids)."')");
foreach($products as $product)
{
var_dump($product->getEntityId());
var_dump($product->getSku());
}
這篇關于Magento 以任意順序獲取產(chǎn)品集合的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!