Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow DoctrineWriter to handle association values #245

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion src/Writer/DoctrineWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public function prepare()
/**
* Return a new instance of the entity
*
* @throws \Exception
* @return object
*/
protected function getNewInstance()
Expand Down Expand Up @@ -176,6 +177,7 @@ public function writeItem(array $item)
{
$entity = $this->findOrCreateItem($item);

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

Expand Down Expand Up @@ -210,6 +212,60 @@ protected function updateEntity(array $item, $entity)
}
}

/**
* @param array $item
* @param $entity
* @throws \Exception
*/
public function updateAssociations(array &$item, $entity)
{
foreach ($this->entityMetadata->getAssociationMappings() as $map) {
// There is data for the association
if (isset($item[$map['fieldName']]) && is_array($item[$map['fieldName']])) {
$this->updateAssociation($item, $entity, $map['fieldName'], $map['targetEntity']);
}
}
}

/**
* @param array $item
* @param object $entity
* @param string $fieldName
* @param string $targetEntity
* @throws \Exception
*/
public function updateAssociation(array &$item, $entity, $fieldName, $targetEntity)
{
$getterMethod = sprintf('get%s', $fieldName);

if (method_exists($entity, $getterMethod)) {
$orgRepository = $this->entityRepository;
$orgMetadata = $this->entityMetadata;
$orgName = $this->entityName;

$this->entityRepository = $this->entityManager->getRepository($targetEntity);
$this->entityMetadata = $this->entityManager->getClassMetadata($targetEntity);
$this->entityName = $this->entityMetadata->getName();

$association = call_user_func([$entity,$getterMethod]);
if (!$association) {
$association = $this->getNewInstance();
}

$value = $item[$fieldName];
unset($item[$fieldName]);

$this->updateEntity($value, $association);

$setterMethod = sprintf('set%s',$fieldName);
call_user_func([$entity, $setterMethod],$association);

$this->entityRepository = $orgRepository;
$this->entityMetadata = $orgMetadata;
$this->entityName = $orgName;
}
}

/**
* Add the associated objects in case the item have for persist its relation
*
Expand All @@ -221,7 +277,7 @@ protected function loadAssociationObjectsToEntity(array $item, $entity)
foreach ($this->entityMetadata->getAssociationMappings() as $associationMapping) {

$value = null;
if (isset($item[$associationMapping['fieldName']]) && !is_object($item[$associationMapping['fieldName']])) {
if (isset($item[$associationMapping['fieldName']]) && !is_object($item[$associationMapping['fieldName']]) && !is_array($item[$associationMapping['fieldName']])) {
$value = $this->entityManager->getReference($associationMapping['targetEntity'], $item[$associationMapping['fieldName']]);
}

Expand Down Expand Up @@ -268,6 +324,7 @@ protected function reEnableLogging()
* Finds existing entity or create a new instance
*
* @param array $item
* @return mixed|null|object
*/
protected function findOrCreateItem(array $item)
{
Expand Down
37 changes: 37 additions & 0 deletions tests/Fixtures/Entity/TestEntityAssociation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Created by PhpStorm.
* User: gnat
* Date: 29/07/15
* Time: 3:20 PM
*/

namespace Ddeboer\DataImport\Tests\Fixtures\Entity;


class TestEntityAssociation
{
private $aProperty;

private $bProperty;

public function getAProperty()
{
return $this->aProperty;
}

public function setAProperty($firstProperty)
{
$this->aProperty = $firstProperty;
}

public function getBProperty()
{
return $this->bProperty;
}

public function setBProperty($secondProperty)
{
$this->bProperty = $secondProperty;
}
}
50 changes: 46 additions & 4 deletions tests/Writer/DoctrineWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ protected function getEntityManager()
->disableOriginalConstructor()
->getMock();

$assocMetadata = clone $metadata;

$metadata->expects($this->any())
->method('getName')
->will($this->returnValue('Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntity'));
Expand All @@ -55,7 +57,19 @@ protected function getEntityManager()

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

$assocMetadata->expects($this->any())
->method('getName')
->will($this->returnValue('Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntityAssociation'));

$assocMetadata->expects($this->any())
->method('getFieldNames')
->will($this->returnValue(array('aProperty', 'bProperty')));

$assocMetadata->expects($this->any())
->method('getAssociationNames')
->will($this->returnValue(array()));

$configuration = $this->getMockBuilder('Doctrine\DBAL\Configuration')
->setMethods(array('getConnection'))
Expand Down Expand Up @@ -83,13 +97,15 @@ protected function getEntityManager()
->method('executeQuery')
->with('TRUNCATE SQL');

$em->expects($this->once())
$em->expects($this->any())
->method('getRepository')
->will($this->returnValue($repo));

$em->expects($this->once())
$em->expects($this->any())
->method('getClassMetadata')
->will($this->returnValue($metadata));
->will($this->returnCallback(function($arg) use ($metadata,$assocMetadata){
return ($arg =='DdeboerDataImport:TestEntity') ? $metadata :$assocMetadata;
}));

$em->expects($this->any())
->method('getConnection')
Expand Down Expand Up @@ -149,6 +165,32 @@ public function testLoadAssociationWithPresetObject()
$writer->writeItem($item);
}

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

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

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

$association = new TestEntity();
$writer = new DoctrineWriter($em, 'DdeboerDataImport:TestEntity');
$item = [
'firstProperty' => 'some value',
'secondProperty' => 'some other value',
'firstAssociation' => [
'aProperty' => 'another value',
'bProperty' => 'another other value',
]
];

$writer->updateAssociations($item,$association);
$this->assertInstanceOf('Ddeboer\DataImport\Tests\Fixtures\Entity\TestEntityAssociation',$association->getFirstAssociation());
$this->assertEquals('another value',$association->getFirstAssociation()->getAProperty());
}

/**
* Test to make sure that we are clearing the write entity
*/
Expand Down