Magento以任意顺序获得产品集合 [英] Magento get a product collection in an arbitrary order
问题描述
我为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以任意顺序获得产品集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!