通过 Table Yii2 搜索模型
Search Model Via Table Yii2
我有三个 tables:
user
game
user_game
game.user_id
- 创建游戏的用户。 Table user_game
描述了 ADDED 游戏未创建的用户,它
有 user_id
和 game_id
字段。我有 GameSearch 模型,它应该搜索当前用户添加的游戏。这里 搜索方法;
public function search($params)
{
// HERE I SHOULD GET ONLY GAMES WHICH ADDED BY USER via table user_game
$query = Game::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => [
'sorting' => SORT_DESC,
]
],
]);
if (!empty($params['pageSize'])) {
$dataProvider->pagination->pageSize = $params['pageSize'];
}
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'user_id' => $this->user_id,
'visible' => $this->visible,
'sorting' => $this->sorting,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'slug', $this->slug])
->andFilterWhere(['like', 'image', $this->image])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['>=', 'created_at', $this->date_from ? $this->date_from : null])
->andFilterWhere(['<=', 'created_at', $this->date_to ? $this->date_to : null])
->andFilterWhere(['>=', 'updated_at', $this->date_upd_from ? $this->date_upd_from : null])
->andFilterWhere(['<=', 'updated_at', $this->date_upd_to ? $this->date_upd_to : null]);
return $dataProvider;
}
所以我需要通过 table user_game 获取游戏列表,其中 user_id = 当前用户 ID,game_id = 游戏 ID。请帮忙。
首先你需要在 Game model
中首先有两个关系,它将从 user_game
table (model)
中获取一对多的数据,然后在模型 user_game
中你需要写从 user
table (model)
获取用户的关系
$query->joinWith(['userGame', 'userGame.user']);
->andFilterWhere(['=', 'tbl_user.id', Yii::app()->user->id])
我有三个 tables:
user
game
user_game
game.user_id
- 创建游戏的用户。 Table user_game
描述了 ADDED 游戏未创建的用户,它
有 user_id
和 game_id
字段。我有 GameSearch 模型,它应该搜索当前用户添加的游戏。这里 搜索方法;
public function search($params)
{
// HERE I SHOULD GET ONLY GAMES WHICH ADDED BY USER via table user_game
$query = Game::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' => [
'defaultOrder' => [
'sorting' => SORT_DESC,
]
],
]);
if (!empty($params['pageSize'])) {
$dataProvider->pagination->pageSize = $params['pageSize'];
}
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
// grid filtering conditions
$query->andFilterWhere([
'id' => $this->id,
'user_id' => $this->user_id,
'visible' => $this->visible,
'sorting' => $this->sorting,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
]);
$query->andFilterWhere(['like', 'name', $this->name])
->andFilterWhere(['like', 'slug', $this->slug])
->andFilterWhere(['like', 'image', $this->image])
->andFilterWhere(['like', 'description', $this->description])
->andFilterWhere(['>=', 'created_at', $this->date_from ? $this->date_from : null])
->andFilterWhere(['<=', 'created_at', $this->date_to ? $this->date_to : null])
->andFilterWhere(['>=', 'updated_at', $this->date_upd_from ? $this->date_upd_from : null])
->andFilterWhere(['<=', 'updated_at', $this->date_upd_to ? $this->date_upd_to : null]);
return $dataProvider;
}
所以我需要通过 table user_game 获取游戏列表,其中 user_id = 当前用户 ID,game_id = 游戏 ID。请帮忙。
首先你需要在 Game model
中首先有两个关系,它将从 user_game
table (model)
中获取一对多的数据,然后在模型 user_game
中你需要写从 user
table (model)
$query->joinWith(['userGame', 'userGame.user']);
->andFilterWhere(['=', 'tbl_user.id', Yii::app()->user->id])