Orderby In Hasmany Relation

There is a way to sort the ActiveRelation of a Model?

in the model

public function getChestProducts() {

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

                    ->viaTable('map_chest_product', ['id_chest' => 'id']);


in the controller

$chest = \app\models\Chest::find($idc);



“$chest->chestproducts” is ordered by ID but it should be sorted by name… it’s possible?

Technically you should be able to pass the orderBy when you define the relation… as mentioned in the AR Documentation example with [font="Courier New"]getBigOrders[/font].

public function getChestProducts() {

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

                    ->viaTable('map_chest_product', ['id_chest' => 'id'])

                    ->orderBy('name ASC');


However this does not currently work exactly on my setup. Let know if it works for you.

It works!

I did not understand very well the difference between ActiveRelation and ActiveRecord.

thank you

Good for you then :D, must have missed something in my testing before I posted.