有:Yii2 + MongoDb 和两个集合:StorageItems(货物)和 Storage(仓库):
我通过“存储”连接中的 storage_id 在 GridView 中显示来自 StorageItems + [b]name[/b] 仓库的字段。出现排序 [b]name[/b] 字段的链接,但实际上按名称字段排序不起作用。错误在哪里?我求你帮忙!(我翻遍了我能找到的所有东西,并尝试了所有能找到的选项......它没有帮助......)
存储物品:
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']);
}
}
存储物品搜索:
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;
}
看法:
...
<?=
GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
[
'attribute' => 'storage',
'value' => 'storage.name',
],
...
]
]);?>
事实证明,你需要使用'content'而不是'value',+发现另一个错误:在'attribute'中你需要使用的不是关系的名称('storage'),而是'storage_id'字段,这一切都奏效了!