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
4 changes: 4 additions & 0 deletions src/Relation/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ public function queue(Pool $pool, Tuple $tuple): void
if ($related instanceof ReferenceInterface && $related->hasValue()) {
$related = $related->getValue();
$state->setRelation($relName, $related);
if ($related === null) {
$state->setRelationStatus($this->getName(), RelationInterface::STATUS_RESOLVED);
return;
}
}
if ($related === null) {
$this->setNullFromRelated($tuple, false);
Expand Down
15 changes: 8 additions & 7 deletions src/Relation/Morphed/BelongsToMorphed.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ public function initReference(Node $node): ReferenceInterface
{
$scope = $this->getReferenceScope($node);
$nodeData = $node->getData();
if ($scope === null || !isset($nodeData[$this->morphKey])) {
$result = new Reference($node->getRole(), []);
$result->setValue(null);
return $result;
if (!isset($nodeData[$this->morphKey], $scope)) {
return new EmptyReference('?', null);
}
// $scope[$this->morphKey] = $nodeData[$this->morphKey];
$target = $nodeData[$this->morphKey];
Expand All @@ -48,7 +46,7 @@ public function prepare(Pool $pool, Tuple $tuple, mixed $related, bool $load = t
parent::prepare($pool, $tuple, $related, $load);
$related = $tuple->state->getRelation($this->getName());

if ($related === null) {
if ($related === null || $related instanceof EmptyReference) {
return;
}

Expand All @@ -70,8 +68,11 @@ protected function assertValid(Node $related): void

protected function setNullFromRelated(Tuple $tuple, bool $isPreparing): void
{
// Set morph key to null
$tuple->state->register($this->morphKey, null);
if ($tuple->node->getRelation($this->getName()) !== null) {
// Set morph key to null if the relation was changed to null
$tuple->state->register($this->morphKey, null);
}

parent::setNullFromRelated($tuple, $isPreparing);
}
}
2 changes: 1 addition & 1 deletion src/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public function offset(int $offset): self
* Available DTO classes (one per relation type):
* - {@see \Cycle\ORM\Select\Options\HasOneLoadOptions}
* - {@see \Cycle\ORM\Select\Options\HasManyLoadOptions}
* - {@see \Cycle\ORM\Select\Options\BelongsToLoadOptions}
* - {@see \Cycle\ORM\Select\Options\BelongsToLoadOptions} applicable for both Belongs To and Refers To relations
* - {@see \Cycle\ORM\Select\Options\ManyToManyLoadOptions}
* - {@see \Cycle\ORM\Select\Options\MorphedHasOneLoadOptions}
* - {@see \Cycle\ORM\Select\Options\MorphedHasManyLoadOptions}
Expand Down
1 change: 1 addition & 0 deletions tests/ORM/Fixtures/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
class Image
{
public $id;
public $parentType;
public $parent;
public $url;
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ public function testSetNullShouldClearMorphType(): void
$row = $this->getDatabase()->table('image')->select()->where('id', 1)->fetchAll();
$this->assertSame('user', $row[0]['parent_type']);
$this->assertNotNull($row[0]['parent_id']);
$this->save($c);

$c->parent = null;
$this->save($c);
Expand Down Expand Up @@ -318,6 +319,43 @@ public function testCreateWithoutParentShouldNotSetMorphType(): void
$this->assertNull($row[0]['parent_type'], 'parent_type should be NULL for entity without parent');
}

public function testUsingFactoryCreateWithoutRelatedButSetMorphTypeManually(): void
{
$schemaArray = $this->getNullableMorphedSchemaArray();
$this->orm = $this->orm->with(
schema: new Schema($schemaArray),
);

/** @var Image $c */
$c = $this->orm->make(Image::class);
$c->url = 'no-parent.png';
$c->parentType = 'user';

$this->save($c);

$row = $this->getDatabase()->table('image')->select()->where('id', $c->id)->fetchAll();
$this->assertNull($row[0]['parent_id'], 'parent_id should be NULL for entity without parent');
$this->assertSame('user', $row[0]['parent_type'], 'parent_type should be NULL for entity without parent');
}

public function testCreateWithoutRelatedButSetMorphTypeManually(): void
{
$schemaArray = $this->getNullableMorphedSchemaArray();
$this->orm = $this->orm->with(
schema: new Schema($schemaArray),
);

$c = new Image();
$c->url = 'no-parent.png';
$c->parentType = 'user';

$this->save($c);

$row = $this->getDatabase()->table('image')->select()->where('id', $c->id)->fetchAll();
$this->assertNull($row[0]['parent_id'], 'parent_id should be NULL for entity without parent');
$this->assertSame('user', $row[0]['parent_type'], 'parent_type should be NULL for entity without parent');
}

public function testUpdateRelation(): void
{
$this->captureReadQueries();
Expand Down
Loading