Skip to content

Commit

Permalink
fix init of 1:1 relationship from non main side [closes #588]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Apr 15, 2023
1 parent 57355a0 commit 3b0c1cc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Relationships/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ protected function getPrimaryValue(): mixed
*/
protected function getValue(bool $allowPreloadContainer = true): ?IEntity
{
if (!$this->isValueValidated && $this->value !== null) {
if (!$this->isValueValidated && ($this->value !== null || $this->metadata->isNullable)) {
$this->initValue($allowPreloadContainer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
namespace NextrasTests\Orm\Integration\Relationships;


use Nextras\Orm\Relationships\HasMany;
use Nextras\Orm\Relationships\HasOne;
use NextrasTests\Orm\Book;
use NextrasTests\Orm\DataTestCase;
use NextrasTests\Orm\Ean;
use NextrasTests\Orm\Photo;
use NextrasTests\Orm\PhotoAlbum;
use Tester\Assert;


Expand Down Expand Up @@ -45,6 +46,29 @@ class RelationshipOneHasOneTest extends DataTestCase
}


public function testReadOnNullable(): void
{
$album = new PhotoAlbum();
$album->title = 'Test';
$this->orm->photoAlbums->persist($album);

$photo = new Photo();
$photo->title = 'Test';
$photo->album = $album;
$photo->previewFor = $album;
$this->orm->photos->persistAndFlush($photo);

$photoId = $photo->id;
$albumId = $album->id;
$this->orm->clear();

$photo = $this->orm->photos->getByIdChecked($photoId);
$album = $photo->previewFor;
Assert::notNull($album);
Assert::equal($albumId, $album->id);
}


public function testPersistence(): void
{
$this->orm->clear();
Expand Down Expand Up @@ -95,6 +119,7 @@ class RelationshipOneHasOneTest extends DataTestCase
}


/** @noinspection PhpFieldImmediatelyRewrittenInspection */
public function testUpdateRelationship(): void
{
$book1 = new Book();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
START TRANSACTION;
INSERT INTO "photo_albums" ("title", "preview_id") VALUES ('Test', NULL);
SELECT CURRVAL('photo_albums_id_seq');
INSERT INTO "photos" ("title", "album_id") VALUES ('Test', 1);
SELECT CURRVAL('photos_id_seq');
UPDATE "photo_albums" SET "preview_id" = 1 WHERE "id" = 1;
COMMIT;
SELECT "photos".* FROM "photos" AS "photos" WHERE (("photos"."id" = 1));
SELECT "photo_albums".* FROM "photo_albums" AS "photo_albums" WHERE "photo_albums"."preview_id" IN (1);

0 comments on commit 3b0c1cc

Please sign in to comment.