如何在 Magento 中对集合进行排序?
我正在尝试按 attribute_id
对集合进行排序。我认为这很容易,但我认为我没有正确使用它:
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setOrder('attribute_id');
echo $attributes->getSelect();
结果:
SELECT `main_table`.* FROM `eav_attribute` AS `main_table`
为什么没有 order by
?
I'm trying to sort a collection by attribute_id
. I thought it would be easy, but I think I'm not using it correctly:
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
->setOrder('attribute_id');
echo $attributes->getSelect();
Result:
SELECT `main_table`.* FROM `eav_attribute` AS `main_table`
Why isn't there any order by
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你实际上正在以正确的方式做这件事。然而,由于 Magento 使用 EAV,因此需要应用技巧来提高性能。
这些技巧之一是用于构建最终 SQL 字符串的时间。通常它是在最后一刻延迟加载的,直到您实际表明想要访问集合的数据时,您才能看到用于生成集合的完整 SQL。例如,运行代码,但提示 magento 实际构建和加载集合,会产生预期的输出。
这会导致 SQL:
所以你走在正确的道路上,只是 Magento 竭尽全力来迷惑你。它非常擅长这一点。
You're actually doing it the right way. However, since Magento uses EAV, it needs to apply tricks to help performance.
One of these tricks is the timing used to build the eventual SQL string. Usually it's lazily loaded at the last minute and it's not until you actually indicate you want to access a collection's data, that you can see the full SQL used to produce the collection. For example running your code, but prompting magento to actually construct and load the collection, produces the expected output.
This results in the SQL:
So you were on the right path, just Magento was doing its level best to confuse you. It's very good at that.
使用它代替
$attributes->getSelect();
:不要问为什么。
Use this instead of
$attributes->getSelect();
:Don't ask why.