/** * Update product export date for the given product * @param string $identifier * @param JobInstance $jobInstance */ public function updateProductExport($identifier, JobInstance $jobInstance) { $now = new \DateTime('now', new \DateTimeZone('UTC')); $product = $this->productRepository->findByReference((string) $identifier); if (class_exists('\\PimEnterprise\\Bundle\\WorkflowBundle\\Model\\PublishedProduct')) { if ($product instanceof \PimEnterprise\Bundle\WorkflowBundle\Model\PublishedProduct) { /**@var \PimEnterprise\Bundle\WorkflowBundle\Model\PublishedProduct $product **/ $productId = $product->getOriginalProduct()->getId(); $product = $product->getOriginalProduct(); } } if (null != $product) { $productExport = $this->productExportRepository->findOneBy(array('product' => $product, 'jobInstance' => $jobInstance)); $conn = $this->entityManager->getConnection(); $jobInstance->getId(); $product->getId(); if (null === $productExport) { $sql = ' INSERT INTO pim_delta_product_export (product_id, job_instance_id, date) VALUES (:product_id, :job_instance_id, :date) '; } else { $sql = ' UPDATE pim_delta_product_export SET date = :date WHERE product_id = :product_id AND job_instance_id = :job_instance_id '; } $q = $conn->prepare($sql); $date = $now->format('Y-m-d H:i:s'); $productId = $product->getId(); $jobInstanceId = $jobInstance->getId(); $q->bindParam(':date', $date, PDO::PARAM_STR); $q->bindParam(':product_id', $productId, PDO::PARAM_INT); $q->bindParam(':job_instance_id', $jobInstanceId, PDO::PARAM_INT); $q->execute(); } }
/** * {@inheritdoc} */ public function writeItem(array $item) { $this->counter++; $entity = null; // If the table was not truncated to begin with, find current entities // first if (false === $this->truncate) { if ($this->index) { // If the table has a composite key if (!empty($this->compositeKey) && is_array($this->compositeKey)) { $composite = ''; foreach ($this->compositeKey as $key => $index) { $composite .= $item[$index]; } $value = $composite; } else { $value = $item[$this->index]; } $entity = $this->entityRepository->findOneBy(array($this->index => $value)); } else { $entity = $this->entityRepository->find(current($item)); } } if (!$entity) { $entity = $this->getNewInstance(); } $fieldNames = array_merge($this->entityMetadata->getFieldNames(), $this->entityMetadata->getAssociationNames()); foreach ($fieldNames as $fieldName) { $value = null; if (isset($item[$fieldName])) { $value = $item[$fieldName]; } elseif (method_exists($item, 'get' . ucfirst($fieldName))) { $value = $item->{'get' . ucfirst($fieldName)}; } if (null === $value) { continue; } if (!$value instanceof \DateTime || $value != $this->entityMetadata->getFieldValue($entity, $fieldName)) { $setter = 'set' . ucfirst($fieldName); $this->setValue($entity, $value, $setter); } } $this->entityManager->persist($entity); if ($this->counter % $this->batchSize == 0) { $this->entityManager->flush(); $this->entityManager->clear($this->entityName); } return $this; }
/** * Finds existing entity or create a new instance */ protected function findOrCreateItem(array $item) { $entity = null; // If the table was not truncated to begin with, find current entity // first if (false === $this->truncate) { if ($this->lookupFields) { $lookupConditions = array(); foreach ($this->lookupFields as $fieldName) { $lookupConditions[$fieldName] = $item[$fieldName]; } $entity = $this->entityRepository->findOneBy($lookupConditions); } else { $entity = $this->entityRepository->find(current($item)); } } if (!$entity) { return $this->getNewInstance(); } return $entity; }
/** * Returns entity found for given criteria * * @param array $criteria * * @return object */ public function findOneBy(array $criteria) { return $this->repository->findOneBy($criteria); }