From 99b4183889414e593029557c356a829eaba34f8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 1 Jul 2024 23:02:27 +0200 Subject: [PATCH] Remove soft-delete-cookbook (#2657) --- docs/en/cookbook/soft-delete-extension.rst | 226 --------------------- 1 file changed, 226 deletions(-) delete mode 100644 docs/en/cookbook/soft-delete-extension.rst diff --git a/docs/en/cookbook/soft-delete-extension.rst b/docs/en/cookbook/soft-delete-extension.rst deleted file mode 100644 index 8c1cd3993..000000000 --- a/docs/en/cookbook/soft-delete-extension.rst +++ /dev/null @@ -1,226 +0,0 @@ -Soft Delete Extension -===================== - -Sometimes you may not want to delete data from your database completely, but you want to -disable or temporarily delete some records so they do not appear anymore in your frontend. -Then, later you might want to restore that deleted data like it was never deleted. - -This is possible with the ``SoftDelete`` extension which can be found on `github`_. - -Installation ------------- - -First you just need to get the code by cloning the `github`_ repository: - -.. code-block:: console - - $ git clone git://github.com/doctrine/mongodb-odm-softdelete.git - -Now once you have the code you can setup the autoloader for it: - -.. code-block:: php - - register(); - -Setup ------ - -Now you can autoload the classes you need to setup the ``SoftDeleteManager`` instance you need to manage -the soft delete state of your documents: - -.. code-block:: php - - deletedAt; - } - - // ... - } - -Usage ------ - -Once you have the ``$sdm`` you can start managing the soft delete state of your documents: - -.. code-block:: php - - getRepository(User::class)->findOneBy(['username' => 'jwage']); - $fabpot = $dm->getRepository(User::class)->findOneBy(['username' => 'fabpot']); - $sdm->delete($jwage); - $sdm->delete($fabpot); - $sdm->flush(); - -The call to ``SoftDeleteManager#flush()`` would persist the deleted state to the database -for all the documents it knows about and run a query like the following: - -.. code-block:: javascript - - db.users.update({ _id : { $in : userIds }}, { $set : { deletedAt : new Date() } }) - -Now if we were to restore the documents: - -.. code-block:: php - - restore($jwage); - $sdm->flush(); - -It would execute a query like the following: - -.. code-block:: javascript - - db.users.update({ _id : { $in : userIds }}, { $unset : { deletedAt : true } }) - -Events ------- - -We trigger some additional lifecycle events when documents are soft deleted and restored: - -- Events::preSoftDelete -- Events::postSoftDelete -- Events::preRestore -- Events::postRestore - -Using the events is easy, just define a class like the following: - -.. code-block:: php - - getDocument(); - $sdm = $args->getSoftDeleteManager(); - } - - public function getSubscribedEvents(): array - { - return [Events::preSoftDelete]; - } - } - -Now we just need to add the event subscriber to the EventManager: - -.. code-block:: php - - addEventSubscriber($eventSubscriber); - -When we soft delete something the preSoftDelete() method will be invoked before any queries are sent -to the database: - -.. code-block:: php - - delete($fabpot); - $sdm->flush(); - -Cascading Soft Deletes ----------------------- - -You can easily implement cascading soft deletes by using events in a certain way. Imagine you have -a User and Post document and you want to soft delete a users posts when you delete him. - -You just need to setup an event listener like the following: - -.. code-block:: php - - getSoftDeleteManager(); - $document = $args->getDocument(); - if ($document instanceof User) { - $sdm->deleteBy(Post::class, ['user.id' => $document->getId()]); - } - } - - public function preRestore(LifecycleEventArgs $args): void - { - $sdm = $args->getSoftDeleteManager(); - $document = $args->getDocument(); - if ($document instanceof User) { - $sdm->restoreBy(Post::class, ['user.id' => $document->getId()]); - } - } - - public function getSubscribedEvents(): array - { - return [ - Events::preSoftDelete, - Events::preRestore - ]; - } - } - -Now when you delete an instance of User it will also delete any Post documents where they -reference the User being deleted. If you restore the User, his Post documents will also be restored. - -.. _github: https://github.com/doctrine/mongodb-odm-softdelete