Major Productions LLC Major Productions LLC - 18 days ago 6
PHP Question

StofDoctrineExtensionsBundle softdelete - How do I use it?

My boss installed this bundle for the softdelete filter, but the documentation is beyond sparse. How do I use this in my delete queries?

Answer

Enable it in your config:

stof_doctrine_extensions:
    orm:
        default:
            ...
            softdeleteable: true

doctrine:
    ...
    orm:
        filters:
            softdeleteable:
                class: Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter
                enabled: true

Then in your entity:

<?php

namespace Foo\BarBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * ...
 * @Gedmo\SoftDeleteable(fieldName="deletedAt")
 * @ORM\Entity
 */
class Foo
{
    /**
     * @var \DateTime $deletedAt
     *
     * @ORM\Column(name="deleted_at", type="datetime", nullable=true)
     */
    private $deletedAt;

Then just delete entities like you normally would (the extension takes care of the rest):

    $em = $this->getDoctrine()->getManager();
    $em->remove($entity);
    $em->flush();
Comments