Skip to content

Commit

Permalink
#16 : DoctrineEntityLoader handle manytomany relations
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-bastien committed Dec 21, 2018
1 parent d350ff4 commit eb96284
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/Loader/DoctrineInsertUpdateLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ protected function processObject($object)
$property,
$this->references[$relationIdentifier]
);
} elseif ($propertyValue instanceof \Traversable) {
foreach ($propertyValue as $k => $v) {
if ($this->isEntityRelation($v)) {
if (!isset($this->entitiesToProcess[get_class($v)])) {
throw new EntityTypeNotHandledException(get_class($v));
}
$relationIdentifier = $this->entitiesToProcess[get_class($v)]['callback']($v);
if (!isset($this->references[$relationIdentifier])) {
//new relation should be processed before
$this->processObject($v);
}
$propertyValue[$k] = $this->references[$relationIdentifier];
}
}
$this->accessor->setValue(
$object,
$property,
$propertyValue
);
}
}

Expand Down Expand Up @@ -153,6 +172,6 @@ protected function processObject($object)
*/
protected function isEntityRelation($propertyValue)
{
return (is_object($propertyValue) && !($propertyValue instanceof \DateTime));
return (is_object($propertyValue) && !($propertyValue instanceof \DateTime) && !($propertyValue instanceof \Traversable));
}
}
33 changes: 31 additions & 2 deletions tests/Loader/DoctrineInsertUpdateLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ function ($e) {
[
'project',
'code',
'name'
'name',
'tags'
]
);
$loader->addEntityToProcess(
Expand Down Expand Up @@ -123,12 +124,21 @@ function ($e) {
//=======================
// Test relations
//=======================
$tagTodo = new Tag('Todo', 'todo');
$tagDoing = new Tag('Doing', 'doing');
$tagDone = new Tag('Done', 'done');
$tagEasy = new Tag('Easy', 'easy');
$tagHard = new Tag('Hard', 'hard');

$this->assertEquals(2, $em->getRepository(Task::class)->count([]));
$taskSetUp = new Task($projectEtl, 'Bundle setup updated');
$taskSetUp->setCode('etl-bundle-setup');
$taskSetUp->addTag($tagTodo);

$newTask = new Task($projectEtl, 'New Task');
$newTask->setCode('etl-bundle-new-task');
$newTask->addTag($tagDoing);
$newTask->addTag($tagEasy);

$loader->load([$taskSetUp, $newTask]);

Expand All @@ -137,6 +147,7 @@ function ($e) {
'code' => 'etl-bundle-new-task'
]);
$this->assertEquals('New Task', $newTaskLoaded->getName());
$this->assertEquals(2, $newTaskLoaded->getTags()->count());

$newTask->setName('New Task updated');
$loader->load([$taskSetUp, $newTask]);
Expand All @@ -147,6 +158,24 @@ function ($e) {
$taskSetUpLoaded = $em->getRepository(Task::class)->findOneBy([
'code' => 'etl-bundle-setup'
]);
$this->assertEquals(2, count($taskSetUpLoaded->getTags()));
$this->assertEquals(1, count($taskSetUpLoaded->getTags()));
//Test manytomany remove and replace
$tagTodoLoaded = $em->getRepository(Tag::class)->findOneBy(['importId' => 'todo']);
$taskSetUp->removeTag($tagTodoLoaded);
$taskSetUp->addTag($tagDone);

$loader->load([$taskSetUp]);
$taskSetUpLoaded = $em->getRepository(Task::class)->findOneBy([
'code' => 'etl-bundle-setup'
]);
$this->assertEquals(1, count($taskSetUpLoaded->getTags()));
//Test manytomany remove
$tagDoneLoaded = $em->getRepository(Tag::class)->findOneBy(['importId' => 'done']);
$taskSetUp->removeTag($tagDoneLoaded);
$loader->load([$taskSetUp]);
$taskSetUpLoaded = $em->getRepository(Task::class)->findOneBy([
'code' => 'etl-bundle-setup'
]);
$this->assertEquals(0, count($taskSetUpLoaded->getTags()));
}
}

0 comments on commit eb96284

Please # to comment.