From efa7643e76b65148fd26b160e37d3ed1f142b6b2 Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Fri, 6 Nov 2026 23:56:23 +0100 Subject: [PATCH 1/2] Fix replacing one-sided relationships after target removal Avoid resolving the previous target when replacing an unloaded one-sided relationship. This allows assigning a replacement or null after the previous target was removed, without throwing NoResultException. Preserve tracking of already loaded targets and updates to reverse relationships. Add database regression tests using the existing Book::nextPart relationship as one-sided through test-local metadata. Clear the stored reference and delete its target through DBAL, leaving the loaded owner's lazy reference stale while preserving foreign-key constraints. Verify entity, ID, new-entity and null assignments through persistence and reload, including an already loaded reference. Fixes #846 --- src/Relationships/HasOne.php | 10 +- .../relationships.oneHasOne.oneSided.phpt | 140 ++++++++++++++++++ ...OneOneSidedTest_testClearDeletedTarget.sql | 8 + ...SidedTest_testClearLoadedDeletedTarget.sql | 10 ++ ...eOneSidedTest_testReplaceDeletedTarget.sql | 11 ++ ...SidedTest_testReplaceDeletedTargetById.sql | 10 ++ ..._testReplaceDeletedTargetWithNewEntity.sql | 14 ++ 7 files changed, 199 insertions(+), 4 deletions(-) create mode 100644 tests/cases/integration/Relationships/relationships.oneHasOne.oneSided.phpt create mode 100644 tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testClearDeletedTarget.sql create mode 100644 tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testClearLoadedDeletedTarget.sql create mode 100644 tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTarget.sql create mode 100644 tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTargetById.sql create mode 100644 tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTargetWithNewEntity.sql diff --git a/src/Relationships/HasOne.php b/src/Relationships/HasOne.php index aabef76e..86699c69 100644 --- a/src/Relationships/HasOne.php +++ b/src/Relationships/HasOne.php @@ -162,11 +162,13 @@ public function set($value, bool $allowNull = false): bool $isChanged = $this->isChanged($entity); if ($isChanged) { $this->modify(); - $oldEntity = $this->getValue(allowPreloadContainer: false); - if ($oldEntity !== null) { - $this->tracked[] = $oldEntity; + if ($this->metadataRelationship->property !== null || $this->isLoaded()) { + $oldEntity = $this->getValue(allowPreloadContainer: false); + if ($oldEntity !== null) { + $this->tracked[] = $oldEntity; + } + $this->updateRelationship($oldEntity, $entity, $allowNull); } - $this->updateRelationship($oldEntity, $entity, $allowNull); } else { $this->initReverseRelationship($entity); } diff --git a/tests/cases/integration/Relationships/relationships.oneHasOne.oneSided.phpt b/tests/cases/integration/Relationships/relationships.oneHasOne.oneSided.phpt new file mode 100644 index 00000000..b126512c --- /dev/null +++ b/tests/cases/integration/Relationships/relationships.oneHasOne.oneSided.phpt @@ -0,0 +1,140 @@ +section === Helper::SECTION_ARRAY) { + Environment::skip('Test requires DBAL.'); + } + + $relationship = $this->orm->books->getEntityMetadata()->getProperty('nextPart')->relationship; + Assert::notNull($relationship); + $relationship->property = null; + $this->connection = $this->container->getByType(IConnection::class); + $this->book = $this->orm->books->getByIdChecked(4); + } + + + public function testReplaceDeletedTarget(): void + { + $property = $this->book->getProperty('nextPart'); + assert($property instanceof OneHasOne); + Assert::false($property->isLoaded()); + $this->connection->query('UPDATE books SET next_part = NULL WHERE id = 4'); + $this->connection->query('DELETE FROM books WHERE id = 3'); + Assert::same(3, $this->book->getRawValue('nextPart')); + $replacement = $this->orm->books->getByIdChecked(2); + + $this->book->nextPart = $replacement; + Assert::same($replacement, $this->book->nextPart); + Assert::same(2, $this->book->getRawValue('nextPart')); + Assert::true($this->book->isModified('nextPart')); + $this->orm->books->persistAndFlush($this->book); + + $this->orm->clear(); + Assert::same($this->orm->books->getByIdChecked(2), $this->orm->books->getByIdChecked(4)->nextPart); + Assert::null($this->orm->books->getById(3)); + } + + + public function testClearDeletedTarget(): void + { + $property = $this->book->getProperty('nextPart'); + assert($property instanceof OneHasOne); + Assert::false($property->isLoaded()); + $this->connection->query('UPDATE books SET next_part = NULL WHERE id = 4'); + $this->connection->query('DELETE FROM books WHERE id = 3'); + + $this->book->nextPart = null; + Assert::null($this->book->getRawValue('nextPart')); + Assert::true($this->book->isModified('nextPart')); + $this->orm->books->persistAndFlush($this->book); + + $this->orm->clear(); + Assert::null($this->orm->books->getByIdChecked(4)->nextPart); + Assert::null($this->orm->books->getById(3)); + } + + + public function testReplaceDeletedTargetById(): void + { + $this->connection->query('UPDATE books SET next_part = NULL WHERE id = 4'); + $this->connection->query('DELETE FROM books WHERE id = 3'); + + $this->book->nextPart = 2; + Assert::same($this->orm->books->getByIdChecked(2), $this->book->nextPart); + $this->orm->books->persistAndFlush($this->book); + + $this->orm->clear(); + Assert::same($this->orm->books->getByIdChecked(2), $this->orm->books->getByIdChecked(4)->nextPart); + } + + + public function testReplaceDeletedTargetWithNewEntity(): void + { + $this->connection->query('UPDATE books SET next_part = NULL WHERE id = 4'); + $this->connection->query('DELETE FROM books WHERE id = 3'); + $replacement = new Book(); + $replacement->title = 'Replacement book'; + $replacement->author = $this->orm->authors->getByIdChecked(1); + $replacement->publisher = $this->orm->publishers->getByIdChecked(1); + + $this->book->nextPart = $replacement; + $this->orm->books->persistAndFlush($this->book); + Assert::true($replacement->isPersisted()); + $replacementId = $replacement->id; + + $this->orm->clear(); + Assert::same( + $this->orm->books->getByIdChecked($replacementId), + $this->orm->books->getByIdChecked(4)->nextPart, + ); + Assert::null($this->orm->books->getById(3)); + } + + + public function testClearLoadedDeletedTarget(): void + { + $target = $this->orm->books->getByIdChecked(3); + Assert::same($target, $this->book->nextPart); + $this->connection->query('UPDATE books SET next_part = NULL WHERE id = 4'); + $this->connection->query('DELETE FROM books WHERE id = 3'); + + $this->book->nextPart = null; + $this->orm->books->persistAndFlush($this->book); + + $this->orm->clear(); + Assert::null($this->orm->books->getByIdChecked(4)->nextPart); + Assert::null($this->orm->books->getById(3)); + } + +} + + +$test = new RelationshipOneHasOneOneSidedTest(); +$test->run(); diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testClearDeletedTarget.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testClearDeletedTarget.sql new file mode 100644 index 00000000..5002c32c --- /dev/null +++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testClearDeletedTarget.sql @@ -0,0 +1,8 @@ +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 4; +UPDATE books SET next_part = NULL WHERE id = 4; +DELETE FROM books WHERE id = 3; +START TRANSACTION; +UPDATE "books" SET "next_part" = NULL WHERE "id" = 4; +COMMIT; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 4; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 3; diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testClearLoadedDeletedTarget.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testClearLoadedDeletedTarget.sql new file mode 100644 index 00000000..903abbdd --- /dev/null +++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testClearLoadedDeletedTarget.sql @@ -0,0 +1,10 @@ +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 4; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 3; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" IN (3); +UPDATE books SET next_part = NULL WHERE id = 4; +DELETE FROM books WHERE id = 3; +START TRANSACTION; +UPDATE "books" SET "next_part" = NULL WHERE "id" = 4; +COMMIT; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 4; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 3; diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTarget.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTarget.sql new file mode 100644 index 00000000..61cbe743 --- /dev/null +++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTarget.sql @@ -0,0 +1,11 @@ +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 4; +UPDATE books SET next_part = NULL WHERE id = 4; +DELETE FROM books WHERE id = 3; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 2; +START TRANSACTION; +UPDATE "books" SET "next_part" = 2 WHERE "id" = 4; +COMMIT; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 2; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 4; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" IN (2); +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 3; diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTargetById.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTargetById.sql new file mode 100644 index 00000000..2e9a8dd3 --- /dev/null +++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTargetById.sql @@ -0,0 +1,10 @@ +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 4; +UPDATE books SET next_part = NULL WHERE id = 4; +DELETE FROM books WHERE id = 3; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 2; +START TRANSACTION; +UPDATE "books" SET "next_part" = 2 WHERE "id" = 4; +COMMIT; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 2; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 4; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" IN (2); diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTargetWithNewEntity.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTargetWithNewEntity.sql new file mode 100644 index 00000000..1d8f2d38 --- /dev/null +++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneOneSidedTest_testReplaceDeletedTargetWithNewEntity.sql @@ -0,0 +1,14 @@ +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 4; +UPDATE books SET next_part = NULL WHERE id = 4; +DELETE FROM books WHERE id = 3; +SELECT "authors".* FROM "public"."authors" AS "authors" WHERE "authors"."id" = 1; +SELECT "publishers".* FROM "publishers" AS "publishers" WHERE "publishers"."publisher_id" = 1; +START TRANSACTION; +INSERT INTO "books" ("title", "author_id", "translator_id", "next_part", "ean_id", "publisher_id", "genre", "published_at", "printed_at", "thread_id", "price", "price_currency", "orig_price_cents", "orig_price_currency") VALUES ('Replacement book', 1, NULL, NULL, NULL, 1, 'fantasy', '2021-12-31 23:59:59.000000'::timestamp, NULL, NULL, NULL, NULL, NULL, NULL); +SELECT CURRVAL('public.books_id_seq'); +UPDATE "books" SET "next_part" = 5 WHERE "id" = 4; +COMMIT; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 5; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 4; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" IN (5); +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 3; From 036ada4bb0226b0acee4716e47338b169fa58ec1 Mon Sep 17 00:00:00 2001 From: Jan Skrasek Date: Sun, 8 Nov 2026 20:35:55 +0100 Subject: [PATCH 2/2] Preserve replacement entities in relationship persistence queues Append the current HasOne entity to the tracked entities instead of combining the arrays with a union. Both arrays can contain key 0, causing a tracked previous target to hide the replacement and skip its pending changes. Cover bidirectional 1:1 and m:1 relationships using the existing fixtures. Verify that both previous and replacement targets are queued and their changes survive persistence and reload. --- src/Relationships/HasOne.php | 2 +- .../relationships.manyHasOne.phpt | 23 +++++++++++++++++++ .../relationships.oneHasOne.oneSided.phpt | 1 - .../relationships.oneHasOne.phpt | 22 ++++++++++++++++++ ...stPersistPreviousAndReplacementChanges.sql | 10 ++++++++ ...stPersistPreviousAndReplacementChanges.sql | 10 ++++++++ 6 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasOneTest_testPersistPreviousAndReplacementChanges.sql create mode 100644 tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneTest_testPersistPreviousAndReplacementChanges.sql diff --git a/src/Relationships/HasOne.php b/src/Relationships/HasOne.php index 86699c69..95e6bebb 100644 --- a/src/Relationships/HasOne.php +++ b/src/Relationships/HasOne.php @@ -382,7 +382,7 @@ public function getEntitiesForPersistence(): array if ($isImmediate || $entity === null) { return $this->tracked; } else { - return $this->tracked + [$entity]; + return [...$this->tracked, $entity]; } } diff --git a/tests/cases/integration/Relationships/relationships.manyHasOne.phpt b/tests/cases/integration/Relationships/relationships.manyHasOne.phpt index b785621e..b8423238 100644 --- a/tests/cases/integration/Relationships/relationships.manyHasOne.phpt +++ b/tests/cases/integration/Relationships/relationships.manyHasOne.phpt @@ -10,6 +10,7 @@ namespace NextrasTests\Orm\Integration\Relationships; use Nextras\Orm\Collection\Aggregations\NoneAggregator; use Nextras\Orm\Collection\ICollection; +use Nextras\Orm\Relationships\HasOne; use NextrasTests\Orm\Author; use NextrasTests\Orm\Book; use NextrasTests\Orm\DataTestCase; @@ -123,6 +124,28 @@ class RelationshipManyHasOneTest extends DataTestCase } + public function testPersistPreviousAndReplacementChanges(): void + { + $book = $this->orm->books->getByIdChecked(1); + $previous = $book->translator; + Assert::notNull($previous); + $replacement = $this->orm->authors->getByIdChecked(2); + $previous->name = 'Updated previous translator'; + $replacement->name = 'Updated replacement translator'; + $book->translator = $replacement; + + $property = $book->getProperty('translator'); + assert($property instanceof HasOne); + Assert::contains($previous, $property->getEntitiesForPersistence()); + Assert::contains($replacement, $property->getEntitiesForPersistence()); + $this->orm->books->persistAndFlush($book); + + $this->orm->clear(); + Assert::same('Updated previous translator', $this->orm->authors->getByIdChecked(1)->name); + Assert::same('Updated replacement translator', $this->orm->authors->getByIdChecked(2)->name); + } + + public function testCache(): void { $author = $this->orm->authors->getByIdChecked(2); diff --git a/tests/cases/integration/Relationships/relationships.oneHasOne.oneSided.phpt b/tests/cases/integration/Relationships/relationships.oneHasOne.oneSided.phpt index b126512c..521fa194 100644 --- a/tests/cases/integration/Relationships/relationships.oneHasOne.oneSided.phpt +++ b/tests/cases/integration/Relationships/relationships.oneHasOne.oneSided.phpt @@ -132,7 +132,6 @@ class RelationshipOneHasOneOneSidedTest extends DataTestCase Assert::null($this->orm->books->getByIdChecked(4)->nextPart); Assert::null($this->orm->books->getById(3)); } - } diff --git a/tests/cases/integration/Relationships/relationships.oneHasOne.phpt b/tests/cases/integration/Relationships/relationships.oneHasOne.phpt index b19a87f3..255da9dc 100644 --- a/tests/cases/integration/Relationships/relationships.oneHasOne.phpt +++ b/tests/cases/integration/Relationships/relationships.oneHasOne.phpt @@ -141,6 +141,28 @@ class RelationshipOneHasOneTest extends DataTestCase } + public function testPersistPreviousAndReplacementChanges(): void + { + $book = $this->orm->books->getByIdChecked(4); + $previous = $book->nextPart; + Assert::notNull($previous); + $replacement = $this->orm->books->getByIdChecked(2); + $previous->title = 'Updated previous part'; + $replacement->title = 'Updated replacement part'; + $book->nextPart = $replacement; + + $property = $book->getProperty('nextPart'); + assert($property instanceof HasOne); + Assert::contains($previous, $property->getEntitiesForPersistence()); + Assert::contains($replacement, $property->getEntitiesForPersistence()); + $this->orm->books->persistAndFlush($book); + + $this->orm->clear(); + Assert::same('Updated previous part', $this->orm->books->getByIdChecked(3)->title); + Assert::same('Updated replacement part', $this->orm->books->getByIdChecked(2)->title); + } + + /** @noinspection PhpFieldImmediatelyRewrittenInspection */ public function testUpdateRelationship(): void { diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasOneTest_testPersistPreviousAndReplacementChanges.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasOneTest_testPersistPreviousAndReplacementChanges.sql new file mode 100644 index 00000000..27ad7e75 --- /dev/null +++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipManyHasOneTest_testPersistPreviousAndReplacementChanges.sql @@ -0,0 +1,10 @@ +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 1; +SELECT "authors".* FROM "public"."authors" AS "authors" WHERE "authors"."id" IN (1); +SELECT "authors".* FROM "public"."authors" AS "authors" WHERE "authors"."id" = 2; +START TRANSACTION; +UPDATE "books" SET "translator_id" = 2 WHERE "id" = 1; +UPDATE "public"."authors" SET "name" = 'Updated previous translator' WHERE "id" = 1; +UPDATE "public"."authors" SET "name" = 'Updated replacement translator' WHERE "id" = 2; +COMMIT; +SELECT "authors".* FROM "public"."authors" AS "authors" WHERE "authors"."id" = 1; +SELECT "authors".* FROM "public"."authors" AS "authors" WHERE "authors"."id" = 2; diff --git a/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneTest_testPersistPreviousAndReplacementChanges.sql b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneTest_testPersistPreviousAndReplacementChanges.sql new file mode 100644 index 00000000..3003350b --- /dev/null +++ b/tests/sqls/NextrasTests/Orm/Integration/Relationships/RelationshipOneHasOneTest_testPersistPreviousAndReplacementChanges.sql @@ -0,0 +1,10 @@ +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 4; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" IN (3); +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 2; +START TRANSACTION; +UPDATE "books" SET "next_part" = 2 WHERE "id" = 4; +UPDATE "books" SET "title" = 'Updated previous part' WHERE "id" = 3; +UPDATE "books" SET "title" = 'Updated replacement part' WHERE "id" = 2; +COMMIT; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 3; +SELECT "books".* FROM "books" AS "books" WHERE "books"."id" = 2;