【发布时间】: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