I
I
ImPuuLsE2015-02-16 22:01:35
Yii
ImPuuLsE, 2015-02-16 22:01:35

How to do random sorting of products in Yii?

Hello! Products are currently listed alphabetically by title. Those products that are marked with "Priority" (number > 0) are shown first, then the rest.
How to make the display in random order, taking into account the priority (i.e. products with a numeric "Priority" first, as well as now, and products with zero priority next in random order

public function sortedByPriorityInCatalog()
    {
        $criteria = $this->getDbCriteria(); 
        $criteria->join = 'left join '.SeriesPriority::model()->tableName().' sp
                           on t.series_id = sp.series_id';
        $criteria->order = 'IF(t.priority=0,65000,t.priority),
                            IF(sp.priority >0,sp.priority,65000),
                            t.name';
        $criteria->mergeWith(array(
            'with'=>array('images','currency','manufacturer','series'),
        ));
        return $this;
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bay, 2015-02-16
@kawabanga

Maybe
$criteria->order = 'IF(t.priority=0,65000,t.priority),
IF(sp.priority >0,sp.priority,65000),
rand()';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question