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

Magento 以任意順序獲取產(chǎn)品集合

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

問題描述

我為我們的 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)!

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

相關文檔推薦

Override Magento Config(覆蓋 Magento 配置)
What would cause a print_r and/or a var_dump to fail debugging a variable?(什么會導致 print_r 和/或 var_dump 調(diào)試變量失敗?)
How to update custom options programatically in magento?(如何在 magento 中以編程方式更新自定義選項?)
Magento 404 on Admin Page(管理頁面上的 Magento 404)
Magento - get price rules from order(Magento - 從訂單中獲取價格規(guī)則)
Magento Change Product Page Titles to Include Attributes(Magento 更改產(chǎn)品頁面標題以包含屬性)
主站蜘蛛池模板: 中文av网站 | 天天操网 | 久热电影 | 欧美日韩一区二区三区四区 | 精品国产1区2区3区 在线国产视频 | 国产成人午夜精品影院游乐网 | 国产目拍亚洲精品99久久精品 | 免费亚洲视频 | 亚洲第一天堂 | 久久精品国产免费看久久精品 | 中文字幕亚洲一区 | 欧美激情视频一区二区三区在线播放 | 老外几下就让我高潮了 | 欧美成人一区二区三区片免费 | 国产激情一区二区三区 | 天天色综| 久久久久久国产免费视网址 | 亚洲性视频 | 91视频在线 | 欧美视频在线观看 | 亚洲激情第一页 | 久久精品国产一区 | 久久一久久 | 国产成人99久久亚洲综合精品 | 精品美女久久久久久免费 | 国产精品黄视频 | 久久91精品| 在线色网 | 国产真实精品久久二三区 | 一级毛片免费看 | 最新国产精品视频 | 色网在线观看 | 久久久精品一区 | 黄瓜av | 国产精品久久久久久久久免费高清 | 免费一看一级毛片 | 午夜久草| 一区二区三区免费观看 | 国产91中文 | 日本不卡一区二区 | 亚洲va欧美va人人爽午夜 |