-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cascade detach should detach lazy collections too #11717
base: 2.20.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
|
@@ -2390,29 +2390,27 @@ | |
|
||
$visited[$oid] = $entity; // mark visited | ||
|
||
switch ($this->getEntityState($entity, self::STATE_DETACHED)) { | ||
case self::STATE_MANAGED: | ||
if ($this->isInIdentityMap($entity)) { | ||
$this->removeFromIdentityMap($entity); | ||
} | ||
$state = $this->getEntityState($entity, self::STATE_DETACHED); | ||
if (! $noCascade && $state === self::STATE_MANAGED) { | ||
$this->cascadeDetach($entity, $visited); | ||
} | ||
|
||
unset( | ||
$this->entityInsertions[$oid], | ||
$this->entityUpdates[$oid], | ||
$this->entityDeletions[$oid], | ||
$this->entityIdentifiers[$oid], | ||
$this->entityStates[$oid], | ||
$this->originalEntityData[$oid] | ||
); | ||
break; | ||
case self::STATE_NEW: | ||
case self::STATE_DETACHED: | ||
return; | ||
if ($state !== self::STATE_MANAGED) { | ||
return; | ||
} | ||
|
||
if (! $noCascade) { | ||
$this->cascadeDetach($entity, $visited); | ||
if ($this->isInIdentityMap($entity)) { | ||
$this->removeFromIdentityMap($entity); | ||
} | ||
|
||
unset( | ||
$this->entityInsertions[$oid], | ||
$this->entityUpdates[$oid], | ||
$this->entityDeletions[$oid], | ||
$this->entityIdentifiers[$oid], | ||
$this->entityStates[$oid], | ||
$this->originalEntityData[$oid] | ||
); | ||
} | ||
|
||
/** | ||
|
@@ -2548,11 +2546,7 @@ | |
$relatedEntities = $class->reflFields[$assoc['fieldName']]->getValue($entity); | ||
|
||
switch (true) { | ||
case $relatedEntities instanceof PersistentCollection: | ||
// Unwrap so that foreach() does not initialize | ||
$relatedEntities = $relatedEntities->unwrap(); | ||
// break; is commented intentionally! | ||
|
||
// in order to detach the entities in the collection, initialization is needed (no unwrap) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be documented in the docs, because it could lead to the operation becoming very expensive. It should also be documented in upgrade notes. You could also implement a shortcut here, if no entity of $assoc entity is in the identity map, then loading the collection from database will not yield any benefit. For both lazy and extra lazy you could also make a query to load only the IDs, then check if they are in the identity map to detech. This would make the query + loading cheaper. The reaosn is that the entities loaded here are immediately going out of scope and garbage collected anyways. |
||
case $relatedEntities instanceof Collection: | ||
case is_array($relatedEntities): | ||
foreach ($relatedEntities as $relatedEntity) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to make this changes because "child" objects should be detached first, and then the parents (otherwise the IDs are not available for some DBs as sqlite)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am afraid this is going to break some people event listeners :)
We should add this the UPGRADE notes to warn people. We don't guarantee the order so its not a BC break, but a heads up is always nice.