Thursday, April 26, 2012

Magento : Set Random Order in Collection


we can select random rows from MySQL table using Zend_Db_Expr(‘RAND()’),
here we have to create a new function in module collection class
(Namespace/Module/Model/Mysql4/Module/Collection.php)
public function setRandomOrder()
{
    $this->getSelect()->order(new Zend_Db_Expr('RAND()'));
    return $this;
}
And use it as
$collection = Mage::getModel('test/test')
                    ->getCollection()
                    ->setRandomOrder();

1 comment:

  1. Congratulations for this nice looking blog. In this post everything about Web Development. I am also interested in latest news
    Thanks
    Magento Modules

    ReplyDelete