Yii2:如何使用DataFilter中的一个filter属性来过滤多个属性



需要帮助使用一个过滤器属性过滤多个属性。

$filter = new ActiveDataFilter([
'searchModel' => static::class,
'attributeMap' => [
'detail' => '{{classification}}.[[name]]',
'detail' => '{{subclass}}.[[name]]',
'detail' => ...,
],
]);

在执行$filter->build()之后,过滤器条件仅为最后一个属性(即{{subclass}}.[[name]]属性)构建过滤器。

因此,我没有使用ActiveDataFilter,而是将搜索过程更改为正常搜索

public $detail;
public $classification;
public $subclass;
public function rules()
{
return [
[['object_code', 'detail', 'classification', 'subclass'], 'string'],
];
}
public function search($params)
{
$query = $this->model::find();
$query->joinWith([
'classification as classification',
'subclass as subclass',
'group as group',
'subgroup as subgroup',
]);
$dataProvider =  new ActiveDataProvider([
'query' => $query,
]);
$this->load($params, '');
if (!$this->validate()) {
return $dataProvider;
}
$query
->andFilterWhere(['like', 'classification.name', $this->classification])
->andFilterWhere(['like', 'subclass.name', $this->subclass])
->andFilterWhere([
'or',
['like', 'classification.name', $this->detail],
['like', 'subclass.name', $this->detail],
['like', 'group.name', $this->detail],
['like', 'subgroup.name', $this->detail],
['like', 'subobject_details', $this->detail],
['like', 'object_details', $this->detail],
]);
return $dataProvider;
}

我不认为这将是一个很好的回答我最初的问题,但我想把这个作为一个替代的解决方案。

相关内容

  • 没有找到相关文章

最新更新