我试图在条件过滤器中传递数组元素。
getFacilityID()函数
public function getFacilityID()
{
$facArray= array();
$facilityName = Facility::find()->where(['company_id' => \Yii::$app->user->identity->company_id])->all();
foreach ($facilityName as $facName){
$facArray[] = $facName->facility_id;
}
// var_dump($facArray); exit();
return $facArray;
}
我在这里使用 getFacilityID()函数进行过滤
$query->orFilterWhere(['like', 'area_name', $this->area_name])
->andFilterWhere(['like', 'facility_id', $this->getfacilityID()]);
问题是,如果我有超过1个设施,它不会显示任何区域。
" - > andFilterWhere([' like',' facility_id',$ this-> getfacilityID()]); "
粗体代码导致问题,如果getfacilityId()具有多于1个数组元素,则它不会在区域中显示数据。如果getFacilityId()中只有一个元素,则它会正确显示数据。我可以为此获得解决方案吗?
提前致谢..
答案 0 :(得分:1)
由于 Topher 提到不能使用类似的数组。所以将查询更改为In条件有效。
$query->orFilterWhere(['like', 'area_name', $this->area_name])
->andFilterWhere(['in', 'facility_id', $this->getFacilityID()]);
了解详情:http://www.yiiframework.com/doc-2.0/yii-db-queryinterface.html#where()-detail
答案 1 :(得分:1)
试试这段代码:
if(null != $this->getFacilityID())
foreach ($this->getfacilityID() as $key => $id) {
$query->orFilterWhere(['like', 'facility_id', $id]);
}
$query->andFilterWhere(['like', 'area_name', $this->area_name]);
$query->all();
这是我的例子
foreach (['a1','b1'] as $key => $value) {
$q->orFilterWhere(['like', 'username', $value]);
}
$q->andFilterWhere(['like', 'email', 'someEmail']);
$q->all();
,结果查询为:
SELECT
*
FROM
`user`
WHERE
((`username` LIKE '%a1%')
OR (`username` LIKE '%b1%'))
AND (`email` LIKE '%someEmail%')