StofDoctrineExtensionsBundle softdelete - How do I use it?
Asked Answered
P

2

13

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

Peggiepeggir answered 21/5, 2013 at 12:50 Comment(1)
The documentation is not beyond sparse. StofDoctrineExtensionsBundle is only the integration of the Doctrine Extenssions for Symfony. So look there for the softdeleteable documentationAvouch
C
47

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();
Chinook answered 21/5, 2013 at 13:21 Comment(2)
Thank you very much. They did not put those instructions in the docs!Epicycle
I have a related question: Once you remove an entity, it gets a deletedAt timestamp and it doesn't show up in a normal find query, which is fine. But, what if I want to access the deleted timestamp for some other use? Is manually disabling the filter, getting the data, and re-enabling it the only option? Seems a little dirty and possibly unsafe.Redhead
F
2

I also needed another puzzle part: The doctrine yaml config:

XYBundle\Entity\Adresse:
type: entity
table: adresse

gedmo:
  soft_deleteable:
    field_name: deleted_at
    time_aware: false


id:
    id:
        type: integer
        generator: { strategy: AUTO }


fields:
    ort:
        type: string
        length: 100
    plz:
        type: string
        columnDefinition: varchar(255) NOT NULL DEFAULT ''

    deleted_at:
      type: datetime
      nullable: true
Flywheel answered 15/11, 2016 at 21:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.