在GridView中为相关模型过滤设置 [英] Filter setup for related model in GridView
问题描述
我试图在Yii2的 GridView中设置相关模型的过滤器部件,但我不断收到错误,就像过滤器值必须是一个整数。
我遵循这个问题。现在,我有两个模型 Services.php
和 ServiceCharge.php
。
在 ServiceCharge.php
中,关系设置为:
{
return $ this-> hasOne(Services :: className(),['id'=>'service_name']);
在 ServiceChargeSearch.php $ c $代码如下所示:
<?php
命名空间app \models;
使用Yii;
使用yii\base\Model;
使用yii\data\ActiveDataProvider;
使用app \models\ServiceCharges;
/ **
* ServiceChargesSearch表示关于`app \models\ServiceCharges`的搜索表单后面的模型。
* /
class ServiceChargesSearch extends ServiceCharges
{
/ **
* @inheritdoc
* /
public function attributes()
{
//将相关字段添加到可搜索属性
返回array_merge(parent :: attributes(),['serviceName.services']);
$ b public function rules()
{
return [
[['id'],'integer'],
[ ['charges_cash','charges_cashless'],'数字'],
[['id','serviceName.services','room_category'],'safe'],
];
$ b / **
* @inheritdoc
* /
公共功能场景()
{
//绕过在父类中的scenario()实现
return Model :: scenarios();
}
/ **
*使用搜索查询创建数据提供程序实例
*
* @param array $ params
*
* @return ActiveDataProvider
* /
公共函数搜索($ 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->载入($ params);
if(!$ this-> validate()){
//如果您在验证失败时不想记录任何记录,请取消注释以下行
// $ query - 化合物其中( '0 = 1');
返回$ 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')]);
返回$ dataProvider;
$ p
$ b $ p $并且在我的Gridview中它的设置是这样的: / p>
[
'属性'=>'service_name',
'value'=>'serviceName .services',
],
显示的服务名称来自相关的模型正确。
我无法看到我做错了什么,但服务属性的过滤字段根本没有显示。
解决方案其实它比看起来简单得多。
-
将 column_name
添加到安全属性中。 like - $ query-> joinWith(['serviceName','roomCategory']);
-
添加过滤条件,如:
- > andFilterWhere(['like','services.services',$ this- > service_name])
- > andFilt erWhere(['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],
];
就是这样。注意:删除默认验证,如相关列的整数和由 gii $生成的默认过滤。 c $ c>否则会产生错误。
I am trying to setup the filter for related model in Yii2's GridView widget, but I am keep getting the error like the filter value must be an integer.
I have followed this question. Now, I have a two models Services.php
and ServiceCharge.php
.
In ServiceCharge.php
the relation is setup like:
public function getServiceName()
{
return $this->hasOne(Services::className(),['id'=>'service_name']);
}
In the ServiceChargeSearch.php
the code is like this:
<?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;
}
}
and in my Gridview it is setup like this:
[
'attribute'=>'service_name',
'value'=>'serviceName.services',
],
Which is showing the services name from the related model correctly.
I am not able to see what I am doing wrong, but the filter field for the attribute for service is not showing at all.
解决方案 Actually it is much simpler than it seems.
add the column_name
to safe attribute.
add the join with query - like - $query->joinWith(['serviceName','roomCategory']);
add the filter condition like:
->andFilterWhere(['like', 'services.services', $this->service_name])
->andFilterWhere(['like', 'room_category.room_category', $this->room_category]);
if like to add sorting add the code like:
$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],
];
That's it. Both sorting and filtering for related table works perfectly.
Note: Remove default validation like integer for related column and default filtering generated by gii
otherwise it will generate an error.
这篇关于在GridView中为相关模型过滤设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!