4

I am using \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory and loading data by creating objects.

$collection = $this->collectionFactory->create()
                    ->addAttributeToSelect('*')
                    ->addAttributeToFilter('type_id',  array('simple', 'configurable'))
                    ->setPageSize($limit)
                    ->setCurPage(1);
$result = $collection->getData();

The $result contains the first-page result. Can I use the same object to render the second-page result?.

2
  • change setCurPage(1) to setCurPage(2) and check what results you are getting..
    – aravind
    Jul 19, 2019 at 12:06
  • It returns the previous data. Jul 19, 2019 at 12:08

1 Answer 1

4

The collection data can be reset by using clear function in $collection and then changing the page will provide next page data.

The following code might be helpful.

 $limit = 10;
 $collection = $this->collectionFactory->create()
     ->addAttributeToSelect('*')
     ->addAttributeToFilter('type_id',  array('simple', 'configurable'))
     ->setPageSize($limit)
     ->setCurPage(1);
 $pages = $collection->getLastPageNumber();
 $page = 1;
 while ($page <= $pages) {
     $collection->clear();
     $collection->setCurPage($page);
     $result = $collection->getData();
     $page++;
}
1
  • Thanks! I missed the clear(). Jul 19, 2019 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.