Skip to content

Rework scheduling for orphan removal #1280

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

Closed
wants to merge 3 commits into from
Closed
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
33 changes: 16 additions & 17 deletions lib/Doctrine/ODM/MongoDB/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,20 @@ function ($a, $b) { return $a === $b ? 0 : 1; }
);
}

/**
* INTERNAL: get objects that were removed, unlike getDeleteDiff this doesn't care about indices.
*
* @return array
*/
public function getDeletedDocuments()
{
return array_udiff(
$this->snapshot,
$this->coll->toArray(),
function ($a, $b) { return $a === $b ? 0 : ($a > $b ? 1 : -1); }
);
}

/**
* INTERNAL:
* getInsertDiff
Expand Down Expand Up @@ -420,10 +434,6 @@ public function remove($key)

$this->changed();

if ($this->isOrphanRemovalEnabled()) {
$this->uow->scheduleOrphanRemoval($removed);
}

return $removed;
}

Expand All @@ -441,10 +451,6 @@ public function removeElement($element)

$this->changed();

if ($this->isOrphanRemovalEnabled()) {
$this->uow->scheduleOrphanRemoval($element);
}

return $removed;
}

Expand Down Expand Up @@ -534,18 +540,11 @@ public function count()
*/
public function set($key, $value)
{
$oldValue = $this->get($key);
$this->coll->set($key, $value);

// Handle orphanRemoval
if ($this->uow !== null && $this->isOrphanRemovalEnabled() && $oldValue !== $value) {
if ($oldValue !== null) {
$this->uow->scheduleOrphanRemoval($oldValue);
}

if ($value !== null) {
$this->uow->unscheduleOrphanRemoval($value);
}
if ($this->uow !== null && $this->isOrphanRemovalEnabled() && $value !== null) {
$this->uow->unscheduleOrphanRemoval($value);
}

$this->changed();
Expand Down
10 changes: 8 additions & 2 deletions lib/Doctrine/ODM/MongoDB/UnitOfWork.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class UnitOfWork implements PropertyChangedListener
private $evm;

/**
* Embedded documents that are scheduled for removal.
* Additional documents that are scheduled for removal.
*
* @var array
*/
Expand Down Expand Up @@ -898,12 +898,18 @@ private function computeAssociationChanges($parentDocument, array $assoc, $value
return;
}

if ($value instanceof PersistentCollection && $value->isDirty() && $assoc['isOwningSide']) {
if ($value instanceof PersistentCollection && $value->isDirty() && ($assoc['isOwningSide'] || isset($assoc['embedded']))) {
if ($topOrExistingDocument || CollectionHelper::usesSet($assoc['strategy'])) {
$this->scheduleCollectionUpdate($value);
}
$topmostOwner = $this->getOwningDocument($value->getOwner());
$this->visitedCollections[spl_object_hash($topmostOwner)][] = $value;
if ( ! empty($assoc['orphanRemoval']) || isset($assoc['embedded'])) {
$value->initialize();
foreach ($value->getDeletedDocuments() as $orphan) {
$this->scheduleOrphanRemoval($orphan);
}
}
}

// Look through the documents, and in any of their associations,
Expand Down
Loading