Skip to content

Commit

Permalink
Merge pull request #172 from gnat42/patch-6
Browse files Browse the repository at this point in the history
Split writeItem function into two
  • Loading branch information
Baachi committed Mar 19, 2015
2 parents 57031f1 + dc7a103 commit 455c239
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions src/Ddeboer/DataImport/Writer/DoctrineWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,27 +189,7 @@ public function finish()
public function writeItem(array $item)
{
$this->counter++;
$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) {
$entity = $this->getNewInstance();
}
$entity = $this->findOrCreateItem($item);

$this->loadAssociationObjectsToEntity($item, $entity);

Expand Down Expand Up @@ -300,4 +280,33 @@ protected function reEnableLogging()
$config = $this->entityManager->getConnection()->getConfiguration();
$config->setSQLLogger($this->originalLogger);
}

/**
* 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;
}
}

0 comments on commit 455c239

Please sign in to comment.