Answer the question
In order to leave comments, you need to log in
How to organize a relational query with pagination in yii?
There is a table Categories, Products and CategoryProduct. (one product can belong to several categories).
On the category page, you need to display all the products of the category and a pagination of 20 pcs.
I do so. I take a category with all products
$category = Category::model()->with('products')->findByAttributes(array('url' => $alias));
'productCategory' => array(self::HAS_MANY, 'ProductCategory', 'category_id'),
'products'=> array(self::HAS_MANY, 'Product', 'product_id', 'through' =>'productCategory'),
Answer the question
In order to leave comments, you need to log in
You can pull getRelated for the category by specifying the CDbCriteria object as the third parameter, which in turn specifies the limit and offset required for pagination ( www.yiiframework.com/doc/api/1.1/CActiveRecord#get... )
$category = Category::model()->with('products')->findByAttributes(array('url' => $alias));
$criteria = new CDbCriteria();
$criteria->limit = 20;
$criteria->offset = 0;
$products = $category->getRelated('products', false, $criteria); //получите первые 20 продуктов категории
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question