If I have 14000 records then there is memory space issue coming and if records are 1000 I am able to upload but takes 00:30 mints .
Can anyone help me to optimize this code that I can upload records in minimum time?
Here is my code:
<?php
namespace ProjectName\AdminBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
class TakeoverController extends Controller {
public function uploadtakeoverAction(Request $request) {
ini_set('max_execution_time', 0); //0=NOLIMIT
ini_set('memory_limit', '8192M');
//start - code to check ip address
$checkipaddress = $this->get('userlogin')->CheckIPAddress();
if (empty($checkipaddress)) {
return $this->redirect($this->generateUrl('admin'));
}
//end - code to check ip address
$em = $this->getDoctrine()->getManager();
$helper = $this->get('common_helper_function');
$reqImage = $_FILES;
$validExtensions = array('xls', 'xlsx');
if (isset($_FILES) && !empty($_FILES)) {
$ext = pathinfo($_FILES['file']["name"], PATHINFO_EXTENSION);
if (!in_array($ext, $validExtensions)) {
echo "1";
die;
}
}
$admin = $this->get('security.context')->getToken()->getUser();
$takeoverfile = $this->uploadtakeoverFile($reqImage);
$takeovertm = array();
$insertedrecord = array();
$existingrecord = array();
$importmulitpletakeover = array();
$atmservicesamount = array();
$errormessage = '';
$neeerro = '';
if ($takeoverfile) {
$phpExcelObject = $this->get('phpexcel')->createPHPExcelObject();
$file = $takeoverfile;
if (!file_exists($file)) {
exit("Please run 05featuredemo.php first.");
}
$objPHPExcel = \PHPExcel_IOFactory::load($file);
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
$i = 0;
foreach ($worksheet->getRowIterator() as $row) {
$cellIterator = $row->getCellIterator();
$cellIterator->setIterateOnlyExistingCells(false); // Loop all cells, even if it is not set
$totalindex = $row->getRowIndex();
// echo $row->getRowIndex();
if ($row->getRowIndex() != 1) { // first index is title index so not cosider
foreach ($cellIterator as $cell) {
if (!is_null($cell)) {
//the below columns are from A to AF
if ($cell->getColumn() == 'A') {
$val1= trim($cell->getValue());
}
if ($cell->getColumn() == 'B') {
$val2= $cell->getValue();
}
if ($cell->getColumn() == 'C') {
$val3= trim($cell->getValue());
}
if ($cell->getColumn() == 'D') {
$val3.=', ' . trim($cell->getValue());
}
if ($cell->getColumn() == 'E') {
$val3.=', ' . trim($cell->getValue());
}
}
// insert this fields into database from here of column A to AF
}
unlink($takeoverfile);
$response = new Response(json_encode($importmulitpletakeover));
return $response;
} else {
$message = 'Image has not been uploaded ';
echo "2";
die;
}
}
}}}
protected function uploadtakeoverFile($uploadedReceipt) {
//start - code to check ip address
$checkipaddress = $this->get('userlogin')->CheckIPAddress();
if (empty($checkipaddress)) {
return $this->redirect($this->generateUrl('admin'));
}
//end - code to check ip address
$basicPath = $_SERVER['DOCUMENT_ROOT'] . '/ProjectName/web/uploads';
$basicReceiptPath = $basicPath . '/FolderName';
if (null === $uploadedReceipt) {
return;
} else {
$extention = explode('.', @$uploadedReceipt['file']["name"]);
$filename = @$extention[0] . '_' . time() . '.' . @$extention[1];
$this->path = $basicReceiptPath . '/' . @$filename;
if (move_uploaded_file($uploadedReceipt['file']["tmp_name"], $this->path))
return $this->path;
else
return false;
}
}
}
The most effective style will be to only use Doctrine DBAL and write SQL code.
Documentation states that Doctrine ORM is not fit for mass inserts.
There are "hacks" like disabling SQLLogger and batch processing, but using only DBAL will save you more time.