我正在研究yii2
。我有一个型号名RouteMeters
。在这里,我有一个meter_id
,其中保存了仪表的所有id。在我的索引视图中,我显示meter_id
的值,如下所示
[
'attribute' => 'mter_id',
'label' => 'Meters',
'value' => function ($data) {
if (is_object($data->mter))
return $data->mter->meter_msn;
return ' - ';
},
],
视图如下
但是当我使用仪表编号搜索仪表时,找不到记录。
以下是RouteMeters
public function search($params)
{
$query = RouteMeters::find();
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$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,
'route_id' => $this->route_id,
'mter_id' => $this->mter_id,
'user_id' => $this->user_id,
]);
if (!Yii::$app->user->isGuest && in_array(Yii::$app->user->identity->user_role, [3,4])) {
$query->joinWith('teamsuser', true);
$query->andFilterWhere(['=', 'user.group_id', Yii::$app->user->identity->group_id]);
}
$query->orderBy(['id' => SORT_DESC]);
return $dataProvider;
}
现在在我的搜索模型中,我想放置一个查询,它会在我的索引视图中搜索时为我提供所选仪表编号的记录。
电表的序列号位于我的meters
表格中,文件名为meter_msn
,我希望与之建立联系。
我不知道如何处理我的搜索模型。
任何帮助都将受到高度赞赏。
答案 0 :(得分:1)
public function search($params)
{
$query = RouteMeters::find()->joinWith('mter');
// add conditions that should always apply here
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$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,
'route_id' => $this->route_id,
//'mter_id' => $this->mter_id,
'user_id' => $this->user_id,
]);
if (!Yii::$app->user->isGuest && in_array(Yii::$app->user->identity->user_role, [3,4])) {
$query->joinWith('teamsuser', true);
$query->andFilterWhere(['=', 'user.group_id', Yii::$app->user->identity->group_id]);
}
$query->andFilterWhere(['like', 'meterTableName.meter_msn', $this->mter_id]);
$query->orderBy(['id' => SORT_DESC]);
return $dataProvider;
}