diff --git a/src/Mapper.php b/src/Mapper.php index 862e10a..1e565ff 100644 --- a/src/Mapper.php +++ b/src/Mapper.php @@ -82,13 +82,13 @@ public function persist(object $object, Collection $onCollection): object $this->persisting[$object] = true; try { - $next = $onCollection->next; + $connectsTo = $onCollection->connectsTo; - if ($next) { - $remote = $this->style->remoteIdentifier($next->name); + if ($connectsTo) { + $remote = $this->style->remoteIdentifier($connectsTo->name); $related = $this->getRelatedEntity($object, $remote); if ($related !== null) { - $this->persist($related, $next); + $this->persist($related, $connectsTo); } } @@ -384,9 +384,9 @@ private function buildSelectStatement(Sql $sql, array $collections): Sql } } - $nextName = $c->next?->name; - if ($nextName !== null) { - $fk = $this->style->remoteIdentifier($nextName); + $connectedName = $c->connectsTo?->name; + if ($connectedName !== null) { + $fk = $this->style->remoteIdentifier($connectedName); $selectTable[] = self::aliasedColumn($tableSpecifier, $fk, $fields[$fk] ?? $fk); } } @@ -492,7 +492,7 @@ private function parseCollection( $s = $this->style; $entity = $collection->name; $parent = $collection->parent?->name; - $next = $collection->next?->name; + $connected = $collection->connectsTo?->name; $parentAlias = $parent ? $aliases[$parent] : null; $aliases[$entity] = $alias; @@ -532,7 +532,7 @@ private function parseCollection( $aliasedPk = $alias . '.' . $s->identifier($entity); $aliasedParentPk = $parentAlias . '.' . $s->identifier($parent); - if ($this->hasComposition($entity, $next, $parent)) { + if ($this->hasComposition($entity, $connected, $parent)) { $onName = $alias . '.' . $s->remoteIdentifier($parent); $onAlias = $aliasedParentPk; } else { @@ -543,14 +543,14 @@ private function parseCollection( $sql->on([$onName => $onAlias]); } - private function hasComposition(string $entity, string|null $next, string|null $parent): bool + private function hasComposition(string $entity, string|null $connected, string|null $parent): bool { - if ($next === null || $parent === null) { + if ($connected === null || $parent === null) { return false; } - return $entity === $this->style->composed($parent, $next) - || $entity === $this->style->composed($next, $parent); + return $entity === $this->style->composed($parent, $connected) + || $entity === $this->style->composed($connected, $parent); } /** @param SplObjectStorage $hydrated */ diff --git a/tests/MapperTest.php b/tests/MapperTest.php index 7987e39..e24b398 100644 --- a/tests/MapperTest.php +++ b/tests/MapperTest.php @@ -1545,9 +1545,10 @@ public function testReadOnlyWithChangesAndPersistRoundTrip(): void $bob = $this->mapper->read_only_author[2]->fetch(); - // withChanges creates modified copy, PK preserved → auto-update - $updated = $this->mapper->entityFactory->withChanges( - $post, + // Partial entity with same PK → auto-update via identity map + $updated = $this->mapper->entityFactory->create( + ReadOnlyPost::class, + id: 1, title: 'Changed', readOnlyAuthor: $bob, ); @@ -1562,16 +1563,18 @@ public function testReadOnlyWithChangesAndPersistRoundTrip(): void $this->assertSame(2, (int) $result->read_only_author_id); } - public function testPersistWithInlineChangesRoundTrip(): void + public function testPersistPartialEntityRoundTrip(): void { $fetched = $this->mapper->read_only_author[1]->fetch(); $this->assertSame('Alice', $fetched->name); - $updated = $this->mapper->read_only_author[1]->persist( - $fetched, + $partial = $this->mapper->entityFactory->create( + ReadOnlyAuthor::class, + id: 1, name: 'Alice Updated', bio: 'new bio', ); + $updated = $this->mapper->read_only_author->persist($partial); $this->mapper->flush(); $this->assertNotSame($fetched, $updated); @@ -1584,7 +1587,7 @@ public function testPersistWithInlineChangesRoundTrip(): void $this->assertSame('new bio', $result->bio); } - public function testPersistWithInlineChangesOnGraph(): void + public function testPersistPartialEntityOnGraph(): void { $this->conn->exec((string) Sql::createTable('read_only_post', [ 'id INTEGER PRIMARY KEY', @@ -1600,14 +1603,16 @@ public function testPersistWithInlineChangesOnGraph(): void ->values([1, 'Original', 'Body', 1]) ->exec(); - $post = $this->mapper->read_only_post->read_only_author->fetch(); + $this->mapper->read_only_post->read_only_author->fetch(); $bob = $this->mapper->read_only_author[2]->fetch(); - $updated = $this->mapper->read_only_post->persist( - $post, + $partial = $this->mapper->entityFactory->create( + ReadOnlyPost::class, + id: 1, title: 'Changed', readOnlyAuthor: $bob, ); + $updated = $this->mapper->read_only_post->persist($partial); $this->mapper->flush(); $this->assertSame(1, $updated->id);