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 以任意顺序获取产品集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)