【问题标题】:Yii2 GridView Filtering on Relational Column not RespondingYii2 GridView过滤关系列没有响应
【发布时间】:2017-03-17 12:48:13
【问题描述】:

我正在尝试过滤关系数据列。

在我的搜索模型中,我添加了字段

public function attributes()
{        
    // add related fields to searchable attributes
    return array_merge(parent::attributes(), ['customerProductBaseProduct.product_name');
}

使它成为一个安全的搜索字段

['customerProductBaseProduct.product_name'], 'safe'],

在模型的搜索功能中,我添加了一个 $query->joinWith

$query = CustomerProducts::find();

    // add conditions that should always apply here
    $query->joinWith(['customerProductBaseProduct']);

    $dataProvider = new ActiveDataProvider([
        'query' => $query,
    ]);

    $this->load($params);

还有一个 ->andFilterWhere

$query->andFilterWhere(['like', 'customer_product_customerID', $this->customer_product_customerID])
        ->andFilterWhere(['like', 'customer_product_formula', $this->customer_product_formula])
        ->andFilterWhere(['like', 'customer_product_name', $this->customer_product_name])
        ->andFilterWhere(['like', 'customer_product_sub_name', $this->customer_product_sub_name])
        ->andFilterWhere(['like', 'customer_product_spanish', $this->customer_product_spanish])
        ->andFilterWhere(['like', 'customer_product_category', $this->customer_product_category])
        ->andFilterWhere(['like', 'customerProductBaseProduct.product_name', $this->customerProductBaseProduct]);

当我尝试过滤时,该列不执行任何操作。我做错了什么?

【问题讨论】:

  • 当你在该列的过滤器中放入一些东西时,如果你 var_dump 变量 $this->customerProductBaseProduct 会发生什么?
  • 返回空值
  • 这是因为$this->load($params); 没有将您正在搜索的参数值加载到模型中。您的查询网址如何?
  • 感谢您的帮助。我能够让它工作;见下文。

标签: gridview yii2 yii2-basic-app


【解决方案1】:

这就是我过滤关系列的方式

class UserSearch extends User
{
    public $company_name;
    public function rules()
    {
        return [
            [['first_name', 'last_name', 'email', 'company_name'], 'safe']
        ];
    }

    public function search()
    {
        $query = User::find();
        $query->joinWith(['client c']); //set relation alias

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query
            ->andFilterWhere(['like', 'first_name', $this->first_name])
            ->andFilterWhere(['like', 'last_name', $this->last_name])
            ->andFilterWhere(['like', 'email', $this->email])
            ->andFilterWhere(['like', "c.company_name", $this->company_name]);

        return $dataProvider;
    }
}

并在列定义中使用company_name 属性。

【讨论】:

  • 什么是 c.company_name in ->andFilterWhere(['like', "c.company_name", $this->company_name])?您的联接位于表客户端 c
  • c 是我在$query->joinWith(['client c']); 上设置的client 表的别名。它在文档\yii\db\ActiveQuery::joinWith
  • 您需要别名吗?您可以改用 client.company_name 吗?
  • 你确定吗?看看这个查询SELECT COUNT(*) FROM users LEFT JOIN clients ON users.client_id = clients.id WHERE (client.company_name LIKE '%vir%'),它来自$query->joinWith(['client']),没有别名,并通过Unknown column 'client.company_name' in 'where clause'
  • 嗯。不确定方法,但使用别名有效。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-12
  • 1970-01-01
  • 2019-02-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多