Skip to content

Instantly share code, notes, and snippets.

@cage-is
Last active January 30, 2019 20:55
Show Gist options
  • Select an option

  • Save cage-is/bdb893530d0c69a64952cee5e8b0841c to your computer and use it in GitHub Desktop.

Select an option

Save cage-is/bdb893530d0c69a64952cee5e8b0841c to your computer and use it in GitHub Desktop.

Create the Migration

<?php
public function change()
{
    $this->table('table_name')->addColumn('deleted_at', 'datetime', ['null' => true])->save();
}

Add the Listener

  gedmo.listener.deleteable:
    class: Gedmo\SoftDeleteable\SoftDeleteableListener
    tags:
      - { name: doctrine.event_subscriber, connection: default }
    calls:
      - [ setAnnotationReader, [ '@annotation_reader' ] ]

Add the Filter

Adding the filter allows you to show or hide deleted records more easily. To use the filter by default, we use the enabled: true part.

  orm:
    # ...
    filters:
      deletableFilter:
        class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
        enabled: true

Add the Annotations to the Entity

<?php

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;

/**
 * @ORM\Table()
 * @ORM\Entity
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 */
class TableName
{
    use SoftDeleteableEntity;
    # ...
}

Remove the Filter (Optional)

Sometimes you may need to access the deleted rows as well. To disable the filter for your query, use this following:

<?php
$em->getFilters()->disable('deletableFilter');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment