Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Created October 19, 2012 11:13
Show Gist options
  • Save bwaidelich/3917639 to your computer and use it in GitHub Desktop.
Save bwaidelich/3917639 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@bwaidelich
Copy link
Author

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
Copy link

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
Copy link
Author

..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