Answer the question
In order to leave comments, you need to log in
How to implement search by site sections in yii2?
I ask in advance ... do not throw tomatoes in the comments (
I want to do a search on the site, taste how it's done, so to speak. There are 10 sections on the site, I want to do a search for everything directly. only in one category and not looking in others
Here is the code,
class SearchController extends Controller {
public function actionSearch() {
$q = Yii::$app->request->get('q');
$query = Extrime::find()->where(['like', 'title', $q]);
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 2]);
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render('search',compact('post','models','q','cat','friend'));
}
}
Answer the question
In order to leave comments, you need to log in
You need some separate storage (a table in the database) that will contain the following data:
1. entity type (essentially the name of the corresponding table or model)
2. entity id
3. text that will be searched for
Entity type and id fields are needed to link the found elements with real pages of your site.
Such a place for searching can be either a separate plate in the database or a separate database of the Elasticsearch type.
You will need to fill this table once and then update it every time you change the data on which you want to search.
it is not necessary to fence a bicycle and to do search by means of a DB project (as far as I understand it at you relational). Take a better ready-made solution such as Sphinx or ElasticSearch
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question