我正在尝试在Yii2的GridView小部件中为相关模型设置过滤器,但是我不断收到错误,例如过滤器值必须是整数。
我一直在关注这个问题。现在,我有两个模型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;
}
}
在我的网格视图中,它的设置是这样的:
[
'attribute'=>'service_name',
'value'=>'serviceName.services',
],
这是正确显示相关模型中的服务名称。
我无法看到我做错了什么,但是服务属性的过滤器字段根本没有显示。
实际上它比看起来简单得多。
-
将"
column_name
添加到安全"属性。注意:这应该是关系名称 -
添加"联接"查询 - 喜欢 -
$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
生成的默认过滤,否则将生成错误。
最新版本更新:
- 不需要添加公共$attribute。
- 也不需要为关系添加安全属性。
- 但是当前模型中要过滤的属性是添加到安全属性是必须的。
- 最重要的是,在您的 GridView 中,相关属性必须采用闭包格式。
那是例子
[
'attribute=>'attribute_name',
'value=function($data){
return $data->relationname->related_table_attribute_name
}
],
请记住,您正在使用relation_name.related_table_attribute_name
过滤器不知何故对我不起作用。
Yii 框架网站上有一套相当全面的说明。唯一需要注意的是,搜索模型抱怨以下行,但如果没有它们,一切似乎都按预期工作:
$this->addCondition(...);
对于具有链接到模型货币的currency_id字段的模型 PaymentEvent(表:subs_payment_event),这是完整的附加代码集(使用 Basic 模板):
在主模型中,支付事件.php:
public function getCurrencyName()
{
return $this->currency->name;
}
在搜索模型中,PaymentEventSearch.php:
public $currencyName;
在其规则中:
[['currencyName'], 'safe'],
在其 setSort 语句的属性中,包括:
'currencyName' => [
'asc' => ['subs_currency.name' => SORT_ASC],
'desc' => ['subs_currency.name' => SORT_DESC],
'label' => 'Currency'
],
网格过滤条件前:
$query->joinWith(['currency' => function ($q) {
$q->where('subs_currency.name LIKE "%' . $this->currencyName . '%"');
}]);
最后,在视图中的 GridView 列数组中(包括我通常指向相关模型记录的链接):
[
'attribute' => 'currencyName',
'label' => 'Currency',
'format' => 'raw',
'value' => function ($data) {
return Html::a($data->currency->name, ['/currency/' . $data->currency_id]);
},
],