如果我有14000条记录,则会出现内存空间问题,如果记录为1000,我可以上传但需要00:30分钟。
任何人都可以帮我优化这段代码,我可以在最短的时间内上传记录吗?
这是我的代码:
<?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;
}
}
}
答案 0 :(得分:0)
最有效的方式是仅使用Doctrine DBAL并编写SQL代码。
Documentation指出Doctrine ORM不适合大量插入。
有“hacks”,例如禁用SQLLogger和批处理,但只使用DBAL可以节省更多时间。