yii2 gridview只显示相关记录

5jvtdoz2  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(314)

我有这样的数据库结构:

我只需要用户在ReservationsEachModel中获取仓库中属于该用户的那些预订。模型用户\u has \u warehouse有一个函数getuserhaswarehouse($user\u id,$warehouse\u id)来检查关系。我能够得到的关系时,查看单一,删除和更新控制器中的记录。但是我不能让searchmodel返回属于当前用户的预订。我不能依赖于reservation->user\u id,因为该值不是必需的,并且可以在没有user\u id的情况下公开插入预订。如何使searchmodel正常工作,从而只显示属于用户拥有仓库的仓库的预订?
预订搜索模型:

$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,
    ]);

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

guykilcj

guykilcj1#

设法解决了。

$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]);

相关问题