我正在尝试在 Yii2 的 GridView 中为相关模型设置过滤器小部件,但我不断收到错误消息,例如过滤器值必须是整数。
我关注了this question .现在,我有两个模型 Services.php
和 ServiceCharge.php
。
在 ServiceCharge.php
中,关系设置如下:
public function getServiceName()
{
return $this->hasOne(Services::className(),['id'=>'service_name']);
}
在 ServiceChargeSearch.php
中的代码是这样的:
<?php
namespace app\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use app\models\ServiceCharges;
/**
* ServiceChargesSearch represents the model behind the search form about `app\models\ServiceCharges`.
*/
class ServiceChargesSearch extends ServiceCharges
{
/**
* @inheritdoc
*/
public function attributes()
{
// add related fields to searchable attributes
return array_merge(parent::attributes(), ['serviceName.services']);
}
public function rules()
{
return [
[['id'], 'integer'],
[['charges_cash', 'charges_cashless'], 'number'],
[['id', 'serviceName.services', 'room_category'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = ServiceCharges::find();
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$dataProvider->sort->attributes['serviceName.services'] = [
'asc' => ['serviceName.services' => SORT_ASC],
'desc' => ['serviceName.services' => SORT_DESC],
];
$query->joinWith(['serviceName']);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
// 'service_name' => $this->service_name,
'room_category' => $this->room_category,
'charges_cash' => $this->charges_cash,
'charges_cashless' => $this->charges_cashless,
])
->andFilterWhere(['LIKE', 'serviceName.services', $this->getAttribute('serviceName.services')]);
return $dataProvider;
}
}
在我的 Gridview 中它是这样设置的:
[
'attribute'=>'service_name',
'value'=>'serviceName.services',
],
正确显示相关模型中的服务名称。
我看不出我做错了什么,但是服务属性的筛选字段根本没有显示。
最佳答案
实际上它比看起来简单得多。
将
column_name
添加到 safe 属性。 注意:这应该是关系名称添加带有查询的连接 - 如 -
$query->joinWith(['serviceName','roomCategory']);
添加过滤条件如下:
->andFilterWhere(['like', 'services.services', $this->service_name]) ->andFilterWhere(['like', 'room_category.room_category', $this->room_category]);
如果要添加排序,请添加如下代码:
$dataProvider->sort->attributes['service_name'] = [ 'asc' => ['services.services' => SORT_ASC], 'desc' => ['services.services' => SORT_DESC], ]; $dataProvider->sort->attributes['room_category'] = [ 'asc' => ['room_category.room_category' => SORT_ASC], 'desc' => ['room_category.room_category' => SORT_DESC], ];
5 你还应该设置关系名称 public $roomCategory
就是这样。相关表的排序和过滤都非常有效。
注意:删除相关列的默认验证(如整数)和 gii
生成的默认过滤,否则会产生错误。
最新版本更新:
- 不需要添加公共(public) $attribute。
- 也不需要为关系添加安全属性。
- 但是你当前模型中你想要过滤的属性是 添加到安全属性是必须的。
- 最重要的是,在您的 gridview 中,相关属性必须 采用闭包格式。
这是例子
[
'attribute=>'attribute_name',
'value=function($data){
return $data->relationname->related_table_attribute_name
}
],
请记住,您使用的 relation_name.related_table_attribute_name
过滤器对我来说不起作用。
关于php - GridView 中相关模型的过滤器设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28285084/