Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand All @@ -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<object, Collection> $hydrated */
Expand Down
25 changes: 15 additions & 10 deletions tests/MapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -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);
Expand All @@ -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',
Expand All @@ -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);
Expand Down
Loading