P
P
Pavel Gogolinsky2014-10-21 16:06:06
Yii
Pavel Gogolinsky, 2014-10-21 16:06:06

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));

Here is the dependency in the model
'productCategory' => array(self::HAS_MANY, 'ProductCategory', 'category_id'),
'products'=> array(self::HAS_MANY, 'Product', 'product_id', 'through' =>'productCategory'),

And then the stopper. How to break them into portions of 20 pieces?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vit, 2014-10-21
@gogolinsky

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 question

Ask a Question

731 491 924 answers to any question