public function uploadAction(Request $request) { $form = $this->createForm(new Pud\Upload()); $form->handleRequest($request); if ($request->isMethod("POST")) { if ($form->isValid()) { try { foreach ($request->files as $uploadedFile) { $file = $uploadedFile['file']; $filename = $file->getClientOriginalName(); $file->move(QA\TDQueueAbstract::getUploadReportDir(), $filename); break; } $filepath = QA\TDQueueAbstract::getUploadReportDir() . '/' . $filename; $this->objExcel = \PHPExcel_IOFactory::load($filepath); $this->sheet = $this->objExcel->getActiveSheet(); $pudRepository = $this->getDoctrine()->getRepository('AppTruckingBundle:Pud'); $em = $this->getDoctrine()->getManager(); //массив ошибок, если они есть $error = null; foreach ($this->sheet->getRowIterator() as $row) { $rowIndex = $row->getRowIndex(); if ($rowIndex < $this->startRows) { continue; } $row = null; $result = $this->isImportRowCorrect($rowIndex); $row = $result['RESULT']; //проверка нужна на случай если есть незаполненые строки в импортируемом файле. if (!count($row)) { continue; } //если есть ошибки, прерываем загрузку if ($result['SUCCESS'] == false) { $error = $row; break; } $pud = new Entity\Pud(); $pud->setCustomerId($row['CUSTOMER']); $pud->setPackingId($row['PACKAGE_TYPE']); $pud->setCargoType($row['TRUCKING_CARGO_TYPE']); $pud->setCargoDescription($this->getCellValue('D', $rowIndex)); $pud->setLength($this->getCellValue('E', $rowIndex)); $pud->setWidth($this->getCellValue('F', $rowIndex)); $pud->setHeight($this->getCellValue('G', $rowIndex)); $volume = isset($row['TOTAL_VOLUME']) ? $row['TOTAL_VOLUME'] : $this->getCellValue('H', $rowIndex); $pud->setVolume($volume); $quantity = isset($row['ITEM_QUANTITY']) ? $row['ITEM_QUANTITY'] : $this->getCellValue('I', $rowIndex); $pud->setCountPlaces($quantity); $pud->setSummaryWeight($row['TOTAL_WEIGHT']); $pud->setStackFlag($row['STACKABILITY']); $pud->setShipper($row['SHIPPER']); $pud->setShipperAddress($row['SHIPPER_ADDRESS']); $pud->setLoadTimeSlot($this->renderDateString($row['LOADING_TIME_SLOT'])); $pud->setConsignee($row['CONSIGNEE']); $pud->setConsigneeAddress($row['CONSIGNEE_ADDRESS']); $pud->setUnloadTimeSlot($this->renderDateString($row['UNLOADING_TIME_SLOT'])); $pud->setSecurityFlag($row['SECURITY']); $pud->setSecurityType($row['SECURITY_TYPE']); $pud->setComments($this->getCellValue('T', $rowIndex)); $pud->setStatus($this->getStatus('Uploaded')); $pud->setUserId($this->getUser()); $em->persist($pud); $em->flush(); $message = "The file name is {$filename} was upload successfully"; } } catch (\Exception $ex) { $message = 'Error: ' . $ex->getMessage() . '. Line: ' . $ex->getLine() . ' File: ' . $ex->getFile(); $logger = $this->get('logger'); $logger->error($message); $error[] = $message; } } } return $this->render('AppTruckingBundle:Pud:upload.html.twig', ['form' => isset($form) ? $form->createView() : null, 'message' => isset($message) ? $message : null, 'error' => isset($error) ? $error : null]); }