我的模型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;
问题在于我正在尝试加入当前未加入的表格。