There are: Yii2 + MongoDb and two collections: StorageItems (goods) and Storage (warehouses):

I bring the fields from the StorageItems + [b] name [/ b] of the warehouse to the GridView by its storage_id from the "storage" connection. The link to sort the field [b] name [/ b] appears but the actual sorting by the Name field does not work. Where is the mistake? I beg you to help! (I rummaged through all that was possible and tried all the options I just found ... it doesn't help ...)

StorageItems (goods): enter image description here

Storage (warehouses): enter image description here

StorageItems:

class StorageItems extends ActiveRecord { public function attributes() { return [ '_id', 'item_id', 'storage_id', 'qty',... ]; } public function rules() { return [ [['item_id', 'storage_id'], 'filter', 'filter' => function ($value) { return new ObjectId($value); }], [['qty'], 'integer'], [['_id'], 'safe'], ]; public function getStorage() { return $this->hasOne(Storage::className(), ['_id' => 'storage_id']); } } 

StorageItemsSearch:

 class StorageItemsSearch extends StorageItems { public $storage; public function attributes() { return [ '_id', 'item_id', 'storage_id', 'qty', 'storage', 'items' ]; } public function rules() { return [ [['qty'], 'integer'], [['storage_id', 'item_id', '_id', 'storage',' items'], 'safe'], ]; } ... public function search($params) { $query = StorageItems::find(); $query->with(['items', 'storage']); $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); $dataProvider->setSort([ 'attributes' => [ 'storage' => [ 'asc' => [Storage::CollectionName().'name' => SORT_ASC], 'desc' => [Storage::CollectionName().'name' => SORT_DESC], ], ] ]); $this->load($params); if (!$this->validate()) { return $dataProvider; } .... return $dataProvider; } 

View:

 ... <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [ [ 'attribute' => 'storage', 'value' => 'storage.name', ], ... ] ]);?> 

    1 answer 1

    As it turned out, you need to use 'content' instead of 'value', + found another error: in 'attribute' you need to use not the name of the connection ('storage'), but the field 'storage_id' and it all worked!