Skip to content

Commit 21a2d17

Browse files
committed
Adopt connectsTo rename, use partial entities
Follows Data's Collection API rename (next→connectsTo) and removal of the $changes parameter from Collection::persist(). Tests now construct partial entities via entityFactory->create() for identity map merging.
1 parent df35c4a commit 21a2d17

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

src/Mapper.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ public function persist(object $object, Collection $onCollection): object
8282
$this->persisting[$object] = true;
8383

8484
try {
85-
$next = $onCollection->next;
85+
$connectsTo = $onCollection->connectsTo;
8686

87-
if ($next) {
88-
$remote = $this->style->remoteIdentifier($next->name);
87+
if ($connectsTo) {
88+
$remote = $this->style->remoteIdentifier($connectsTo->name);
8989
$related = $this->getRelatedEntity($object, $remote);
9090
if ($related !== null) {
91-
$this->persist($related, $next);
91+
$this->persist($related, $connectsTo);
9292
}
9393
}
9494

@@ -384,9 +384,9 @@ private function buildSelectStatement(Sql $sql, array $collections): Sql
384384
}
385385
}
386386

387-
$nextName = $c->next?->name;
388-
if ($nextName !== null) {
389-
$fk = $this->style->remoteIdentifier($nextName);
387+
$connectedName = $c->connectsTo?->name;
388+
if ($connectedName !== null) {
389+
$fk = $this->style->remoteIdentifier($connectedName);
390390
$selectTable[] = self::aliasedColumn($tableSpecifier, $fk, $fields[$fk] ?? $fk);
391391
}
392392
}
@@ -492,7 +492,7 @@ private function parseCollection(
492492
$s = $this->style;
493493
$entity = $collection->name;
494494
$parent = $collection->parent?->name;
495-
$next = $collection->next?->name;
495+
$connected = $collection->connectsTo?->name;
496496

497497
$parentAlias = $parent ? $aliases[$parent] : null;
498498
$aliases[$entity] = $alias;
@@ -532,7 +532,7 @@ private function parseCollection(
532532
$aliasedPk = $alias . '.' . $s->identifier($entity);
533533
$aliasedParentPk = $parentAlias . '.' . $s->identifier($parent);
534534

535-
if ($this->hasComposition($entity, $next, $parent)) {
535+
if ($this->hasComposition($entity, $connected, $parent)) {
536536
$onName = $alias . '.' . $s->remoteIdentifier($parent);
537537
$onAlias = $aliasedParentPk;
538538
} else {
@@ -543,14 +543,14 @@ private function parseCollection(
543543
$sql->on([$onName => $onAlias]);
544544
}
545545

546-
private function hasComposition(string $entity, string|null $next, string|null $parent): bool
546+
private function hasComposition(string $entity, string|null $connected, string|null $parent): bool
547547
{
548-
if ($next === null || $parent === null) {
548+
if ($connected === null || $parent === null) {
549549
return false;
550550
}
551551

552-
return $entity === $this->style->composed($parent, $next)
553-
|| $entity === $this->style->composed($next, $parent);
552+
return $entity === $this->style->composed($parent, $connected)
553+
|| $entity === $this->style->composed($connected, $parent);
554554
}
555555

556556
/** @param SplObjectStorage<object, Collection> $hydrated */

tests/MapperTest.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,9 +1545,10 @@ public function testReadOnlyWithChangesAndPersistRoundTrip(): void
15451545

15461546
$bob = $this->mapper->read_only_author[2]->fetch();
15471547

1548-
// withChanges creates modified copy, PK preserved → auto-update
1549-
$updated = $this->mapper->entityFactory->withChanges(
1550-
$post,
1548+
// Partial entity with same PK → auto-update via identity map
1549+
$updated = $this->mapper->entityFactory->create(
1550+
ReadOnlyPost::class,
1551+
id: 1,
15511552
title: 'Changed',
15521553
readOnlyAuthor: $bob,
15531554
);
@@ -1562,16 +1563,18 @@ public function testReadOnlyWithChangesAndPersistRoundTrip(): void
15621563
$this->assertSame(2, (int) $result->read_only_author_id);
15631564
}
15641565

1565-
public function testPersistWithInlineChangesRoundTrip(): void
1566+
public function testPersistPartialEntityRoundTrip(): void
15661567
{
15671568
$fetched = $this->mapper->read_only_author[1]->fetch();
15681569
$this->assertSame('Alice', $fetched->name);
15691570

1570-
$updated = $this->mapper->read_only_author[1]->persist(
1571-
$fetched,
1571+
$partial = $this->mapper->entityFactory->create(
1572+
ReadOnlyAuthor::class,
1573+
id: 1,
15721574
name: 'Alice Updated',
15731575
bio: 'new bio',
15741576
);
1577+
$updated = $this->mapper->read_only_author->persist($partial);
15751578
$this->mapper->flush();
15761579

15771580
$this->assertNotSame($fetched, $updated);
@@ -1584,7 +1587,7 @@ public function testPersistWithInlineChangesRoundTrip(): void
15841587
$this->assertSame('new bio', $result->bio);
15851588
}
15861589

1587-
public function testPersistWithInlineChangesOnGraph(): void
1590+
public function testPersistPartialEntityOnGraph(): void
15881591
{
15891592
$this->conn->exec((string) Sql::createTable('read_only_post', [
15901593
'id INTEGER PRIMARY KEY',
@@ -1600,14 +1603,16 @@ public function testPersistWithInlineChangesOnGraph(): void
16001603
->values([1, 'Original', 'Body', 1])
16011604
->exec();
16021605

1603-
$post = $this->mapper->read_only_post->read_only_author->fetch();
1606+
$this->mapper->read_only_post->read_only_author->fetch();
16041607
$bob = $this->mapper->read_only_author[2]->fetch();
16051608

1606-
$updated = $this->mapper->read_only_post->persist(
1607-
$post,
1609+
$partial = $this->mapper->entityFactory->create(
1610+
ReadOnlyPost::class,
1611+
id: 1,
16081612
title: 'Changed',
16091613
readOnlyAuthor: $bob,
16101614
);
1615+
$updated = $this->mapper->read_only_post->persist($partial);
16111616
$this->mapper->flush();
16121617

16131618
$this->assertSame(1, $updated->id);

0 commit comments

Comments
 (0)