在Gridview yii2中搜索
我有一张公交路线表,它有以下规则在Gridview yii2中搜索,gridview,yii2,Gridview,Yii2,我有一张公交路线表,它有以下规则 public function rules() { return [ [['schedule_id', 'stop_id'], 'required'], [['schedule_id', 'stop_id', 'route_order', 'is_destination'], 'integer'], [['departure_time'], 'safe']
public function rules()
{
return [
[['schedule_id', 'stop_id'], 'required'],
[['schedule_id', 'stop_id', 'route_order', 'is_destination'], 'integer'],
[['departure_time'], 'safe']
];
}
stop\u id
(外键)是表stops的主键(id)。
现在,我想在公交线路视图中显示相应的stop\u id
表中的stop\u name
。为此,我在模型中添加了以下代码
public function getStop()
{
return $this->hasOne(Stop::className(), ['id' => 'stop_id']);
}
在视图中
“住手,住手”
其工作正常,但搜索功能不适用于“停止名称”字段。其他字段显示要搜索的框,其中stop\u id
字段不显示要搜索的框。如何搜索此字段
编辑
BusrouteSearch.php
引自
在modelBusroute.php中
// ...
class Bus extends \yii\db\ActiveRecord
{
// ...
public function getStop()
{
return $this->hasOne(Stop::className(), ['id' => 'stop_id']);
}
public function getStop_name()
{
if(isset($this->stop->stop_name))
return $this->stop->stop_name;
else
return '(stop name not set)';
}
// ...
}
// ...
class BusrouteSearch extends Busroute
{
// ...
public $stop_name;
public function rules()
{
return [
// other attributes
[['stop_name'], 'safe'],
];
}
public function search($params)
{
$query = BusRoute::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
'id',
// other attribute
'stop_name' => [
'asc' => ['stops.stop_name' => SORT_ASC],
'desc' => ['stops.stop_name' => SORT_DESC],
'label' => 'Stop name',
'default' => SORT_ASC
],
// other attribute
]
]);
$query->joinWith(['stop']);
$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;
}
$query->andFilterWhere([
'id' => $this->id,
'schedule_id' => $this->schedule_id,
'departure_time' => $this->departure_time,
'route_order' => $this->route_order,
'is_destination' => $this->is_destination,
]);
$query->andFilterWhere(['like', 'stops.stop_name', $this->stop_name]);
return $dataProvider;
}
// ...
}
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
// other attributes
'stop_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
在搜索模型中BusrouteSearch.php
// ...
class Bus extends \yii\db\ActiveRecord
{
// ...
public function getStop()
{
return $this->hasOne(Stop::className(), ['id' => 'stop_id']);
}
public function getStop_name()
{
if(isset($this->stop->stop_name))
return $this->stop->stop_name;
else
return '(stop name not set)';
}
// ...
}
// ...
class BusrouteSearch extends Busroute
{
// ...
public $stop_name;
public function rules()
{
return [
// other attributes
[['stop_name'], 'safe'],
];
}
public function search($params)
{
$query = BusRoute::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->setSort([
'attributes' => [
'id',
// other attribute
'stop_name' => [
'asc' => ['stops.stop_name' => SORT_ASC],
'desc' => ['stops.stop_name' => SORT_DESC],
'label' => 'Stop name',
'default' => SORT_ASC
],
// other attribute
]
]);
$query->joinWith(['stop']);
$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;
}
$query->andFilterWhere([
'id' => $this->id,
'schedule_id' => $this->schedule_id,
'departure_time' => $this->departure_time,
'route_order' => $this->route_order,
'is_destination' => $this->is_destination,
]);
$query->andFilterWhere(['like', 'stops.stop_name', $this->stop_name]);
return $dataProvider;
}
// ...
}
<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],
'id',
// other attributes
'stop_name',
['class' => 'yii\grid\ActionColumn'],
],
]); ?>
在视图文件index.php
(可能@app/views/bus/index.php
)
showsearchModel
,Ajith.使用$query->andFilterWhere(['like','stop.stop\u name',$this->stop\u id])
它不起作用,没有搜索字段出现$dataProvider->sort->attributes[stop\u id']=['asc'=>['stop.stop\u name'=>sort\u asc],'desc desc代码>参考:和