Ejemplo n.º 1
0
 /**
  * @param $term
  * @return string JSON
  */
 public function actionAutoCompleteSearch($term)
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     $search = new Search();
     $search->q = $term;
     $search->on(Search::QUERY_SEARCH_PRODUCTS_BY_DESCRIPTION, function (SearchEvent $event) {
         $event->setFunctionSearch(function ($activeQuery) {
             $activeQuery->limit(Yii::$app->getModule('core')->autoCompleteResultsCount);
             return Product::find()->select(['id', 'name', 'main_category_id', 'slug', 'sku'])->where(['id' => $activeQuery->all()])->all();
         });
     });
     $products = $search->searchProductsByDescription();
     $result = [];
     foreach ($products as $product) {
         /** @var Product $product */
         $result[] = ['id' => $product->id, 'name' => $product->name, 'url' => Url::toRoute(['@product', 'model' => $product, 'category_group_id' => $product->getMainCategory()->category_group_id], true)];
     }
     return $result;
 }