php - GridView 中相关模型的过滤器设置

标签 php gridview yii yii2

我正在尝试在 Yii2 的 GridView 中为相关模型设置过滤器小部件,但我不断收到错误消息,例如过滤器值必须是整数。

我关注了this question .现在,我有两个模型 Services.phpServiceCharge.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',

            ],

正确显示相关模型中的服务名称。

我看不出我做错了什么,但是服务属性的筛选字段根本没有显示。

最佳答案

实际上它比看起来简单得多。

  1. column_name 添加到 safe 属性。 注意:这应该是关系名称

  2. 添加带有查询的连接 - 如 - $query->joinWith(['serviceName','roomCategory']);

  3. 添加过滤条件如下:

    ->andFilterWhere(['like', 'services.services', $this->service_name])
    ->andFilterWhere(['like', 'room_category.room_category', $this->room_category]);
    
  4. 如果要添加排序,请添加如下代码:

    $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/

相关文章:

php - 使用 ajax Php 和 mysql 级联下拉菜单

php - 当我尝试使用 laravel 添加主键的数据类型字符串时发生错误

php mysqli 更新集不起作用

c# - 从另一个函数调用 RowDataBound

wpf - 使用 ListView 和 GridView 删除额外的 "Space"

mysql - 使用 ORDER BY 时如何减少 sql 查询时间

php - 使用 PHP Mcrypt 加密并使用 MySQL aes_decrypt 解密?

c# - 如何将一系列名称分成不同的行

Yii:是否可以从外部页面链接指定的CJuiTabs?

php - 通过模型上的特定方法订购 CGridView