Consult usage viatable sorting method

Hello everyone, my English is not good, but in order to follow more people can understand the content, so i used Google translation, please forgive me.




public function getItems()

{

    return $this->hasMany(Item::className(), ['id' => 'item_id'])

                ->viaTable('order_item', ['order_id' => 'id']);

}



My question is, If I want to return results by order_item.id DESC , how should write this relationship?

[size="5"]i test this code[/size]




public function getItems()

{

    return $this->hasMany(Item::className(), ['id' => 'item_id'])

                ->viaTable('order_item', ['order_id' => 'id'], function ($query) {

            $query->orderBy(['order_item.id' => SORT_DESC]);

        });

}



According debug bar view my test results, check out the order_item found as I really want to sort of form, but in the end the query associated data in, yii generated id in (‘3’, ‘2’, '1 ') conditions.

But this way the query results returned and did not last as I want to sort. I do not know how to solve.

[size="5"]query sql is[/size]




 1. SELECT * FROM `order` WHERE `id`='1'

 2. SELECT * FROM `order_item` WHERE `order.id`='1' ORDER BY `order_item`.`id` DESC

 3. SELECT * FROM `item` WHERE `order_item.id` IN ('101', '102', '103')



I use findbysql() or querybuilder, I can return the required dataset, and sort of hope, but I might have obsessive-compulsive disorder, so I hope use viatable can achieve the goal I want to achieve. Thank you.




public function getItems()

{

    return $this->hasMany(Item::className(), ['id' => 'item_id'])

                ->viaTable('order_item', ['order_id' => 'id'])

                ->orderBy(['order_item.id' => SORT_DESC]);

}



but is not work




SQLSTATE[42S22]: Column not found: 1054 Unknown column 'order_item.id' in 'order clause'

The SQL being executed was: SELECT * FROM `order` WHERE `id` IN ('101', '102', '103') ORDER BY `order_item`.`id` DESC






public function getItems()

{

    return $this->hasMany(Item::className(), ['id' => 'item_id'])

                ->viaTable('order_item', ['order_id' => 'id'])

                ->orderBy(['order_item.id' => SORT_DESC]);

}



use this ->orderBy('order_item.id desc);