按相关字段(2度远)在网格视图中进行筛选和排序

时间:2015-07-28 11:05:47

标签: sorting gridview filter yii2

我的模型Order具有以下功能:

public function getAccount()
    {
        return $this->hasOne(Account::className(), ['id' => 'account_id']);
    }

    public function getChairName()
    {
        return $this->account->chairName;
    }

问题是chairName本身是一个相关字段(在另一个模型Account中创建):

public function getChair0()
    {
        return $this->hasOne(Category::className(), ['id' => 'chair']);
    }

    public function getChairName()
    {
        return $this->chair0->name;
    }

在我的OrderSearch模型中,我现在尝试按chair的名称进行搜索和过滤。在Account中,主席与id一起存储,该Category链接到模型OrderSearch

我一直在看这个链接,但它没有帮助我解决我的问题: http://www.yiiframework.com/wiki/621/filter-sort-by-calculated-related-fields-in-gridview-yii-2-0/

我的<?php namespace app\models; use Yii; use yii\base\Model; use yii\data\ActiveDataProvider; use app\models\OrderHistory; [...] class OrderHistorySearch extends OrderHistory { [...] public $chairName; [...] public function rules() { return [ [...] [['chairName'], 'safe'], [...] ]; } [...] public function scenarios() { [...] } public function search($params) { $query = OrderHistory::find(); $query->joinWith(['employee', 'account', 'item']); $dataProvider = new ActiveDataProvider([ 'query' => $query, ]); [...] $dataProvider->sort->attributes['chairName'] = [ 'asc' => ['account.chair' => SORT_ASC], 'desc' => ['account.chair' => SORT_DESC], ]; [...] $this->load($params); if (!$this->validate()) { [...] } $query->andFilterWhere([ [...] ]); $query->andFilterWhere(['like', 'category.name', $this->chairName]); return $dataProvider; } } 模型看起来像这样:

$query->joinWith(['employee', 'account', 'item']);

我认为我的错误在于某处: ofstream& operator= (const ofstream&) = delete;

问题在于我正在尝试加入当前未加入的表格。

0 个答案:

没有答案