SearchController.php
1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
    
    namespace frontend\controllers;
    
    use common\modules\product\models\Product;
    use yii\data\ActiveDataProvider;
    use yii\web\Controller;
    
    /**
     * Search controller
     */
    class SearchController extends Controller
    {
        
        /**
         * Displays search result.
         * @return string
         */
        public function actionIndex()
        {
            $search = \Yii::$app->request->get('search');
            if(empty( $search )) {
                return $this->redirect([ 'site/index' ]);
            }
            $query = Product::find()
                            ->joinWith('lang', true, 'INNER JOIN')
                            ->joinWith('variants.lang', true, 'INNER JOIN')
                            ->joinWith('brand.lang', true, 'INNER JOIN')
                            ->where([
                                'ilike',
                                'product_lang.name',
                                $search,
                            ])
                            ->orWhere([
                                'ilike',
                                'brand_lang.name',
                                $search,
                            ])
                            ->orWhere([
                                'ilike',
                                'product_variant_lang.name',
                                $search,
                            ]);
            $dataProvider = new ActiveDataProvider([
                'query'      => $query,
                'pagination' => [
                    'pageSize' => 20,
                ],
            ]);
            
            return $this->render('index', [
                'dataProvider' => $dataProvider,
                'search'       => $search,
            ]);
        }
    }