Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Last active September 22, 2022 16:19
Show Gist options
  • Save bwaidelich/9617211 to your computer and use it in GitHub Desktop.
Save bwaidelich/9617211 to your computer and use it in GitHub Desktop.
A simple example showing how doctrine behaviors (in this chase soft-deleteable) can be used within TYPO3 Flow.
{
...
"require": {
"typo3/flow": "2.0.*",
"gedmo/doctrine-extensions": "2.3.*"
},
...
}
TYPO3:
Flow:
# disable reflection for non psr-0 compliant 3rd party packages
object:
excludeClasses:
'gedmo.doctrineextensions' : ['Gedmo\\.*']
# register soft deletable filter & event listeners
persistence:
doctrine:
filters:
soft-deletable: 'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter'
eventListeners:
-
events: ['onFlush', 'loadClassMetadata']
listener: 'Gedmo\SoftDeleteable\SoftDeleteableListener'
<?php
namespace Your\Package\Domain\Model;
use TYPO3\Flow\Annotations as Flow;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Trait for soft-deletable entities.
* Usage:
*
* * Add @Gedmo\SoftDeleteable(fieldName="deletedAt") annotation to your entity
* * add "use SoftDeletableTrait;" to your entity
*/
trait SoftDeletableTrait {
/**
* DateTime when this entity was deleted (used for "Soft-Delete behaviour")
*
* @var \DateTime
* @ORM\Column(nullable=true)
*/
protected $deletedAt;
/**
* @return \DateTime
*/
public function getDeletedAt() {
return $this->deletedAt;
}
/**
* @param \DateTime $deletedAt
* @return void
*/
public function setDeletedAt(\DateTime $deletedAt) {
$this->deletedAt = $deletedAt;
}
}
<?php
namespace Your\Package\Domain\Model;
use TYPO3\Flow\Annotations as Flow;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* Some Entity
*
* @Flow\Entity
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
class SomeEntity {
use SoftDeletableTrait;
/** ..other properties ..*/
}
?>
@bwaidelich
Copy link
Author

NOTE: This currently requires the GIT master of the TYPO3.Flow package (or specifically this patch https://review.typo3.org/27755)

Thanks to christianjul and mneuhaus for your help!

@dogawaf
Copy link

dogawaf commented May 12, 2014

Hi

I can't get rid of this exception when I try to get this working:

Uncaught Exception
    [Semantical Error] The annotation "@Gedmo\Blameable" in property
    Gedmo\Blameable\Traits\BlameableDocument::$createdBy was never
  imported. Did you maybe forget to add a "use" statement for this annotation?

The setting TYPO3.Flow.object.excludeClasses does not work?
Thanks

@dogawaf
Copy link

dogawaf commented May 13, 2014

Found...
I had splitted the settings in two parts, and the first part was overriden by the second, so the excludeClass was not taken into account.

TYPO3:
  Flow:
    object:
      excludeClasses:
       'gedmo.doctrineextensions' : ['Gedmo\\.*']


 TYPO3:
  Flow:
    persistence:
      ...

@thicolares
Copy link

Worked like a charm! Thanks for sharing.

Unfortunately, my Flow version is yet outdate (shame on me), so I've done some extra patch on EntityManagerFactory (i.e.g: also added the buildEventManager).

@Wirone
Copy link

Wirone commented Feb 13, 2015

Anyone knows how to mix softdeleteable and blameable in proper way? I want to update deleted_by field when "removing" entity.

@KatharinaSt
Copy link

As I stumbled upon it:
Please notice the different spellings of "deleteable" and "deletable" (with/without additional "e") in the example. ;-)

@BossBele
Copy link

BossBele commented Sep 22, 2022

Adding @Gedmo\SoftDeleteable(fieldName="deletedAt") annotation to your entity to every entity is quite a hustle 😅

While using 'deletedAt' column name for soft deletion, one can use the SoftDeleteableEntity which doesn't require annotations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment