1

i am using magento 2.1.5, i created one module to export all order detail in csv.

but i am getting this issue:

Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 16384 bytes) in /var/www/html/XXXXX/public_html/XXXXX/vendor/magento/zendframework1/library/Zend/Db/Statement/Pdo.php on line 228

Code for export sales Data:

function execute()
{


    if (!file_exists($this->_directoryList->getRoot().'/pub/media/OrderExport')) {
        mkdir($this->_directoryList->getRoot().'/pub/media/OrderExport', 0777, true);
    }

    $todayDate = date('Y_m_d_H_i_s', time());
    $fileName = $this->_directoryList->getRoot().'/pub/media/OrderExport/OrderExport'.$todayDate.'.csv';


    $fp = fopen($fileName, 'w');
    $this->writeHeadRow($fp);
    $collection = $this->collectionFactory->create()->addFieldToFilter('store_id', 6);//->getCollection();
    $countOrderExport = 0;
    //$j = 0;
    foreach ($collection->getItems() as $_order) {
        //if($j++ > 5000)

            //break;
      $orderId = $_order->getId();
      if ($orderId) {
        $order = $this->orderModel->load($_order->getId());
        $this->writeOrder($order, $fp);
        $incId = $order->getIncrementId();
        $countOrderExport++;
      }
    }
    fclose($fp);

    $this->downloadCsv($fileName);
    $this->messageManager->addSuccess(__('We Exported %1 order(s).', $countOrderExport));

    //$resultRedirect = $this->resultRedirectFactory->create();
    //$resultRedirect->setPath('sales/*/');
    //return $resultRedirect;
}

i tried with increse mamory limit, maximum execution time, but still i am not able to export full report.

How to fix this issue?

  • I guess the reason is somewhere in your custom code. It might be worth putting some code into the question. Depending on how many orders you have maybe you should split the whole collection into smaller bunches get them one by one. – HelgeB Mar 14 at 16:34
  • @HelgeB Thanks for reply, please check my updated question. i updated with code which i am using. i am trying to export all order, it might be 20k-50k? please tell me how to achieve this. – sam Mar 15 at 5:51
  • From your code it's not clear what data you really need for your csv file and what $this->writeOrder() does. I would suggest first to try to limit the collection into bunches of 1000 orders. It may also be possible to load all needed data with the collection and not each order separately - but that depends on what data you are using. If you limit the collection to 1000 items or even less and you still have the problem you have to look at writeOrder function. – HelgeB Mar 15 at 8:50
  • @HelgeB I already did this, i put limit till 6000 data. and its exported successfully. after that it shows memory limit issue. – sam Mar 15 at 9:49
1

If your code is working for smaller sizes of the collection than I would suggest to split the collection. I have modified your code to demonstrate how that could work - in my example a value of 1000 is used for the size of the collection loaded in an iteration. You can change that value to something that works for you.

function execute()
{


    if (!file_exists($this->_directoryList->getRoot().'/pub/media/OrderExport')) {
        mkdir($this->_directoryList->getRoot().'/pub/media/OrderExport', 0777, true);
    }

    $todayDate = date('Y_m_d_H_i_s', time());
    $fileName = $this->_directoryList->getRoot().'/pub/media/OrderExport/OrderExport'.$todayDate.'.csv';


    $fp = fopen($fileName, 'w');
    $this->writeHeadRow($fp);
    $countOrderExport = 0;

    $collection = $this->collectionFactory->create()->addFieldToFilter('store_id', 6);//->getCollection();

    $collection->setPageSize(1000); //set the bunchsize to something that works for you
    $pages = $collection->getLastPageNumber();

    for ($currentPage = 1;$currentPage <= $pages;$currentPage++){
        $collection->setCurPage($currentPage);
        $collection->load();
        foreach ($collection->getItems() as $_order) {

            $orderId = $_order->getId();
            if ($orderId) {
              $order = $this->orderModel->load($_order->getId());
              $this->writeOrder($order, $fp);
              $incId = $order->getIncrementId();
              $countOrderExport++;
            }
        }
        $collection->clear();
    }
    fclose($fp);

    $this->downloadCsv($fileName);
    $this->messageManager->addSuccess(__('We Exported %1 order(s).', $countOrderExport));

    //$resultRedirect = $this->resultRedirectFactory->create();
    //$resultRedirect->setPath('sales/*/');
    //return $resultRedirect;
}

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.