Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ddeboer/data-import
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: javierseixas/data-import
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Checking mergeability… Don’t worry, you can still create the pull request.
  • 1 commit
  • 1 file changed
  • 1 contributor

Commits on Apr 10, 2013

  1. Modify DoctrineWriter adding capability to persist entities with rela…

    …tions
    Javier Seixas committed Apr 10, 2013
    Copy the full SHA
    5de0c5b View commit details
Showing with 35 additions and 3 deletions.
  1. +35 −3 src/Ddeboer/DataImport/Writer/DoctrineWriter.php
38 changes: 35 additions & 3 deletions src/Ddeboer/DataImport/Writer/DoctrineWriter.php
Original file line number Diff line number Diff line change
@@ -179,9 +179,14 @@ public function writeItem(array $item, array $originalItem = array())
// first
if (false === $this->truncate) {
if ($this->index) {
$entity = $this->entityRepository->findOneBy(array(
$this->index => $item[$this->index]
));
if (!is_array($this->index)) {
$tableIndex = array($this->index => $item[$this->index]);
} else {
foreach ($this->index as $index) {
$tableIndex[$index] = $item[$index];
}
}
$entity = $this->entityRepository->findOneBy($tableIndex);
} else {
$entity = $this->entityRepository->find(current($item));
}
@@ -192,6 +197,8 @@ public function writeItem(array $item, array $originalItem = array())
$entity = $this->getNewInstance($className, $item);
}

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

foreach ($this->entityMetadata->getFieldNames() as $fieldName) {

$value = null;
@@ -223,6 +230,31 @@ public function writeItem(array $item, array $originalItem = array())
}
}

/**
* Add the associated objects in case the item have for persist its relation
*
* @param array $item
* @param $entity
* @return void
*/
protected function loadAssociationObjectsToEntity(array $item, $entity)
{
foreach ($this->entityMetadata->getAssociationMappings() as $associationMapping) {

$value = null;
if (isset($item[$associationMapping['fieldName']])) {
$value = $this->entityManager->getRepository($associationMapping['targetEntity'])->find($item[$associationMapping['fieldName']]);
}

if (null === $value) {
continue;
}

$setter = 'set' . ucfirst($associationMapping['fieldName']);
$this->setValue($entity, $value, $setter);
}
}

/**
* Truncate the database table for this writer
*