Skip to content

Commit

Permalink
Merge pull request #99 from tommygnr/allow-m2m-audit
Browse files Browse the repository at this point in the history
Allow auditing of entities with many-many relations
  • Loading branch information
DavidBadura committed Dec 10, 2014
2 parents acab791 + 6fad68c commit 7dd95ca
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/SimpleThings/EntityAudit/AuditReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,9 @@ private function createEntity($className, array $data, $revision)
$class->reflFields[$assoc['fieldName']]->setValue($entity, $collection);
}
} else {
throw new \Exception(sprintf('Association type %d is not yet supported', $assoc['type']));
// Inject collection
$reflField = $class->reflFields[$field];
$reflField->setValue($entity, new ArrayCollection);
}
}

Expand Down
83 changes: 83 additions & 0 deletions tests/SimpleThings/Tests/EntityAudit/RelationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class RelationTest extends BaseTest
'SimpleThings\EntityAudit\Tests\OwnerEntity',
'SimpleThings\EntityAudit\Tests\OwnedEntity1',
'SimpleThings\EntityAudit\Tests\OwnedEntity2',
'SimpleThings\EntityAudit\Tests\OwnedEntity3',
'SimpleThings\EntityAudit\Tests\OneToOneMasterEntity',
'SimpleThings\EntityAudit\Tests\OneToOneAuditedEntity',
'SimpleThings\EntityAudit\Tests\OneToOneNotAuditedEntity',
Expand Down Expand Up @@ -169,6 +170,37 @@ public function testOneToOne()
$this->assertEquals('notaudited', $audited->getNotAudited()->getTitle());
}

/**
* This test verifies the temporary behaviour of audited entities with M-M relationships
* until https://github.com/simplethings/EntityAudit/issues/85 is implemented
*/
public function testManyToMany()
{
$auditReader = $this->auditManager->createAuditReader($this->em);

$owner = new OwnerEntity();
$owner->setTitle('owner#1');

$owned31 = new OwnedEntity3();
$owned31->setTitle('owned3#1');
$owner->addOwned3($owned31);

$owned32 = new OwnedEntity3();
$owned32->setTitle('owned3#2');
$owner->addOwned3($owned32);

$this->em->persist($owner);
$this->em->persist($owned31);
$this->em->persist($owned32);

$this->em->flush(); //#1

//checking that getOwned3() returns an empty collection
$audited = $auditReader->find(get_class($owner), $owner->getId(), 1);
$this->assertInstanceOf('Doctrine\Common\Collections\Collection', $audited->getOwned3());
$this->assertCount(0, $audited->getOwned3());
}

public function testRelations()
{
$auditReader = $this->auditManager->createAuditReader($this->em);
Expand Down Expand Up @@ -691,6 +723,9 @@ class OwnerEntity
/** @ORM\OneToMany(targetEntity="OwnedEntity2", mappedBy="owner") */
protected $owned2;

/** @ORM\ManyToMany(targetEntity="OwnedEntity3", mappedBy="owner") */
protected $owned3;

public function getId()
{
return $this->id;
Expand Down Expand Up @@ -725,6 +760,16 @@ public function addOwned2($owned2)
{
$this->owned2[] = $owned2;
}

public function getOwned3()
{
return $this->owned3;
}

public function addOwned3($owned3)
{
$this->owned3[] = $owned3;
}
}

/** @ORM\Entity */
Expand Down Expand Up @@ -803,6 +848,44 @@ public function setOwner($owner)
}
}

/** @ORM\Entity */
class OwnedEntity3
{
/** @ORM\Id @ORM\Column(type="integer", name="strange_owned_id_name") @ORM\GeneratedValue(strategy="AUTO") */
protected $id;

/** @ORM\Column(type="string", name="even_strangier_column_name") */
protected $title;

/** @ORM\ManyToMany(targetEntity="OwnerEntity", inversedBy="owned3")*/
protected $owner;

public function getId()
{
return $this->id;
}

public function getTitle()
{
return $this->title;
}

public function setTitle($title)
{
$this->title = $title;
}

public function getOwner()
{
return $this->owner;
}

public function setOwner($owner)
{
$this->owner = $owner;
}
}

/**
* @ORM\MappedSuperclass()
*/
Expand Down

0 comments on commit 7dd95ca

Please sign in to comment.