Example #1
0
 public function actionSearch()
 {
     Yii::$app->response->format = Response::FORMAT_JSON;
     $productDto = new ProductDto();
     $productDto->setScenario('user-query');
     $ds = $this->productService->query($productDto, ['pageParam' => 'pageCurrent', 'pageSizeParam' => 'pageSize', 'defaultPageSize' => $this->pageParams['pageSize'], 'route' => Yii::$app->requestedRoute, 'params' => $this->pageParams], [$this->sortParams['orderField'] => $this->sortParams['orderDirection']]);
     return $ds;
 }
Example #2
0
 public function query(ProductDto $productDto, array $pageParams, array $sortParams)
 {
     $query = Product::find()->with('category')->with('brand');
     //todo 增加查询条件
     $pagination = new Pagination(array_merge($pageParams, ['totalCount' => $query->count()]));
     $sort = new Sort(['defaultOrder' => $sortParams]);
     $provider = new ActiveDataProvider(['query' => $query, 'pagination' => $pagination, 'sort' => $sort]);
     $models = $provider->getModels();
     $retDtos = [];
     foreach ($models as $model) {
         $productDto->load(['ProductDto' => ArrayHelper::toArray($model)]);
         $category = $model->category;
         if ($category) {
             $productDto->category_name = $category->name;
         }
         $brand = $model->brand;
         if ($brand) {
             $productDto->brand_name = $brand->name;
         }
         $retDtos[] = ArrayHelper::toArray($productDto, ['common\\dtos\\ProductDto' => $productDto->activeAttributes()]);
     }
     return ['models' => $retDtos, 'pagination' => $pagination];
 }