Skip to content

Commit

Permalink
Recursivly add elements to change set (#35)
Browse files Browse the repository at this point in the history
* Recursivly add elements to change set

* Only check the cascade persist relations
  • Loading branch information
yannickl88 authored and linaori committed Oct 31, 2017
1 parent 26781f9 commit 08601d9
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
51 changes: 31 additions & 20 deletions src/Provider/EntityMutationMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,32 +195,45 @@ public function getFullChangeSet(EntityManagerInterface $em)
foreach ($managed as $class => $entities) {
$metadata = $em->getClassMetadata($class);

if (isset($change_set[$class])) {
$change_set[$class] = array_merge($change_set[$class], array_values($entities));
} else {
$change_set[$class] = array_values($entities);
}

foreach ($entities as $entity) {
$this->appendAssociations($em, $metadata, $entity, $change_set);
$this->addToChangeSet($em, $metadata, $entity, $change_set);
}
}

foreach ($new as $entity) {
$metadata = $em->getClassMetadata(get_class($entity));

if (!isset($change_set[$metadata->rootEntityName])) {
$change_set[$metadata->rootEntityName] = [];
}
$this->addToChangeSet($em, $metadata, $entity, $change_set);
}

if (!in_array($entity, $change_set[$metadata->rootEntityName], true)) {
$change_set[$metadata->rootEntityName][] = $entity;
}
return $change_set;
}

$this->appendAssociations($em, $metadata, $entity, $change_set);
/**
* Add an entity to the change set. This also adds any elements to the
* change set that are in the associations.
*
* @param EntityManagerInterface $em
* @param ClassMetadata $metadata
* @param mixed $entity
* @param array $change_set
*/
private function addToChangeSet(
EntityManagerInterface $em,
ClassMetadata $metadata,
$entity,
array &$change_set
) {
if (!isset($change_set[$metadata->rootEntityName])) {
$change_set[$metadata->rootEntityName] = [];
}

return $change_set;
if (!in_array($entity, $change_set[$metadata->rootEntityName], true)) {
$change_set[$metadata->rootEntityName][] = $entity;

// recursively find all changes
$this->appendAssociations($em, $metadata, $entity, $change_set);
}
}

/**
Expand Down Expand Up @@ -257,13 +270,11 @@ private function appendAssociations(
continue;
}

if (!isset($change_set[$target_class->rootEntityName])) {
$change_set[$target_class->rootEntityName] = [];
if (!$assoc['isCascadePersist']) {
continue;
}

if (!in_array($entry, $change_set[$target_class->rootEntityName], true)) {
$change_set[$target_class->rootEntityName][] = $entry;
}
$this->addToChangeSet($em, $target_class, $entry, $change_set);
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion test/Provider/Entity/B.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ class B

/**
* @ORM\ManyToOne(targetEntity="A", inversedBy="bees")
* @var Visitor
* @var A
*/
public $a;

/**
* @ORM\OneToMany(targetEntity="C", mappedBy="c", cascade={"persist"})
* @var c[]|ArrayCollection
*/
public $cees;

public function __construct()
{
$this->cees = new ArrayCollection();
}

}
31 changes: 31 additions & 0 deletions test/Provider/Entity/C.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Hostnet\Component\EntityTracker\Provider\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class C
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*
* @var int
*/
private $id;

/**
* @ORM\ManyToOne(targetEntity="B", inversedBy="cees")
* @var C
*/
public $b;

public function __construct()
{
}

}
10 changes: 7 additions & 3 deletions test/Provider/EntityMutationMetadataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Hostnet\Component\DatabaseTest\MysqlPersistentConnection;
use Hostnet\Component\EntityTracker\Provider\Entity\A;
use Hostnet\Component\EntityTracker\Provider\Entity\B;
use Hostnet\Component\EntityTracker\Provider\Entity\C;
use Hostnet\Component\EntityTracker\Provider\Entity\Gallery;
use Hostnet\Component\EntityTracker\Provider\Entity\Node;
use Hostnet\Component\EntityTracker\Provider\Entity\Painting;
Expand Down Expand Up @@ -99,6 +100,7 @@ public function testChangesNewEntityFlushedBadOrder()
$a = new A();
$b1 = new B();
$b2 = new B();
$c = new C();

$a->bees->add($b1);
$b1->a = $a;
Expand All @@ -110,10 +112,14 @@ public function testChangesNewEntityFlushedBadOrder()
$a->bees->add($b2);
$b2->a = $a;

$b2->cees->add($c);
$c->b = $b2;

$change_set = $this->provider->getFullChangeSet($this->em);

self::assertCount(1, $change_set[A::class]);
self::assertCount(2, $change_set[B::class]);
self::assertCount(1, $change_set[C::class]);
}

public function testChangesNewEntityOneToOne()
Expand All @@ -123,9 +129,7 @@ public function testChangesNewEntityOneToOne()

$this->em->persist($root);

self::assertEquals([
Node::class => [$root, $mirror],
], $this->provider->getFullChangeSet($this->em));
self::assertEquals([Node::class => [$root]], $this->provider->getFullChangeSet($this->em));
}

public function testCreateOriginalEntity()
Expand Down

0 comments on commit 08601d9

Please sign in to comment.