gpt4 book ai didi

gridview - 在 Gridview yii2 中搜索

转载 作者:行者123 更新时间:2023-12-02 20:05:50 50 4
gpt4 key购买 nike

我有一个表bus_route它有以下规则

public function rules()
{
return [
[['schedule_id', 'stop_id'], 'required'],
[['schedule_id', 'stop_id', 'route_order', 'is_destination'], 'integer'],
[['departure_time'], 'safe']
];
}

stop_id(外键)是表stops的主键(id)。现在我想在公交车路线 View 中显示相应 stop_idstops 表中的 stop_name 。为此,我在 model

中添加了以下代码
public function getStop()
{
return $this->hasOne(Stop::className(), ['id' => 'stop_id']);
}

并且在 View

'stop.stop_name',

它可以工作,但搜索功能不适用于站点名称字段。其他字段显示一个用于搜索的框,其中 stop_id 字段不显示用于搜索的框。我如何搜索该字段?

编辑

BusrouteSearch.php

 public function search($params)
{
$query = BusRoute::find();
$query->joinWith(['stop']);

$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;
}

$query->andFilterWhere([
'id' => $this->id,
'schedule_id' => $this->schedule_id,
'departure_time' => $this->departure_time,
'stop_id' => $this->stop_id,
'route_order' => $this->route_order,
'is_destination' => $this->is_destination,
]);


return $dataProvider;
}

最佳答案

引用here

在模型Busroute.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)';
}
// ...
}
// ...

在搜索模型中BusrouteSearch.php

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;
}


// ...
}

在 View 文件index.php中(可能是@app/views/bus/index.php)

<?= GridView::widget([
'dataProvider' => $dataProvider,
'filterModel' => $searchModel,
'columns' => [
['class' => 'yii\grid\SerialColumn'],

'id',
// other attributes
'stop_name',

['class' => 'yii\grid\ActionColumn'],
],
]); ?>

关于gridview - 在 Gridview yii2 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35291049/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com