Skip to content

Instantly share code, notes, and snippets.

@mneuhaus
Created August 5, 2014 15:08
Show Gist options
  • Save mneuhaus/a7780937835c668a583d to your computer and use it in GitHub Desktop.
Save mneuhaus/a7780937835c668a583d to your computer and use it in GitHub Desktop.
GenerateValueListener
<?php
namespace Famelo\Saas\Domain\Model;
use Doctrine\ORM\Mapping as ORM;
use TYPO3\Flow\Annotations as Flow;
/**
* @Flow\Entity
*/
class Example {
/**
* @var integer
* @Saas\GenerateValue(generator="integer", start="0", increment="1")
*/
protected $number;
}
<?php
namespace Famelo\Saas\Annotations;
/* *
* This script belongs to the TYPO3 Flow framework. *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
* @Annotation
* @Target("PROPERTY")
*/
final class GenerateValue {
/**
* @var string
*/
public $options;
/**
* @param array $values
*/
public function __construct(array $values) {
$this->options = $values;
}
}
<?php
namespace Famelo\Saas\Domain\Listener;
use Doctrine\Common\EventArgs;
use Doctrine\Common\EventSubscriber;
use Doctrine\Common\Persistence\ObjectManager;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Reflection\ObjectAccess;
use TYPO3\Flow\Reflection\ReflectionService;
use TYPO3\Flow\Security\Context;
class GenerateValueListener implements EventSubscriber {
/**
* @Flow\Inject
* @var ReflectionService
*/
protected $reflectionService;
/**
* {@inheritdoc}
*/
public function getSubscribedEvents() {
return array(
'onFlush'
);
}
/**
* If it's a SoftDeleteable object, update the "deletedAt" field
* and skip the removal of the object
*
* @param EventArgs $args
* @return void
*/
public function onFlush(EventArgs $args) {
$entityManager = $args->getEntityManager();
$unitOfWork = $entityManager->getUnitOfWork();
foreach ($unitOfWork->getScheduledEntityInsertions() as $entity) {
$className = get_class($entity);
$propertyNames = $this->reflectionService->getPropertyNamesByAnnotation($className, 'Famelo\Saas\Annotations\GenerateValue');
foreach ($propertyNames as $propertyName) {
$annotation = $this->reflectionService->getPropertyAnnotation($className, $propertyName, 'Famelo\Saas\Annotations\GenerateValue');
$options = $annotation->options;
switch ($options['generator']) {
case 'integer':
$query = $entityManager->createQuery('SELECT i FROM ' . $className . ' i ORDER BY i.number DESC');
$query->setMaxResults(1);
$results = $query->getResult();
if (count($results) === 1) {
$result = current($results);
$generatedValue = ObjectAccess::getProperty($result, $propertyName);
$generatedValue+= $options['increment'];
} else {
$generatedValue = $options['start'];
}
ObjectAccess::setProperty($entity, $propertyName, $generatedValue);
$unitOfWork->propertyChanged($entity, $propertyName, NULL, $generatedValue);
break;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment