久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Magento 以任意順序獲取產品集合

Magento get a product collection in an arbitrary order(Magento 以任意順序獲取產品集合)
本文介紹了Magento 以任意順序獲取產品集合的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我為我們的 Magento 商店開發了一個自定義搜索引擎,我正在嘗試以非常特定的順序加載產品系列(我已經根據我設計的算法對結果進行了排名).

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

我可以正確加載產品集合,但它不是我想要的順序.現在它的工作原理如下:

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:

我的數據庫查詢基本上返回一個包含產品 ID 的 PHP 數組.對于這個例子,假設它看起來像這樣:

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 );

現在我可以調換 140452 和 38601,并且產品集合每次都以相同的順序返回.我希望產品集合與實體 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.

我用來創建我的收藏的代碼如下:

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 數組的順序?

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

然而,這只能用于添加簡單的屬性.要創建任意排序,您需要直接操作 Zend_Select 對象.我不喜歡這個,也不喜歡使用自定義的 mysql 函數來實現,但它似乎是唯一的方法

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

我在股票安裝上測試了以下代碼并獲得了預期的結果.您應該能夠使用它來獲得您想要的東西.

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 以任意順序獲取產品集合的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Override Magento Config(覆蓋 Magento 配置)
What would cause a print_r and/or a var_dump to fail debugging a variable?(什么會導致 print_r 和/或 var_dump 調試變量失敗?)
How to update custom options programatically in magento?(如何在 magento 中以編程方式更新自定義選項?)
Magento 404 on Admin Page(管理頁面上的 Magento 404)
Magento - get price rules from order(Magento - 從訂單中獲取價格規則)
Magento Change Product Page Titles to Include Attributes(Magento 更改產品頁面標題以包含屬性)
主站蜘蛛池模板: 亚洲激情欧美 | 九九精品免费视频 | 青青草免费在线观看 | 欧洲色综合 | 久久久久久国产精品 | 精品国产一区二区三区久久久蜜月 | 久久精品视频网 | 三上悠亚激情av一区二区三区 | 狠狠的操 | 999精品视频 | 亚洲精品乱码久久久久久 | 日韩三级在线播放 | 国产高清免费视频 | 色爽视频 | 欧美日韩国产二区 | 欧美色图一区 | 高潮一区二区三区乱码 | 日韩成人在线播放 | 欧美久久一区二区 | 黄色一级片黄色一级片 | 日本在线| 欧美手机在线 | 成人黄色免费网站 | 国产精品欧美一区二区 | 欧美久久久| 91网在线观看 | 色综合天天综合网天天狠天天 | 免费视频成人 | 久久精品一区二区三区四区五区 | 亚洲福利网 | 在线观看欧美日韩 | 久久av一区二区 | 国产欧美综合一区二区三区 | 国内自拍xxxx18 | 黄频在线观看 | 久久精彩 | 成人免费激情视频 | 成人羞羞国产免费游戏 | 黄色三级av | 中文字幕亚洲一区 | 国产午夜精品视频 |