Yii2 GridView仅显示相关记录

时间:2018-05-23 15:23:28

标签: mysql yii2 foreign-keys relational-database

我有这个数据库结构: database model

我需要用户只获取属于该用户的仓库中的ReservationsSeachModel中的那些预留。模型user_has_warehouse有一个函数getUserHasWarehouse($ user_id,$ warehouse_id)来检查关系。在查看控制器中的单个,删除和更新记录时,我能够获得关系。但我无法让SearchModel返回属于当前用户的预订。 我不能依赖Reservation-> user_id,因为该值不是强制性的,并且可以公开插入预订而不使用user_id。如何使SearchModel正常工作,以便只显示属于用户拥有仓库的仓库的预订?

ReservationsSearchModel:

$warehouse_id = User_Has_Warehouse::find()->select(['warehouse_id'])->where(['user_id' => Yii::$app->user->id]);
    $freezer_type_id = Freezer_type::find()->select(['id'])->where(['warehouse_id' => $warehouse_id]);
    $reservation_id = Reservation_freezer_type::find()->select(['reservation_id'])->where(['freezer_type_id'=>$freezer_type_id]);
...
$query->andFilterWhere([
        'id' => $reservation_id,
        'customer_id' => $this->customer_id,
        'user_id' => $this->user_id,
    ]);

...

但以下对我不起作用。我做错了什么?

1 个答案:

答案 0 :(得分:0)

管理解决它。

$h = User_Has_Warehouse::find()->select(['warehouse_id'])->where(['user_id' => Yii::$app->user->id]);
$rt = Freezer_type::find()->select(['id'])->where(['warehouse_id' => $h]);
$res = Reservation_freezer_type::find()->select(['reservation_id'])->where(['freezer_type_id' => $rt]);

// grid filtering conditions
$query->andFilterWhere(['id' => $res]);