yii2 ActiveQuery 'OR LIKE' 过滤器

编程入门 行业动态 更新时间:2024-10-27 04:34:51
本文介绍了yii2 ActiveQuery 'OR LIKE' 过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一项任务 - 添加到按全名搜索的搜索模型.全名是名字+姓氏.所以我需要构建像

这样的查询

WHERE first_name LIKE '%var%' OR last_name LIKE '%var%'

我能做到的唯一方法:

$query->andFilterWhere(['或者','profiles.first_name LIKE "%' . $this->userFullName . '%" ','profiles.last_name LIKE "%' . $this->userFullName . '%"']);

但我不喜欢它,因为它不安全.我不知道怎么...我认为有一种方法可以使用 yii2 活动构建器来构建这样的查询,我想获得类似

的结果

$query->andFilterWhere(['LIKE', 'profiles.first_name', $this->userFullName]);$query->andFilterWhere(['OR LIKE', 'profiles.last_name', $this->userFullName]);

问题出在查询中,例如,我可以使用数组作为属性将被匹配的值,但我不能使用数组作为要与之进行比较的属性列表.

$subQuery1 = Profile::find()->Where(['LIKE', 'profiles.first_name', $this->userFullName]);$subQuery2 = Profile::find()->Where(['LIKE', 'profiles.last_name', $this->userFullName]);//我认为它重载(3个查询而不是1个但仍然)和最终查询$query->andFilterWhere(['或者',$subQuery1,$subQuery2]);

任何想法如何构建没有%"的查询?

解决方案

你应该简单地尝试:

$query->andFilterWhere(['或者',['like', 'profiles.first_name', $this->userFullName],['like', 'profiles.last_name', $this->userFullName],]);

or:类似于 and 运算符,不同之处在于操作数使用 OR 连接.例如,['or', ['type' =>[7, 8, 9]], ['id' =>[1, 2, 3]] 将生成 (type IN (7, 8, 9) OR (id IN (1, 2, 3))).

阅读更多:www.yiiframework/doc-2.0/yii-db-queryinterface.html#where()-detail

i have a task - add to search model searching by full name. Full name is first name + last name. So i need to build query like

WHERE first_name LIKE '%var%' OR last_name LIKE '%var%'

The only way i could do it :

$query->andFilterWhere([ 'OR', 'profiles.first_name LIKE "%' . $this->userFullName . '%" ', 'profiles.last_name LIKE "%' . $this->userFullName . '%"' ]);

But i dont like it because % its unsafe. I dont know how... I think there is the way to build such query with yii2 active builder, and i would like to get in result smth like

$query->andFilterWhere(['LIKE', 'profiles.first_name', $this->userFullName]); $query->andFilterWhere(['OR LIKE', 'profiles.last_name', $this->userFullName]);

The problem is in the query Like, i could use array as the values that attribute will be comapred but i cant use array as list of attributes to be compared with.

or

$subQuery1 = Profile::find()->Where(['LIKE', 'profiles.first_name', $this->userFullName]); $subQuery2 = Profile::find()->Where(['LIKE', 'profiles.last_name', $this->userFullName]); //i think its overloaded(3 queries insteadof 1 but still) and the final query $query->andFilterWhere([ 'OR', $subQuery1, $subQuery2 ]);

Any ideas how to build query whithout "%"?

解决方案

You should simply try :

$query->andFilterWhere([ 'or', ['like', 'profiles.first_name', $this->userFullName], ['like', 'profiles.last_name', $this->userFullName], ]);

or: similar to the and operator except that the operands are concatenated using OR. For example, ['or', ['type' => [7, 8, 9]], ['id' => [1, 2, 3]] will generate (type IN (7, 8, 9) OR (id IN (1, 2, 3))).

Read more : www.yiiframework/doc-2.0/yii-db-queryinterface.html#where()-detail

更多推荐

yii2 ActiveQuery 'OR LIKE' 过滤器

本文发布于:2023-11-15 05:53:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591724.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   ActiveQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!