首页 > 解决方案 > 需要帮助以使用 activedataprovider 构建查询

问题描述

我想在 yii2 ActiveDataProvider 中执行以下 WHERE 条件

预期在哪里条件:

$query="WHERE VoterName Like '%s%' AND (contactno != '' OR whatsapp_no!= '')";

我目前的 where 条件:

$query->andFilterWhere(['like', 'VoterName', $this->VoterName]);
$query->orWhere(['<>', 'contactno', ''])->orWhere(['<>', 'whatsapp_no', '']);

我只想获取那些有contactno或whatsapp_no的记录。

标签: mysqlyii2yii2-advanced-app

解决方案


当您需要设置多个条件时,您必须使用andWhere,例如对于您的问题:

$query->andFilterWhere(['like', 'VoterName', $this->VoterName]);
$query->andWhere(['OR',['<>', 'contactno', ''],['<>', 'whatsapp_no', '']]);

推荐阅读