Skip to content

Instantly share code, notes, and snippets.

@jwage
Created January 31, 2011 17:49
Show Gist options
  • Save jwage/804459 to your computer and use it in GitHub Desktop.
Save jwage/804459 to your computer and use it in GitHub Desktop.
<?php
// create SoftDeleteManager
$sdm = new SoftDeleteManager($dm);
// Soft delete a few documents
$sdm->delete($user);
$sdm->delete($product);
$sdm->delete($seller);
// Flush and commit the deletions
$sdm->flush();
/**
* db.users.update( { _id : { $in : userIds } }, $set : { deleted: true } )
* db.products.update( { _id : { $in : productIds } }, $set : { deleted: true })
* db.sellers.update( { _id : { $in : sellerIds } }, $set : { deleted: true } )
*/
// undelete a specific document
$sdm->undelete($user);
$sdm->flush();
/**
* db.users.update( { _id : { $in : userIds } }, { $unset { deleted : true } } )
*/
$sdm->delete($document);
$sdm->clear(); // clear and forget any pending deletes or undeletes
// Query for documents includes condition to exlude deleted documents
$qb = $sdm->createQueryBuilder('Product');
$query = $qb->getQuery();
$products = $query->execute();
/**
* db.users.find( { deleted: { $ne : true } })
*/
// Get only deleted documents
$qb = $sdm->createDeletedQueryBuilder('Product');
$query = $qb->getQuery();
$products = $query->execute();
/**
* db.users.find( { deleted: { $ne : false } })
*/
// Dealing with deleted references
// Delete a sellable that is still referenced on a product
$sdm->delete($sellable);
$sdm->flush();
$sellables = $product->getSellables();
/**
* Doctrine lazily loads sellables based on array of references and the ids included.
* We can use a Doctrine event to alter this query to ignore deleted documents.
*
* db.sellables.find( { _id : { $in : referencedSellableIds }, deleted : { $ne : true }} )
*
* So even though the reference to the deleted sellable still exists in products in the database
* the resulting collection of Sellables on the Product will not have the deleted sellable present.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment