Skip to content

Commit

Permalink
minor improvements in embedded logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfy-j committed Jun 6, 2019
1 parent f60105b commit b4c3d1e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Relation/Embedded.php
Expand Up @@ -118,7 +118,7 @@ public function initPromise(Node $parentNode): array
}

$r = $this->orm->promise($this->target, [$this->primaryKey => $primaryKey]);
return [$r, new Nil()];
return [$r, [$this->primaryKey => $primaryKey]];
}

/**
Expand All @@ -135,7 +135,16 @@ public function extract($data)
public function queue(CC $store, $entity, Node $node, $related, $original): CommandInterface
{
if ($related instanceof ReferenceInterface) {
$related = $this->resolve($related);
if ($related->__scope() === $original) {
// do not update non resolved and non changed promises
if (!$related instanceof PromiseInterface || !$related->__loaded()) {
return new Nil();
}
$related = $this->resolve($related);
} else {
// do not affect parent embeddings
$related = clone $this->resolve($related);
}
}

if ($related === null) {
Expand Down
45 changes: 45 additions & 0 deletions tests/ORM/EmbeddedRelationTest.php
Expand Up @@ -272,6 +272,51 @@ public function testChangePromise()
$this->assertSame('user3', $u2->credentials->username);
}

public function testSavePromise()
{
$selector = new Select($this->orm, User::class);
$u = $selector->orderBy('id', 'ASC')->fetchOne();

$this->captureWriteQueries();
$this->captureReadQueries();
$t = new Transaction($this->orm);
$t->persist($u);
$t->run();
$this->assertNumWrites(0);
$this->assertNumReads(0);
}

public function testMovePromise()
{
$selector = new Select($this->orm, User::class);
$u = $selector->orderBy('id', 'ASC')->fetchOne();

$selector = new Select($this->orm, User::class);
$u2 = $selector->orderBy('id', 'ASC')->wherePK(2)->fetchOne();

$u->credentials = $u2->credentials;

$this->captureWriteQueries();
$this->captureReadQueries();
$t = new Transaction($this->orm);
$t->persist($u);
$t->run();
$this->assertNumWrites(1);
$this->assertNumReads(1);

$selector = new Select($this->orm->withHeap(new Heap()), User::class);
$u3 = $selector->load('credentials')->wherePK($u->id)->fetchOne();

$this->assertEquals($u->id, $u3->id);
$this->assertSame('user2', $u3->credentials->username);

$selector = new Select($this->orm->withHeap(new Heap()), User::class);
$u4 = $selector->load('credentials')->wherePK($u2->id)->fetchOne();

// unchanged
$this->assertEquals($u2->id, $u4->id);
$this->assertSame('user2', $u4->credentials->username);
}

public function testSelectEmbeddable()
{
Expand Down

0 comments on commit b4c3d1e

Please sign in to comment.