0

I working on a command line which is fetching all the products in the database, using the Magento\Catalog\Model\ResourceModel\Product\CollectionFactory object.

In order to avoid memory problems, I'm going to process the data in chunks, so using the methods setPageSize and setCurPage to fetch just a portion of the whole dataset and walking through it.

I notice, perhaps the setCurPage value passed was increasing, the data returned are the same all the times. Is there any method I need to call in order to clear the Collection cache if any? Or is there a better way to walk and process the data from the collection?

use Symfony\Component\Console\Command\Command;                                        
use Symfony\Component\Console\Input\InputInterface;               
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;                               

use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory; 
use Magento\Framework\App\State;                     
use Magento\Framework\App\Area;                                                                                                               

class ExportProducts extends Command                 
{                                                  
    public function __construct(                         
        CollectionFactory $collectionFactory,                                      
        State $state                                                          
    ) {                                                
        $this->collectionFactory = $collectionFactory;
        $this->state = $state;

        parent::__construct();                                   
    }                                  

    protected function configure()             
    {  
         // Command description
    } 

    protected function execute(InputInterface $input, OutputInterface $output)
    {                    
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $this->state->setAreaCode(Area::AREA_GLOBAL);

        $productCollection = $this->collectionFactory->create();
        $productsQty = $productCollection->getSize();  

        $pageSize = 25;                                      
        $break = false;                              
        $page = 1;                                                                       
        $count = null;
        $lPage = null;   

        while ($break !== true) {                                               
            $productCollection                   
                ->addAttributeToSelect('*')          
                ->setOrder('entity_id','ASC')      
                ->setPageSize($pageSize)                 
                ->setCurPage($page)                                                
                ->load();                                                     

            $output->writeln($page);                   

            if($count === null) {
                $count = $productCollection->getSize();   
                $lPage = $productCollection->getLastPageNumber();
            }

            if($lPage == $page) {                              
                $break = true;                               
            }                  

            $page++;    

            foreach ($productCollection as $product) {
                $id = $product->getData('entity_id');
                $pr = $objectManager->create('Magento\Catalog\Model\Product')->load($id);
                /** Here I'm working with the data coming from $product and $pr **/
            }
        }
    }
2
  • What you need from $pr object? – Keyur Shah Jan 17 '19 at 10:58
  • $product->getData('quantity_and_stock_status'); returns a string with written 1, instead of the quantity of the stock and it it's in stock or not. So, I'm accessing that data via $pr. But I'm open to any suggestion. – Luigi T. Jan 17 '19 at 11:18
0

move $productCollection = $this->collectionFactory->create(); inside while loop

update your while code like this

while ($break !== true) {
            $productCollection = $this->collectionFactory->create();
            $productCollection
                ->addAttributeToSelect('*')
                ->setOrder('entity_id','ASC')
                ->setPageSize($pageSize)
                ->setCurPage($page)
                ->load();


            if($count === null) {
                $count = $productCollection->getSize();
                $lPage = $productCollection->getLastPageNumber();
            }

            if($lPage == $page) {
                $break = true;
            }

            $page++;

            foreach ($productCollection as $product) {
                $id = $product->getData('entity_id');
                /** Here I'm working with the data coming from $product and $pr **/
            }
        }

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.