Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php GridView中相关模型的过滤器设置_Php_Gridview_Yii_Yii2 - Fatal编程技术网

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

Php GridView中相关模型的过滤器设置,php,gridview,yii,yii2,Php,Gridview,Yii,Yii2,我试图在Yii2的小部件中为相关模型设置过滤器,但我不断得到错误,比如过滤器值必须是整数 我跟着。现在,我有两个模型Services.php和ServiceCharge.php 在ServiceCharge.php中,关系设置如下: public function getServiceName() { return $this->hasOne(Services::className(),['id'=>'service_name']); } 在Serv

我试图在Yii2的小部件中为相关模型设置过滤器,但我不断得到错误,比如过滤器值必须是整数

我跟着。现在,我有两个模型
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',

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


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

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

  • 列\u 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
    生成的默认筛选,否则将生成错误

    最新版本更新:

    • 不需要添加Public$属性
    • 也不需要为关系添加安全属性
    • 但是当前模型中需要筛选的属性是 添加到必须的安全属性
    • 最重要的是,在gridview中,相关属性必须 以闭包形式
    这就是一个例子

    [
    'attribute=>'attribute_name',
    'value=function($data){
         return $data->relationname->related_table_attribute_name
    }
    ],
    

    请记住,您使用的是
    关系\u名称。相关的\u表\u属性\u名称
    过滤器在某种程度上对我不起作用。

    有一套相当全面的说明。唯一需要注意的是,搜索模型抱怨以下几行内容,但如果没有它们,一切似乎都能正常工作:

    $this->addCondition(...);
    
    对于模型PaymentEvent(表:subs_payment_event),其货币id字段链接到模型货币,这是完整的附加代码集(使用基本模板):

    在主模型PaymentEvent.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列数组中(包括我通常指向相关模型记录的链接):


    你可以把完整的php代码发布在“ServiceChargeSearch.php”中吗。错误是由于验证程序已配置incorrectly@BalajiViswanath-更新并添加了
    serviceChargeSearch.php的完整代码
    
    [
        'attribute' => 'currencyName',
        'label' => 'Currency',
        'format' => 'raw',
        'value' => function ($data) {
                return Html::a($data->currency->name, ['/currency/' . $data->currency_id]);
            },
    ],