I
I
Ivan Lykov2017-10-13 11:20:02
Yii
Ivan Lykov, 2017-10-13 11:20:02

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'));
        
    }
    
}

How can I transform, or rather, how can I add more search terms? If you add right now as is, then it will overwrite the variable. How can I add more search terms for other sections?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
matperez, 2017-10-13
@Jhon_Light

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.

M
Maxim Fedorov, 2017-10-13
@qonand

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 question

Ask a Question

731 491 924 answers to any question