search
HomeBackend DevelopmentPHP TutorialHow to achieve simultaneous search of multiple fields in Yii2
How to achieve simultaneous search of multiple fields in Yii2Jun 15, 2018 am 10:52 AM
yii2multiple fieldssearch

This article mainly introduces the method of Yii2 to realize simultaneous search of multiple fields. It analyzes the functions and specific usage methods used in Yii2 to search multiple fields at the same time in the form of examples. Friends in need can refer to the following

The example in this article describes how Yii2 implements simultaneous search of multiple fields. Share it with everyone for your reference, the details are as follows:

The search field in Yii2 uses the andFilterWhere method, which can be used to search for a paragraph.

If you are searching for multiple fields, for example, searching whether the article title and article content contain the keywords you need to search for, because the relationship between them is or, so you need to use orFilterWhereThis method

The following is all the code

public function actionIndex()
{
  $key =Yii::$app->request->post("key");
  $query = Post::find()->joinWith('cate');
  $post = $query->orderBy(['post.id' => SORT_DESC])->asArray()->where(['post.status' => 1]);
  if($key){
    $post->andFilterWhere(['like', 'post.title', $key])
      ->orFilterWhere(['like', 'post.content', $key]);
  }
  $pages = new Pagination([
    'totalCount' => $post->count(),
    'defaultPageSize' => 10
  ]);
  $model = $post->offset($pages->offset)->limit($pages->limit)->all();
  return $this->render('index', [
    'model' => $model,
    'pages' => $pages,
  ]);
}

You can see the sql statement as follows:

select count(*) from `post` left join `category` on `post`.`cate_id`=`category`.`id` where ((`post`.`status`=1) and (`post`.`title` like '%key%')) or (`post`.`content` like '%key%') order by `post`.`id` desc
select `post`.* from `post` left join `category` on `post`.`cate_id`=`category`.`id` where ((`post`.`status`=1) and (`post`.`title` like '%key%')) or (`post`.`content` like '%key%') order by `post`.`id` desc limit 10

The above is the entire content of this article, I hope it will be helpful to everyone's study , please pay attention to the PHP Chinese website for more related content!

Related recommendations:

How to implement Command task processing through Yii

Use Yii2 rbac permission control menu menu

The above is the detailed content of How to achieve simultaneous search of multiple fields in Yii2. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何利用PHP函数进行搜索和过滤数据?如何利用PHP函数进行搜索和过滤数据?Jul 24, 2023 am 08:01 AM

如何利用PHP函数进行搜索和过滤数据?在使用PHP进行开发的过程中,经常需要对数据进行搜索和过滤。PHP提供了丰富的函数和方法来帮助我们实现这些操作。本文将介绍一些常用的PHP函数和技巧,帮助你高效地进行数据的搜索和过滤。字符串搜索PHP中常用的字符串搜索函数是strpos()和strstr()。strpos()用于查找字符串中某个子串的位置,如果存在,则返

Laravel开发:如何使用Laravel Scout实现全文搜索?Laravel开发:如何使用Laravel Scout实现全文搜索?Jun 14, 2023 am 10:14 AM

Laravel开发:如何使用LaravelScout实现全文搜索?LaravelScout是一个Laravel的全文搜索解决方案,它是一个流行的开源软件,它可以让开发者轻松地实现高效的全文搜索功能。在这篇文章中,我们将介绍如何使用LaravelScout来实现全文搜索功能。安装LaravelScout首先,我们需要安装LaravelScout。可以

PHP如何对接淘宝商品搜索API文档PHP如何对接淘宝商品搜索API文档Jul 01, 2023 pm 10:16 PM

PHP如何对接淘宝商品搜索API文档淘宝是中国最大的电子商务平台之一,拥有庞大的商品库存和用户群体。对于开发者来说,通过对接淘宝的API接口,可以获取商品信息、推广活动以及进行交易等功能,从而实现个性化的商业应用。本文将介绍如何使用PHP语言对接淘宝商品搜索API,帮助开发者快速构建自己的电商应用。第一步:注册成为淘宝开发者在开始之前,需要先注册成为淘宝开发

yii2 怎么去掉jqueryyii2 怎么去掉jqueryFeb 17, 2023 am 09:55 AM

yii2去掉jquery的方法:1、编辑AppAsset.php文件,注释掉变量$depends里的“yii\web\YiiAsset”值;2、编辑main.php文件,在字段“components”下面添加配置为“'yii\web\JqueryAsset' => ['js' => [],'sourcePath' => null,],”即可去掉jquery脚本。

如何使用PHP ZipArchive实现对压缩包的文件过滤和搜索?如何使用PHP ZipArchive实现对压缩包的文件过滤和搜索?Jul 23, 2023 pm 08:34 PM

如何使用PHPZipArchive实现对压缩包的文件过滤和搜索?概述在Web开发中,我们经常需要对压缩包文件进行处理,包括过滤和搜索。PHP提供了ZipArchive扩展,它使我们能够轻松地对压缩包进行操作。本文将教您如何使用PHPZipArchive扩展来实现对压缩包文件的过滤和搜索功能。步骤首先,确保您的PHP环境已启用ZipArchive扩展。您可

如何在uniapp中实现关键字搜索如何在uniapp中实现关键字搜索Jul 05, 2023 am 08:53 AM

如何在uniapp中实现关键字搜索在当前信息爆炸的时代,搜索已经成为我们获取所需信息的重要方法之一。在移动端应用开发中,如何在uniapp中实现关键字搜索,提供用户便捷的搜索功能,是一个非常重要的技术挑战。本文将介绍在uniapp中实现关键字搜索的方法,并提供代码示例供参考。一、创建搜索框组件首先,我们需要在uniapp中创建一个搜索框组件,用于用户输入关键

UniApp实现搜索功能的配置与实现技巧UniApp实现搜索功能的配置与实现技巧Jul 04, 2023 pm 05:15 PM

UniApp实现搜索功能的配置与实现技巧随着移动互联网的迅速发展,搜索功能已经成为了几乎每一个应用都必备的功能之一。而对于基于Vue.js的多平台应用开发框架UniApp来说,实现搜索功能也变得更加简单和高效。本文将介绍UniApp中搜索功能的配置与实现技巧,并且附带代码示例,帮助读者快速上手。一、配置搜索功能在uni-app项目的页面文件夹中创建一个搜索页

精选几道CTF习题,带你学习yii2框架!精选几道CTF习题,带你学习yii2框架!Feb 23, 2022 am 10:33 AM

本篇文章带大家了解yii2框架,分享几道CTF习题,通过它们来学习yii2框架,希望对大家有所帮助。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft