yii2-如何使用搜索模型搜索值
yii2- how to search a value using search model
我正在研究 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
table 中,文件名 meter_msn
,我想加入它。
我不知道如何处理我的搜索模型。
非常感谢任何帮助。
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;
}
我正在研究 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
table 中,文件名 meter_msn
,我想加入它。
我不知道如何处理我的搜索模型。
非常感谢任何帮助。
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;
}