Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Doctrine behaviors in TYPO3 Flow - Example: Softdeletable
{
"name": "your/package",
/* ... */
"require": {
"gedmo/doctrine-extensions": "2.3.x-dev"
},
}
<?php
namespace YourPackage\Aop;
use TYPO3\Flow\Annotations as Flow;
/**
* @Flow\Aspect
*/
class EntityManagerFactoryAspect {
/**
* @var \TYPO3\Flow\Reflection\ReflectionService
* @Flow\Inject
*/
protected $reflectionService;
/**
* @Flow\Around("method(TYPO3\Flow\Persistence\Doctrine\EntityManagerFactory->create())")
* @param \TYPO3\Flow\Aop\JoinPointInterface $joinPoint The current join point
* @return \Doctrine\ORM\EntityManager
*/
public function registerSoftDeletableFilter(\TYPO3\Flow\Aop\JoinPointInterface $joinPoint) {
/** @var $entityManager \Doctrine\ORM\EntityManager */
$entityManager = $joinPoint->getAdviceChain()->proceed($joinPoint);
/** @var $eventManager \Doctrine\Common\EventManager */
$eventManager = $entityManager->getEventManager();
$classNamesAnnotatedAsDeletable = $this->reflectionService->getClassNamesByAnnotation('Gedmo\Mapping\Annotation\SoftDeleteable');
if (!is_array($classNamesAnnotatedAsDeletable)) {
return $entityManager;
}
foreach ($classNamesAnnotatedAsDeletable as $index => $className) {
if (!$this->reflectionService->isClassAnnotatedWith($className, 'TYPO3\Flow\Annotations\Entity')) {
unset($classNamesAnnotatedAsDeletable[$index]);
}
}
if ($classNamesAnnotatedAsDeletable !== array()) {
$doctrineConfiguration = $entityManager->getConfiguration();
$doctrineConfiguration->addFilter('soft-deletable', 'Gedmo\SoftDeleteable\Filter\SoftDeleteableFilter');
$entityManager->getFilters()->enable('soft-deletable');
$softDeletableListener = new \Gedmo\SoftDeleteable\SoftDeleteableListener();
$eventManager->addEventSubscriber($softDeletableListener);
}
return $entityManager;
}
}
?>
<?php
namespace YourPackage\Domain\Model;
use TYPO3\Flow\Annotations as Flow;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @Flow\Entity
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
*/
class SomeModel {
/**
* @var \DateTime
* @ORM\Column(nullable=true)
*/
protected $deletedAt;
/**
* @param \DateTime $deletedAt
* @return void
*/
public function setDeletedAt(\DateTime $deletedAt) {
$this->deletedAt= $deletedAt;
}
/**
* @return \DateTime
*/
public function getDeletedAt() {
return $this->deletedAt;
}
/** ... */
}
@bwaidelich

This comment has been minimized.

Copy link
Owner Author

@bwaidelich bwaidelich commented Oct 19, 2012

Note: AOP code shamelessly nicked from https://github.com/radmiraal/TYPO3.Doctrine.Versionable

@bwaidelich

This comment has been minimized.

Copy link
Owner Author

@bwaidelich bwaidelich commented Oct 19, 2012

Note²: Using AOP for this is just a temporary work around of course. We want to make this much easier in Flow 2.0 - Also you won't have to define the "deletedAt" property in your model then

@fsuter

This comment has been minimized.

Copy link

@fsuter fsuter commented Sep 2, 2013

Was this indeed implemented in Flow 2.0? I'm not sure where to start looking in the code if it exists.

@bwaidelich

This comment has been minimized.

Copy link
Owner Author

@bwaidelich bwaidelich commented Apr 10, 2014

..Late response, sorry for that: It didn't make it into 2.0, but in the current version doctrine filters are configurable via settings: http://docs.typo3.org/flow/TYPO3FlowDocumentation/latest/TheDefinitiveGuide/PartIII/Persistence.html#on-the-doctrine-filter-system

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