ProductRepository::getListForCategory PHP Method

getListForCategory() public method

public getListForCategory ( StoreCategory $category, boolean $withChild = true, null $limit = null ) : CActiveDataProvider
$category StoreCategory
$withChild boolean
$limit null
return CActiveDataProvider
    public function getListForCategory(StoreCategory $category, $withChild = true, $limit = null)
    {
        $categories = [];
        if (true === $withChild) {
            $categories = $category->getChildsArray();
        }
        $categories[] = $category->id;
        $criteria = new CDbCriteria(['scopes' => ['published']]);
        $builder = new CDbCommandBuilder(Yii::app()->getDb()->getSchema());
        $criteria->addInCondition('t.category_id', array_unique($categories));
        $criteria->addCondition(sprintf('t.id IN (SELECT product_id FROM {{store_product_category}} WHERE %s)', $builder->createInCondition('{{store_product_category}}', 'category_id', $categories)), 'OR');
        $pagination = ['pageSize' => (int) Yii::app()->getModule('store')->itemsPerPage, 'pageVar' => 'page'];
        if ($limit) {
            $pagination = false;
            $criteria->limit = (int) $limit;
        }
        return new CActiveDataProvider(Product::model(), ['criteria' => $criteria, 'pagination' => $pagination, 'sort' => ['sortVar' => 'sort', 'defaultOrder' => 't.position']]);
    }

Usage Example

 /**
  * @return bool
  * @throws CException
  */
 public function run()
 {
     $category = $this->categoryRepository->getByAlias($this->slug);
     if (null === $category) {
         return false;
     }
     $this->render($this->view, ['category' => $category, 'products' => $this->productRepository->getListForCategory($category, $this->limit)]);
 }
All Usage Examples Of ProductRepository::getListForCategory