E
E
EVOSandru62016-05-03 13:23:06
Yii
EVOSandru6, 2016-05-03 13:23:06

How to sort ActiveRecord with with in yii1 by category sort order?

Good afternoon,
There are 2 tables (products and category) and 2 models( Products and Category )
products
----------
id
name
category_id
=========
category
------- ---
id
name
order_sort (int)
=========

class Products extends ActiveRecord{
...
public function relations() {
    return [
            'category'              =>      [self::BELONGS_TO,  'Category', 'category_id'],
        ];
}

That is, the products must be grouped into categories in the sort order of the categories. Tried like this:
$model = Products::model()->with(
            'category',[
                'order' =>  'order_sort ASC'
            ] )->findAll();

But apparently wrong, tell me, if not difficult.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Bukharev, 2016-05-03
@EVOSandru6

Offhand, using CDbCriteria:

$criteria = new CDbCriteria;
$criteria->with=['category']
$criteria->order='category.order_sort ASC';

$model = Products::model()->findAll($criteria);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question