为了账号安全,请及时绑定邮箱和手机立即绑定

yii框架中的andFilterWhere 和 andWhere的区别

yii框架中的andFilterWhere 和 andWhere的区别

Yii
潇潇雨雨 2018-12-30 04:00:37
yii框架中的andFilterWhere 和 andWhere的区别
查看完整描述

1 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

附加条件
你可以使用 andWhere() 或者 orWhere() 在原有条件的基础上 附加额外的条件。你可以多次调用这些方法来分别追加不同的条件。 例如,
$status = 10;
$search = 'yii';

$query->where(['status' => $status]);

if (!empty($search)) {
$query->andWhere(['like', 'title', $search]);
}12345678

如果 $search 不为空,那么将会生成如下 SQL 语句:
... WHERE (`status` = 10) AND (`title` LIKE '%yii%')1

过滤条件
当 WHERE 条件来自于用户的输入时,你通常需要忽略用户输入的空值。 例如,在一个可以通过用户名或者邮箱搜索的表单当中,用户名或者邮箱 输入框没有输入任何东西,这种情况下你想要忽略掉对应的搜索条件, 那么你就可以使用 yii\db\Query::filterWhere() 方法来实现这个目的:
// $username 和 $email 来自于用户的输入
$query->filterWhere([
'username' => $username,
'email' => $email,
]);12345

yii\db\Query::filterWhere() 和 where() 唯一的不同就在于,前者 将忽略在条件当中的hash format的空值。所以如果 email为空而username 不为空,那么上面的代码最终将生产如下 SQL …WHERE username=:username。
提示: 当一个值为 null、空数组、空字符串或者一个只包含空白字符时,那么它将被判定为空值。
类似于 [yii\db\Query::andWhere()|andWhere()]] 和 orWhere(), 你可以使用 yii\db\Query::andFilterWhere() 和yii\db\Query::orFilterWhere() 方法 来追加额外的过滤条件。
andWhere和andFilterWhere都可以用来追加条件,只是andFilterWhere会忽略条件中的空值



查看完整回答
反对 回复 2019-01-03
  • 1 回答
  • 0 关注
  • 693 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信