Skip to content

Commit

Permalink
Merge pull request #155 from gnat42/PR20
Browse files Browse the repository at this point in the history
Modify DoctrineWriter adding capability to persist entities with relatio...
  • Loading branch information
Baachi committed Mar 7, 2015
2 parents 4bb5939 + affdb07 commit 3cc324d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,12 @@ or
$writer = new DoctrineWriter($entityManager, 'YourNamespace:Employee', array('column1', 'column2', 'column3'));
```

The DoctrineWriter will also search out associations automatically and link them by an entity reference. For example
suppose you have a Product entity that you are importing and must be associated to a Category. If there is a field in
the import file named 'Category' with an id, the writer will use metadata to get the association class and create a
reference so that it can be associated properly. The DoctrineWriter will skip any association fields that are already
objects in cases where a converter was used to retrieve the association.

#### PdoWriter

Use the PDO writer for importing data into a relational database (such as
Expand Down
27 changes: 27 additions & 0 deletions src/Ddeboer/DataImport/Writer/DoctrineWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ public function writeItem(array $item)
$entity = $this->getNewInstance();
}

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

$fieldNames = array_merge($this->entityMetadata->getFieldNames(), $this->entityMetadata->getAssociationNames());
foreach ($fieldNames as $fieldName) {

Expand Down Expand Up @@ -243,6 +245,31 @@ public function writeItem(array $item)
return $this;
}

/**
* 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']]) && !is_object($item[$associationMapping['fieldName']])) {
$value = $this->entityManager->getReference($associationMapping['targetEntity'], $item[$associationMapping['fieldName']]);
}

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

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

/**
* Truncate the database table for this writer
*
Expand Down
51 changes: 49 additions & 2 deletions tests/Ddeboer/DataImport/Tests/Writer/DoctrineWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function testBatches()
protected function getEntityManager()
{
$em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->setMethods(array('getRepository', 'getClassMetadata', 'persist', 'flush', 'clear', 'getConnection'))
->setMethods(array('getRepository', 'getClassMetadata', 'persist', 'flush', 'clear', 'getConnection', 'getReference'))
->disableOriginalConstructor()
->getMock();

Expand All @@ -66,7 +66,7 @@ protected function getEntityManager()
->getMock();

$metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadata')
->setMethods(array('getName', 'getFieldNames', 'getAssociationNames', 'setFieldValue'))
->setMethods(array('getName', 'getFieldNames', 'getAssociationNames', 'setFieldValue', 'getAssociationMappings'))
->disableOriginalConstructor()
->getMock();

Expand All @@ -82,6 +82,10 @@ protected function getEntityManager()
->method('getAssociationNames')
->will($this->returnValue(array('firstAssociation')));

$metadata->expects($this->any())
->method('getAssociationMappings')
->will($this->returnValue(array(array('fieldName' => 'firstAssociation','targetEntity' => 'Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity'))));

$configuration = $this->getMockBuilder('Doctrine\DBAL\Configuration')
->setMethods(array('getConnection'))
->disableOriginalConstructor()
Expand Down Expand Up @@ -146,4 +150,47 @@ public function testFluentInterface()
$this->assertSame($writer, $writer->writeItem($item));
$this->assertSame($writer, $writer->finish());
}

public function testLoadAssociationWithoutObject()
{
$em = $this->getEntityManager();

$em->expects($this->once())
->method('persist');

$em->expects($this->once())
->method('getReference');

$writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');

$item = array(
'firstProperty' => 'some value',
'secondProperty' => 'some other value',
'firstAssociation' => 'firstAssociationId'
);

$writer->writeItem($item);
}

public function testLoadAssociationWithPresetObject()
{
$em = $this->getEntityManager();

$em->expects($this->once())
->method('persist');

$em->expects($this->never())
->method('getReference');

$writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');

$association = new TestEntity();
$item = array(
'firstProperty' => 'some value',
'secondProperty' => 'some other value',
'firstAssociation' => $association,
);

$writer->writeItem($item);
}
}

0 comments on commit 3cc324d

Please sign in to comment.