V
V
Victor Umansky2017-04-24 00:10:21
Yii
Victor Umansky, 2017-04-24 00:10:21

Yii2 how to properly organize site search?

I need to organize a search on the site as in the pictures, there are two tables "profile and profile_has_category".
510b3a7a6b.png
1 - The first table is stored in the form of id, region and city of the specialist, in the second table the list of selected categories is stored by the user, the id is also stored there and the title is also done, how can I correctly implement the search for specialists. Let's say the user selects auto services and the city of Moscow, how to check and compare (so to speak) and display all the specialists in the city of Moscow.
2 - how to make a correct request for the city and region, an example
I drive the city autocomplete picks it up and pulls the area separated by commas
I took the widget from the kartik\typeahead\Typeahead.
Search for experts in the category controller

public function actionCategoryList($q = null)
{
$query = new Query();

$query->select('title')
    ->from('profile_has_category')
    ->where('title LIKE "%' . $q . '%"')
    ->orderBy('title');
$command = $query->createCommand();
$data = $command->queryAll();
$out = [];
foreach ($data as $d) {
    $out[] = ['value' => $d['title']];
}
echo Json::encode($out);
}

view
Typeahead::widget([
                'name' => 'Search[category]',
                'options' => ['placeholder' => 'Искать специальности ...'],
                'pluginOptions' => ['highlight' => true],
                'class' => 'search-input-spec',
                'dataset' => [
                    [
                        'datumTokenizer' => "Bloodhound.tokenizers.obj.whitespace('value')",
                        'display' => 'value',
                        //'prefetch' => $asset->baseUrl . '/samples/countries.json',
                        'remote' => [
                            'url' => Url::to(['search/category-list']) . '?q=%QUERY',
                            'wildcard' => '%QUERY'
                        ]
                    ]
                ]
            ]);

DB - profile
id        | 1
----------| -
----------| -
----------| -
region_id | 12
city_id   | 465

DB - profile_has_category
id          | 1
category_id | 1070
title       | Электрик
price       | 100

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Timofeev, 2017-04-24
@webinar

Make a join. Something like this:

$result = User::find()
->joinWith('profile_has_category')
->joinWith('profile_has_regioncity')
->andWhere(['profile_has_category.id'=>'category_id'])
->andWhere(['profile_has_regioncity.id'=>'city_id'])
->all();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question