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
12 changes: 7 additions & 5 deletions src/Relationships/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -380,7 +382,7 @@ public function getEntitiesForPersistence(): array
if ($isImmediate || $entity === null) {
return $this->tracked;
} else {
return $this->tracked + [$entity];
return [...$this->tracked, $entity];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?php declare(strict_types = 1);

/**
* @testCase
* @dataProvider ../../../databases.ini
*/

namespace NextrasTests\Orm\Integration\Relationships;


use Nextras\Dbal\IConnection;
use Nextras\Orm\Relationships\OneHasOne;
use NextrasTests\Orm\Book;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Helper;
use Tester\Assert;
use Tester\Environment;

require_once __DIR__ . '/../../../bootstrap.php';


class RelationshipOneHasOneOneSidedTest extends DataTestCase
{
private Book $book;
private IConnection $connection;


protected function setUp(): void
{
parent::setUp();
if ($this->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();
22 changes: 22 additions & 0 deletions tests/cases/integration/Relationships/relationships.oneHasOne.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Loading