Skip to content

Commit

Permalink
persistence: fixed repeated persistence of relationships [closes #162]
Browse files Browse the repository at this point in the history
  • Loading branch information
JanTvrdik committed Mar 14, 2016
1 parent cffc101 commit 9cdbdea
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 13 deletions.
5 changes: 4 additions & 1 deletion src/Relationships/HasMany.php
Expand Up @@ -45,6 +45,9 @@ abstract class HasMany extends Object implements IRelationshipCollection
/** @var bool */
protected $isModified = false;

/** @var bool */
protected $wasLoaded = false;

/** @var IRelationshipMapper */
protected $relationshipMapper;

Expand Down Expand Up @@ -188,7 +191,7 @@ public function getIterator()

public function isLoaded()
{
return !($this->collection === null && empty($this->toAdd) && empty($this->toRemove));
return $this->wasLoaded || $this->collection !== null || !empty($this->toAdd) || !empty($this->toRemove);
}


Expand Down
15 changes: 9 additions & 6 deletions src/Relationships/ManyHasMany.php
Expand Up @@ -19,8 +19,8 @@ public function getEntitiesForPersistence()
foreach ($this->toAdd as $entity) {
$entities[] = $entity;
}
if ($this->collection) {
foreach ($this->collection as $entity) {
if ($this->collection || $this->wasLoaded) {
foreach ($this->getIterator() as $entity) {
$entities[] = $entity;
}
}
Expand All @@ -41,10 +41,13 @@ public function doPersist()
$toAdd[$id] = $id;
}

$this->toAdd = [];
$this->toRemove = [];
$this->isModified = false;
$this->collection = null;
if ($this->isModified) {
$this->toAdd = [];
$this->toRemove = [];
$this->isModified = false;
$this->collection = null;
$this->wasLoaded = true;
}

if ($this->metadata->relationship->isMain) {
$this->getRelationshipMapper()->remove($this->parent, $toRemove);
Expand Down
13 changes: 8 additions & 5 deletions src/Relationships/OneHasMany.php
Expand Up @@ -24,7 +24,7 @@ public function getEntitiesForPersistence()
$entities[] = $remove;
}
}
if ($this->collection !== null) {
if ($this->collection !== null || $this->wasLoaded) {
foreach ($this->getIterator() as $entity) {
$entities[] = $entity;
}
Expand All @@ -35,10 +35,13 @@ public function getEntitiesForPersistence()

public function doPersist()
{
$this->toAdd = [];
$this->toRemove = [];
$this->collection = null;
$this->isModified = false;
if ($this->isModified) {
$this->toAdd = [];
$this->toRemove = [];
$this->collection = null;
$this->isModified = false;
$this->wasLoaded = true;
}
}


Expand Down
Expand Up @@ -143,6 +143,26 @@ class RelationshipManyHasManyTest extends DataTestCase
Assert::same(0, $tags->count());
}


public function testRepeatedPersisting()
{
$tagA = new Tag('A');
$tagB = new Tag('B');

$book = $this->orm->books->getById(1);
$book->tags->add($tagA);
$book->tags->add($tagB);

$this->orm->persistAndFlush($book);
Assert::false($tagA->isModified());
Assert::false($tagB->isModified());

$tagA->name = 'X';
$this->orm->persistAndFlush($book);
Assert::false($tagA->isModified());
Assert::false($tagB->isModified());
}

}


Expand Down
Expand Up @@ -29,7 +29,7 @@ class RelationshipsOneHasManyIsLoadedTest extends TestCase
$this->orm->authors->flush();

foreach ($this->orm->authors->findAll() as $author) {
Assert::false($author->books->isLoaded());
Assert::true($author->books->isLoaded());
}

foreach ($this->orm->authors->findAll() as $author) {
Expand Down
Expand Up @@ -10,6 +10,7 @@ use Mockery;
use NextrasTests\Orm\Author;
use NextrasTests\Orm\Book;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Publisher;
use Tester\Assert;

$dic = require_once __DIR__ . '/../../../bootstrap.php';
Expand Down Expand Up @@ -42,7 +43,28 @@ class RelationshipsOneHasManyPersistenceTest extends DataTestCase
foreach ($books as $book) {
Assert::false($book->isModified());
}
}


public function testRepeatedPersisting()
{
$publisher = new Publisher();
$publisher->name = 'Jupiter Mining Corporation';

$author = new Author();
$author->name = 'Arnold Judas Rimmer';

$book = new Book();
$book->title = 'Better Than Life';
$book->publisher = $publisher;
$book->author = $author;

$this->orm->persistAndFlush($author);
Assert::false($book->isModified());

$book->title = 'Backwards';
$this->orm->persistAndFlush($author);
Assert::false($book->isModified());
}

}
Expand Down

0 comments on commit 9cdbdea

Please sign in to comment.