Skip to content

Commit

Permalink
Merge pull request #31 from yannickl88/master
Browse files Browse the repository at this point in the history
Fixed issue that identifier fields are missing from original data
  • Loading branch information
Hidde Boomsma authored Oct 27, 2016
2 parents 77470ac + 4f76c65 commit c6d8831
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/Provider/EntityMutationMetadataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ public function __construct(Reader $reader, LoggerInterface $logger = null)
* hydrate it with the original data.
*
* @param EntityManagerInterface $em
* @param mixed $data
* @param mixed $entity
* @return object
*/
public function createOriginalEntity(EntityManagerInterface $em, $entity)
{
$data = $em->getUnitOfWork()->getOriginalEntityData($entity);
$uow = $em->getUnitOfWork();
$id_data = $uow->isInIdentityMap($entity) ? $uow->getEntityIdentifier($entity) : [];
$data = $uow->getOriginalEntityData($entity);
$metadata = $em->getClassMetadata(get_class($entity));
$original = $metadata->newInstance();
$fields = array_merge($metadata->getFieldNames(), $metadata->getAssociationNames());
Expand All @@ -56,6 +58,10 @@ public function createOriginalEntity(EntityManagerInterface $em, $entity)
foreach ($fields as $field) {
if (isset($data[$field])) {
$metadata->setFieldValue($original, $field, $data[$field]);
continue;
}
if (isset($id_data[$field]) && $metadata->isIdentifier($field) && $metadata->isIdGeneratorIdentity()) {
$metadata->setFieldValue($original, $field, $id_data[$field]);
}
}

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

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

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

/**
* @ORM\Column(type="string")
*/
private $address;

/**
* @ORM\ManyToMany(targetEntity="Visitor", cascade={"persist"})
* @ORM\JoinTable(
* joinColumns={@ORM\JoinColumn(name="contract_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="visitor_id", referencedColumnName="id")}
* )
* @var Collection
*/
private $visitors;

/**
* @param string $address
*/
public function __construct($address)
{
$this->address = $address;
$this->visitors = new ArrayCollection();
}

/**
* @return int
*/
public function getId()
{
return $this->id;
}

/**
* @return string
*/
public function getAddress()
{
return $this->address;
}

/**
* @param string $name
*/
public function addVisitor($name)
{
$this->visitors->add(new Visitor($name));
}
}
33 changes: 33 additions & 0 deletions test/Provider/Entity/Visitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Hostnet\Component\EntityTracker\Provider\Entity;

use Doctrine\ORM\Mapping as ORM;

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

/**
* @ORM\Column(type="string")
* @var string
*/
private $name;

/**
* @param string $name
*/
public function __construct($name)
{
$this->name = $name;
}
}
19 changes: 18 additions & 1 deletion test/Provider/EntityMutationMetadataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\ORM\Tools\SchemaTool;
use Doctrine\ORM\Tools\Setup;
use Hostnet\Component\DatabaseTest\MysqlPersistentConnection;
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 @@ -58,7 +59,6 @@ public function testChanges()
self::assertCount(1, $this->provider->getFullChangeSet($this->em));
}


public function testCreateOriginalEntity()
{
$tall_ship = new Painting('Tall Ship');
Expand All @@ -72,6 +72,23 @@ public function testCreateOriginalEntity()
self::assertSame('Tall Ship', $original->name);
}

public function testCreateOriginalEntityIdentity()
{
$gallery = new Gallery('Riverstreet 12');
$gallery->addVisitor('Foo de Bar');

$this->em->persist($gallery);
$this->em->flush();

$gallery->addVisitor('Bar Baz');

$this->em->getUnitOfWork()->computeChangeSets();
$this->em->flush();

$original = $this->provider->createOriginalEntity($this->em, $gallery);
self::assertSame($gallery->getId(), $original->getId());
}

/**
* @depends testCreateOriginalEntity
*/
Expand Down

0 comments on commit c6d8831

Please sign in to comment.