加载后对 Magento 集合进行排序
Magento 集合排序函数(例如Mage_Eav_Model_Entity_Collection_Abstract::addAttributeToSort
)通过向SQL select 语句添加ORDER BY
子句来工作。然而,有时集合已经被加载,需要对集合进行排序。
当然可以使用 toArray($fields) 函数,然后使用 PHP 数组排序函数(本机的或用户定义的),但这有点笨拙。这也意味着集合中的对象被转换为“哑”值行,而无需神奇的 getter/setter,这些 getter/setter 可以通过算法等实现。
我想知道是否有更优雅/Magento 式的排序方法集合。
谢谢,
乔纳森
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.
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.
I'm wondering if there are more elegant/Magento-esque methods of sorting the collection.
Thanks,
Jonathan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
没有正确的方法来做到这一点。但我认为使用反射是可能的。您可以检索集合对象的 $_items 属性,对它们进行排序并将其设置回集合。
There is no proper way of doing it. But I think it is possible with using of Reflection. You can retrieve $_items property of collection object, sort them and set it back to the collection.
这里有一个提示;集合的
clear
方法会取消设置其已加载标志,它允许您更改排序或过滤器并运行新查询。我在回答仅加载可配置产品时无意中发现了它。
Here's a tip; A collection's
clear
method unsets it's loaded flag, it allows you to change the sort or filters and run the new query.I accidentally discovered it when answering load only configurable products.
@Ivan Chepurnyi 的方法有效,但返回一个 ReflectionObject 对象,在我的例子中,我需要一个 Varien_Data_Collection。
这是我所做的
如果这是排序方法
The method of @Ivan Chepurnyi worked but returns a ReflectionObject object, in my case I needed a Varien_Data_Collection.
Here is what I did instead
And in case here is the sorting method
另一个有效的解决方案:
Another solution which works:
上面的解决方案可以很好地工作,但它比将排序添加到查询本身要慢得多且更密集。
如果您有一个大型集合,您将使用大量内存,因为您需要在每个结果之前加载一个或多个对象并将它们全部存储。
使用集合 Magento 一次只会从数据库加载一行,这比上面的解决方案要高效得多:-)
The above solution will work fine, but it's a LOT slower and more intensive than adding the sort to the query itself.
If you have a large collection you will use a huge amount of memory as you need to load an object (or objects) fore each result and store them all.
Using the collection Magento will only load one row at a time from the database which will be a lot more efficient than the above solution :-)