From a01946c1e15cdb3834e2690009156dbf559632cb Mon Sep 17 00:00:00 2001 From: Oliver Forral Date: Wed, 8 Oct 2014 20:55:23 -0700 Subject: [PATCH] modified DoctrineWriter's setValue to check for manyToMany add methods --- .../DataImport/Writer/DoctrineWriter.php | 17 +++++++++--- .../Tests/Fixtures/Entity/TestEntity.php | 16 ++++++++++++ .../Tests/Writer/DoctrineWriterTest.php | 26 ++++++++++++------- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/Ddeboer/DataImport/Writer/DoctrineWriter.php b/src/Ddeboer/DataImport/Writer/DoctrineWriter.php index b8bbe78f..461f66bf 100644 --- a/src/Ddeboer/DataImport/Writer/DoctrineWriter.php +++ b/src/Ddeboer/DataImport/Writer/DoctrineWriter.php @@ -162,10 +162,22 @@ protected function getNewInstance() return new $className; } - protected function setValue($entity, $value, $setter) + protected function setValue($entity, $value, $fieldName) { + $getter = 'get' . ucfirst($fieldName); + $setter = 'set' . ucfirst($fieldName); + $adder = 'add' . ucfirst($fieldName); + $remover = 'remove' . ucfirst($fieldName); if (method_exists($entity, $setter)) { $entity->$setter($value); + } elseif (method_exists($entity, $adder)) { + $oldValue = $entity->$getter(); + foreach ($oldValue as $oldItem) { + $entity->$remover($oldItem); + } + foreach ($value as $newItem) { + $entity->$adder($newItem); + } } } @@ -228,8 +240,7 @@ public function writeItem(array $item) if (!($value instanceof \DateTime) || $value != $this->entityMetadata->getFieldValue($entity, $fieldName) ) { - $setter = 'set' . ucfirst($fieldName); - $this->setValue($entity, $value, $setter); + $this->setValue($entity, $value, $fieldName); } } diff --git a/tests/Ddeboer/DataImport/Tests/Fixtures/Entity/TestEntity.php b/tests/Ddeboer/DataImport/Tests/Fixtures/Entity/TestEntity.php index f8d046b7..a1fa0cb7 100644 --- a/tests/Ddeboer/DataImport/Tests/Fixtures/Entity/TestEntity.php +++ b/tests/Ddeboer/DataImport/Tests/Fixtures/Entity/TestEntity.php @@ -10,6 +10,13 @@ class TestEntity private $firstAssociation; + private $secondAssociation; + + public function __construct() + { + $this->secondAssociation = array(); + } + public function getFirstProperty() { return $this->firstProperty; @@ -40,4 +47,13 @@ public function setFirstAssociation($firstAssociation) $this->firstAssociation = $firstAssociation; } + public function getSecondAssociation() + { + return $this->secondAssociation; + } + + public function addSecondAssociation($secondAssociation) + { + $this->secondAssociation[] = $secondAssociation; + } } diff --git a/tests/Ddeboer/DataImport/Tests/Writer/DoctrineWriterTest.php b/tests/Ddeboer/DataImport/Tests/Writer/DoctrineWriterTest.php index e29d4ff7..e2c0c0e4 100644 --- a/tests/Ddeboer/DataImport/Tests/Writer/DoctrineWriterTest.php +++ b/tests/Ddeboer/DataImport/Tests/Writer/DoctrineWriterTest.php @@ -17,10 +17,12 @@ public function testWriteItem() $writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity'); $association = new TestEntity(); + $secondAssociation = array(new TestEntity()); $item = array( - 'firstProperty' => 'some value', - 'secondProperty' => 'some other value', - 'firstAssociation'=> $association + 'firstProperty' => 'some value', + 'secondProperty' => 'some other value', + 'firstAssociation' => $association, + 'secondAssociation' => $secondAssociation ); $writer->writeItem($item); } @@ -41,10 +43,12 @@ public function testBatches() $this->assertEquals(3, $writer->getBatchSize()); $association = new TestEntity(); + $secondAssociation = array(new TestEntity()); $item = array( - 'firstProperty' => 'some value', - 'secondProperty' => 'some other value', - 'firstAssociation'=> $association + 'firstProperty' => 'some value', + 'secondProperty' => 'some other value', + 'firstAssociation' => $association, + 'secondAssociation' => $secondAssociation ); for ($i = 0; $i < 11; $i++) { @@ -80,7 +84,7 @@ protected function getEntityManager() $metadata->expects($this->any()) ->method('getAssociationNames') - ->will($this->returnValue(array('firstAssociation'))); + ->will($this->returnValue(array('firstAssociation', 'secondAssociation'))); $configuration = $this->getMockBuilder('Doctrine\DBAL\Configuration') ->setMethods(array('getConnection')) @@ -136,10 +140,12 @@ public function testFluentInterface() $writer = new DoctrineWriter($this->getEntityManager(), 'DdeboerDataImport:TestEntity'); $association = new TestEntity(); + $secondAssociation = array(new TestEntity()); $item = array( - 'firstProperty' => 'some value', - 'secondProperty' => 'some other value', - 'firstAssociation'=> $association + 'firstProperty' => 'some value', + 'secondProperty' => 'some other value', + 'firstAssociation' => $association, + 'secondAssociation' => $secondAssociation ); $this->assertSame($writer, $writer->prepare());