diff --git a/src/Repository/RemovalHelper.php b/src/Repository/RemovalHelper.php index b293ce5e..88fbd9f4 100644 --- a/src/Repository/RemovalHelper.php +++ b/src/Repository/RemovalHelper.php @@ -19,8 +19,8 @@ class RemovalHelper { /** - * @param array> $queuePersist - * @param array $queueRemove + * @param array|IRelationshipCollection> $queuePersist + * @param array $queueRemove */ public static function getCascadeQueueAndSetNulls( IEntity $entity, @@ -51,7 +51,7 @@ public static function getCascadeQueueAndSetNulls( foreach ($prePersist as $value) { $queuePersist[spl_object_id($value)] = $value; } - $queueRemove[$entityHash] = true; + $queueRemove[$entityHash] = $entity; foreach ($pre as $value) { if ($value instanceof IEntity) { static::getCascadeQueueAndSetNulls($value, $model, true, $queuePersist, $queueRemove); @@ -62,6 +62,7 @@ public static function getCascadeQueueAndSetNulls( $queuePersist[spl_object_id($value)] = $value; } } + // re-enqueue to be at the last position unset($queueRemove[$entityHash]); $queueRemove[$entityHash] = $entity; unset($queuePersist[$entityHash]); @@ -126,7 +127,7 @@ public static function getRelationships(IEntity $entity): array /** * @param PropertyMetadata[] $metadata - * @param array> $pre + * @param list|IRelationshipCollection> $pre * @param array $queueRemove */ private static function setNulls( @@ -154,12 +155,15 @@ private static function setNulls( : null; if ($type === Relationship::MANY_HAS_MANY) { + /** @var ManyHasMany $property */ $property = $entity->getProperty($name); assert($property instanceof ManyHasMany); $pre[] = $property; if ($reverseProperty !== null) { foreach ($property as $reverseEntity) { - $pre[] = $reverseEntity->getProperty($reverseProperty->name); + /** @var ManyHasMany $reverseRelationship */ + $reverseRelationship = $reverseEntity->getProperty($reverseProperty->name); + $pre[] = $reverseRelationship; } } $property->set([]); @@ -172,7 +176,9 @@ private static function setNulls( // The reverse side is also being removed, do not set null to this relationship. continue; } - $pre[] = $reverseEntity->getProperty($reverseProperty->name); + /** @var HasOne $reverseRelationship */ + $reverseRelationship = $reverseEntity->getProperty($reverseProperty->name); + $pre[] = $reverseRelationship; $pre[] = $reverseEntity; } $property->set(null, true); @@ -184,6 +190,7 @@ private static function setNulls( $property = $entity->getProperty($name); assert($property instanceof IRelationshipCollection); foreach ($property as $subValue) { + assert($subValue instanceof IEntity); $pre[] = $subValue; } $property->set([]);