问题描述
(如 Mage_Eav_Model_Entity_Collection_Abstract :: addAttributeToSort
)加入一个 ORDER BY
工作Magento的收集整理功能条款的SQL SELECT语句。然而,有些时候,一个集合已加载并且需要排序的集合。
The Magento collection sorting functions (e.g. Mage_Eav_Model_Entity_Collection_Abstract::addAttributeToSort
) work by adding an ORDER BY
clause to the SQL select statement. However, there are times when a collection has already been loaded and it is necessary to sort the collection.
这当然是可以使用的toArray($域)
函数,然后PHP数组排序功能(无论是本地或用户定义的),但是这是一个有点笨拙。这也意味着,在集合中的对象被转换为哑值的未经魔getter / setter方法行可以/用算法来实现,等等。
It is certainly possible to use the toArray($fields)
function and then PHP array sorting functions (either native or user-defined), however this is a little clumsy. It also means that the objects in the collection are converted to "dumb" rows of values without magic getters/setters which can/are be implemented with algorithms, etc.
如果有更优雅的排序集合/ Magento的去年秋季的方式我不知道。
I'm wondering if there are more elegant/Magento-esque methods of sorting the collection.
谢谢,结果
乔纳森
Thanks,
Jonathan
推荐答案
另一种解决方案,它的工作原理:
Another solution which works:
class Aligent_Navigation_Block_Dropdown extends Mage_Catalog_Block_Product_List {
public function getProductsByShortDesc(){
$data = $this->getLoadedProductCollection()->getItems(); //an array of objects
usort($data,array('Aligent_Navigation_Block_Dropdown','sortByShortDesc'));
return $data;
}
public static function sortByShortDesc($a, $b)
{
if($a->getShortDescription() == $b->getShortDescription()){ return 0 ; }
return ($a->getShortDescription() < $b->getShortDescription()) ? -1 : 1;
}
}
这篇关于排序Magento的收集负荷后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!