diff --git a/src/Relationships/HasOne.php b/src/Relationships/HasOne.php index aabef76e..95e6bebb 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); } @@ -380,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 new file mode 100644 index 00000000..521fa194 --- /dev/null +++ b/tests/cases/integration/Relationships/relationships.oneHasOne.oneSided.phpt @@ -0,0 +1,139 @@ +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/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/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; 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;