如何在 Magento 中对集合进行排序?

发布于 2024-10-14 18:48:49 字数 371 浏览 0 评论 0原文

我正在尝试按 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那伤。 2024-10-21 18:48:49

你实际上正在以正确的方式做这件事。然而,由于 Magento 使用 EAV,因此需要应用技巧来提高性能。

这些技巧之一是用于构建最终 SQL 字符串的时间。通常它是在最后一刻延迟加载的,直到您实际表明想要访问集合的数据时,您才能看到用于生成集合的完整 SQL。例如,运行代码,但提示 magento 实际构建和加载集合,会产生预期的输出。

$attributes =  Mage::getResourceModel('eav/entity_attribute_collection')
    ->setOrder('attribute_id');
$attributes->count(); // forces the collection to load
echo $attributes->getSelect()->assemble();

这会导致 SQL:

SELECT `main_table`.* FROM `eav_attribute` AS `main_table` ORDER BY attribute_id DESC

所以你走在正确的道路上,只是 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.

$attributes =  Mage::getResourceModel('eav/entity_attribute_collection')
    ->setOrder('attribute_id');
$attributes->count(); // forces the collection to load
echo $attributes->getSelect()->assemble();

This results in the SQL:

SELECT `main_table`.* FROM `eav_attribute` AS `main_table` ORDER BY attribute_id DESC

So you were on the right path, just Magento was doing its level best to confuse you. It's very good at that.

︶葆Ⅱㄣ 2024-10-21 18:48:49

使用它代替 $attributes->getSelect();

$attributes->getSelect()->order('main_table.attribute_id ASC');

不要问为什么。

Use this instead of $attributes->getSelect();:

$attributes->getSelect()->order('main_table.attribute_id ASC');

Don't ask why.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文