/** * Retrieves a list of models based on the current search/filter conditions. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. */ public function searchFrontend($criteria = false, $category = false) { if (!$criteria) { $criteria = new SDbCriteria(); } $criteria->compare('active', self::ACTIVE); if ($this->searchNameIndex) { $criteria->compare('name_index', $this->searchNameIndex, true); } else { $criteria->compare('id_brand', $this->id_brand); $criteria->group = 't.id'; if ($this->price) { list($priceFrom, $priceTo) = explode(',', $this->price); $criteria->addCondition("price >= :priceFrom AND price <= :priceTo"); $criteria->params += [':priceFrom' => $priceFrom, ':priceTo' => $priceTo]; } $criteria->together = true; } if ($category) { $criteria->compare('t.id_category', ProductCategory::descendantsIds($category->id, true)); } if (!$this->searchNameIndex) { //Для получения минимальной и максимальной цены продукта, чтобы проставить в фильтре цены $builder = new CDbCommandBuilder(Yii::app()->db->getSchema()); $minMaxPriceCriteria = clone $criteria; $minMaxPriceCriteria->select = "t.price"; $command = $builder->createFindCommand(Product::model()->tableName(), $minMaxPriceCriteria); $result = $command->queryColumn(); if ($result) { $this->max_price = max($result); } if ($this->productOptions) { $criteria->with[] = 'options'; $criteria->compare('options.id_variation', $this->productOptions); } } $pageSize = Cookie::get('product-list-view') == 'thumbnail' ? self::PAGESIZE_THUMBNAIL : self::PAGESIZE_LIST; return new SActiveDataProvider($this, array('criteria' => $criteria, 'pagination' => array('pageSize' => $pageSize, 'pageVar' => 'page'), 'sort' => ['attributes' => ['price', 'name', 'viewed']])); }