我为我们的Magento商店开发了一个自定义搜索引擎,我正在尝试按照特定的顺序加载产品集合(我根据我设计的算法对结果进行了排序).
我可以正确加载产品集合,但它不是我希望它的顺序.这里基本上是它如何工作:
我的数据库查询基本上带有PHP数组的产品ID.对于这个例子,让我们说它看起来像这样:
$entity_ids = array(140452, 38601 );
Run Code Online (Sandbox Code Playgroud)
现在我可以转换140452和38601,每次产品系列都以相同的顺序返回.我希望产品集合与实体ID的ID顺序相同.
我用来创建我的集合的代码如下:
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addAttributeToFilter('entity_id', array('in' => $entity_ids))
->setPageSize($results_per_page)
->setCurPage($current_page)
->load();
Run Code Online (Sandbox Code Playgroud)
有没有办法将排序顺序设置为$ entity_ids数组的顺序?
Ala*_*orm 13
集合继承自该类
Varien_Data_Collection_Db
Run Code Online (Sandbox Code Playgroud)
有一个addOrder
在该类上命名的方法.
public function addOrder($field, $direction = self::SORT_ORDER_DESC)
{
return $this->_setOrder($field, $direction);
}
Run Code Online (Sandbox Code Playgroud)
所以,你认为这样的东西应该适用于基本订购
Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect('*')
->addOrder('entity_id');
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.由于EAV集合中涉及的复杂连接,有一种特殊的方法用于向order子句添加属性
Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection::addAttributeToSort
Run Code Online (Sandbox Code Playgroud)
但是,这只能用于添加简单属性.要创建任意排序,您需要Zend_Select
直接操作对象.我不是这个的忠实粉丝,我不是使用自定义mysql函数来实现目标的忠实粉丝,但它似乎是唯一的方法来做到这一点
我在库存安装上测试了以下代码并获得了预期的结果.你应该能够用它来得到你想要的东西.
$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());
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
9878 次 |
最近记录: |