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 the method orFilterWhere
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:
Readers who are interested in more Yii-related content can check out the special topics of this site: "Introduction to Yii Framework and Summary of Common Techniques", "Summary of Excellent Development Framework of PHP", "Basic Tutorial for Getting Started with Smarty Templates", "Introduction to PHP Object-Oriented Programming" Tutorial", "php string (string) usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will help you design PHP programs based on the Yii framework.