magento 从类别中获取产品,通过 rand() 排序

发布于 2024-10-06 01:15:04 字数 346 浏览 2 评论 0原文

我有以下内容:

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSort('id', 'RAND()')
    ->addAttributeToSelect('small_image')
    ->addCategoryFilter(Mage::getModel('catalog/category')->load($catId));

但我需要按 id RAND() 排序,我该怎么做? (代码显示了我如何尝试但没有运气)

I have the following:

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSort('id', 'RAND()')
    ->addAttributeToSelect('small_image')
    ->addCategoryFilter(Mage::getModel('catalog/category')->load($catId));

But I need to order by id RAND(), how can I do this? (The code shows how I've tried with no luck)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

美人迟暮 2024-10-13 01:15:04

Magento 集合不接受除所选属性之一之外的参数。在这种情况下,您应该获取 Zend_Db_Select 对象并向其添加订单指令。

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSort()
    ->addAttributeToSelect('small_image')
    ->addCategoryFilter(Mage::getModel('catalog/category')->load());
$products->getSelect()->order(new Zend_Db_Expr('RAND()'));

要查看将执行什么查询,您可以使用此结构

$products->load(true, true); // first parameter show sql query in output, second show sql query in var/log/syslog

Magento collection do not accept parameters other then one of selected attribute. In this case you should get Zend_Db_Select object and add order instruction to it.

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToSort()
    ->addAttributeToSelect('small_image')
    ->addCategoryFilter(Mage::getModel('catalog/category')->load());
$products->getSelect()->order(new Zend_Db_Expr('RAND()'));

To see what query will be executed you may use this construnction

$products->load(true, true); // first parameter show sql query in output, second show sql query in var/log/syslog
も星光 2024-10-13 01:15:04

参考这个问题:query magento limit + order by rand() 和clockworkgeek的答案:

$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));

Refer to this question: query magento limit + order by rand() and clockworkgeek's answer:

$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
迟到的我 2024-10-13 01:15:04

使用 ORDER BY RAND() 以随机顺序返回项目列表将需要全表扫描和排序。它会对表中大量行的性能产生负面影响。

对于如何优化此查询,有几种可能的替代解决方案。 Magento 为此提供了本地解决方案。

Varien_Db_SelectorderRand() 方法和数据库适配器允许为 ORDER BY 指定随机订单和杠杆索引。
指定要在 ORDER BY 子句中使用的某些整数索引列的名称,例如:

$collection->getSelect()->orderRand('main_table.entity_id');

有关实现详细信息,请参阅 Varien_Db_Adapter_Pdo_Mysql::orderRand()

Using ORDER BY RAND() to return a list of items in a random order will require a full table scan and sort. It can negatively affect performance on large number of rows in the table.

There are several alternative solutions possible of how to optimize this query. Magento provides a native solution for that.

The orderRand() method of Varien_Db_Select and the database adapter allows to specify a random order and leverage index for ORDER BY.
Specify a name of some integer indexed column to be used in the ORDER BY clause, for example:

$collection->getSelect()->orderRand('main_table.entity_id');

See Varien_Db_Adapter_Pdo_Mysql::orderRand() for implementation details.

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