public function parse() { $batch = 10; while (($data = $this->getLine()) !== false) { try { array_walk($data, function (&$value) { $value = trim($value); if (!strlen($value)) { $value = null; } }); if (($product = $this->isProductExist($data[3])) === false) { $product = new Entity\Product(); } $product->setCustomer($this->_customer); $product->setName($data[3]); $product->setTnved($data[5]); $product->setHscode($data[6]); $product->setDescription($data[7]); $product->setDescriptionInvoice($data[8]); $product->setExtraData($data[9]); $product->setNetWeight($data[10]); $product->setStatus($this->_status); $product->setCustomText1($data[13]); $this->_em->persist($product); if ($this->_count % $batch === 0) { echo memory_get_usage() / 1024, "\n"; $this->_em->flush(); $this->_em->clear($product); } $this->_count++; } catch (\Exception $ex) { $logger = $this->get('logger'); $logger->error($ex->getMessage()); } } $this->_em->flush(); $this->_em->clear(); }
public function updateAction() { if (!$this->checkAccess()) { return new JsonResponse(['fail' => 'Wrong API key']); } $em = $this->getDoctrine()->getManager(); $request = $this->container->get('request'); $productName = $request->get('name'); if (empty($productName)) { return new JsonResponse(['fail' => 'Product name must be defined']); } $productCustomerAbbr = $request->get('customer_abbr'); $customer = $em->getRepository('CoreBundle:Customer')->findOneBy(['abbr' => $productCustomerAbbr]); if (empty($customer)) { return new JsonResponse(['fail' => 'Customer not found']); } $product = $em->getRepository('CoreBundle:Product')->findBy(['customer' => $customer, 'name' => $productName]); if (empty($product)) { if (!$request->get('_force')) { return new JsonResponse(['fail' => 'Product does not exist. If you want to create new one use _force parameter.']); } $product = new Entity\Product(); } $product->setName($productName); $product->setDescription($request->get('description')); $product->setCustomer($customer); if ($request->get('status')) { $status = $em->getRepository('CoreBundle:ProductStatus')->findOneBy(['code' => $request->get('status')]); } if (empty($status)) { $status = $em->getRepository('CoreBundle:ProductStatus')->findOneBy(['code' => Entity\ProductStatus::TYPE_ON_CHECKING]); } $product->setStatus($status); if ($request->get('origin_countries')) { foreach ((array) $request->get('origin_countries') as $originCountyCode) { $country = $em->getRepository('CoreBundle:Country')->findOneBy(['code' => $originCountyCode]); } if (!empty($country)) { $product->addOriginCountry($country); } } if ($request->get('net_weight') && $request->get('net_weight_uom')) { $netWeightUom = $em->getRepository('CoreBundle:UOM')->findOneBy(['name' => $request->get('net_weight_uom')]); $netWeight = $request->get('net_weight'); if (!empty($netWeightUom) && !empty($netWeight)) { $product->setNetWeight($netWeight); $product->setNetWeightUOM($netWeightUOM); } } if ($request->get('gross_weight') && $request->get('gross_weight_uom')) { $grossWeightUom = $em->getRepository('CoreBundle:UOM')->findOneBy(['name' => $request->get('gross_weight_uom')]); $grossWeight = $request->get('gross_weight'); if (!empty($grossWeightUom) && !empty($grossWeight)) { $product->setGrossWeight($grossWeight); $product->setGrossWeightUOM($grossWeightUOM); } } if ($request->get('customer_description')) { $product->setCustomerDescription($request->get('customer_description')); } if ($request->get('price')) { $product->setPrice($price); } if ($request->get('currency')) { $currency = $em->getRepository('CoreBundle:Currency')->findOneBy(['codeAlpha3' => $request->get('currency')]); $product->setCurrency($currency); } for ($i = 1; $i <= 10; $i++) { if (!is_null($request->get('custom_date_' . $i))) { $setterMethod = 'setCustomDate' . $i; try { $date = new \DateTime($request->get('custom_date_' . $i)); $product->{$setterMethod}($date); } catch (\Exception $e) { // Skip error } } if (!is_null($request->get('custom_text_' . $i))) { $setterMethod = 'setCustomText' . $i; $product->{$setterMethod}($request->get('custom_text_' . $i)); } if (!is_null($request->get('custom_number_' . $i))) { $setterMethod = 'setCustomNumber' . $i; $product->{$setterMethod}($request->get('custom_number_' . $i)); } } if (!is_null($request->get('extra_data'))) { $product->setExtraData(json_encode($request->get('extra_data'))); } $customsPointDataArray = $request->get('customs_point_data'); if (!empty($customsPointDataRaw) && !empty($customsPointDataRaw['code'])) { $customsPoint = $em->getRepository('CoreBundle:CustomsPoint')->findOneBy(['code' => $customsPointDataRaw['code']]); if (!empty($customsPoint)) { $customsPointData = $product->getCustomsPointDataByCustomsPoint($customsPoint); if (empty($customsPointData)) { $customsPointData = new Entity\ProductCustomsPoint(); $customsPointData->setCustomsPoint($customsPoint); $customsPointData->setProduct($product); } if (isset($customsPointDataRaw['eccn'])) { $customsPointData->setEccn($customsPointDataRaw['eccn']); } if (isset($customsPointDataRaw['hscode'])) { $customsPointData->setEccn($customsPointDataRaw['hscode']); } if (isset($customsPointDataRaw['tnved'])) { $customsPointData->setTnved($customsPointDataRaw['tnved']); } if (isset($customsPointDataRaw['status'])) { $status = $em->getRepository('CoreBundle:ProductStatus')->findOneBy(['code' => $customsPointDataRaw['status']]); $customsPointData->setStatus($status); } if (isset($customsPointDataRaw['description_ccd'])) { $customsPointData->setDescriptionCCD($customsPointDataRaw['description_ccd']); } if (isset($customsPointDataRaw['manufacturer'])) { $customsPointData->setManufacturer($customsPointDataRaw['manufacturer']); } for ($i = 1; $i <= 10; $i++) { if ($i < 6) { if (!is_null($customsPointDataRaw['custom_date_' . $i])) { $setterMethod = 'setCustomDate' . $i; try { $date = new \DateTime($customsPointDataRaw['custom_date_' . $i]); $customsPointData->{$setterMethod}($date); } catch (\Exception $e) { // Skip error } } } if (!is_null($customsPointDataRaw['custom_text_' . $i])) { $setterMethod = 'setCustomText' . $i; $customsPointData->{$setterMethod}($customsPointDataRaw['custom_text_' . $i]); } if (!is_null($customsPointDataRaw['custom_number_' . $i])) { $setterMethod = 'setCustomNumber' . $i; $customsPointData->{$setterMethod}($customsPointDataRaw['custom_number_' . $i]); } } $em->persist($customsPointData); } } $em->persist($product); $em->flush(); return new JsonResponse(['success' => $this->exportProduct($product)]); }
private function createNewProduct($data) { $product = new Entity\Product(); $product->setCustomer($this->_customer); $product->setName($data[3]); $product->setTnved($data[5]); $product->setHscode($data[6]); $product->setDescription($data[7]); $product->setDescriptionInvoice($data[8]); $product->setExtraData($data[9]); $product->setNetWeight($data[10]); $product->setStatus($this->_status); $product->setCustomText1($data[13]); $this->_em->persist($product); return $product; }