3

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?.

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

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++;
}
  • Thanks! I missed the clear(). – Milind Singh Jul 19 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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