dmartinezla
8/6/2015 - 7:50 AM

Create custom annotations. https://www.scandio.de/2012/02/symfony2-custom-annotations/

services:
    created_at_listener:
        class: Scandio\Annotations\Driver\CreatedAtDriver
        tags:
          - {name: doctrine.event_listener, event: prePersist}
        arguments: [@annotation_reader]
<?php
// .../vendor/scandio/lib/Scandio/Annotations/Driver/CreatedAt.php
namespace Scandio\Annotations\Driver;
 
use Scandio\Annotations\CreatedAt;
 
use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\Event\LifecycleEventArgs;
 
class CreatedAtDriver
{
    private $reader;
 
    public function __construct(Reader $reader)
    {
        // initialise Doctrine Reader
        $this->reader = $reader;//get annotations reader
    }
 
    public function prePersist(LifecycleEventArgs $event)
    {
        // get article
        $entity = $event->getEntity();
 
        // get Reflection of Class
        $reflectionProperties = new \ReflectionClass($entity);
        // get all properties with their reflections
        $properties = $reflectionProperties->getProperties();
 
        $annotation = false;
        $property = false;
        // iterate over all properties to check for the @CreatedAt-Annotation
        foreach ($properties as $prop) {
            $annotation = $this->reader->getPropertyAnnotation(
                $prop,
                'Scandio\Annotations\CreatedAt'
            );
            // get the name of the property/field
            if (!empty($annotation)) {
                $property = $prop->getName();
            }
        }
 
        if (!empty($property)) {
            // getCreatedAt and setCreatedAt
            $setMethod = 'set'.ucFirst($property);
            $getMethod = 'get'.ucFirst($property);
            if (method_exists($entity, $setMethod) && method_exists($entity, $getMethod)) {
                // add the current date if not available
                $date = $entity->{$getMethod}();
                if (empty($date)) {
                    $entity->{$setMethod}(new \DateTime());
                }
            }
        }
    }
}
<?php
// .../vendor/scandio/lib/Scandio/Annotations/CreatedAt.php
 
namespace Scandio\Annotations;
 
use Doctrine\Common\Annotations\Annotation;
 
/**
 * Annotation
 */
class CreatedAt extends Annotation
{
   public $type = 'datetime';
}
<?php
 
namespace Scandio\PlaygroundBundle\Entity;
 
use Doctrine\ORM\Mapping as ORM;
use Scandio\Annotations as ScandioAnnotation;
 
/**
* Scandio\PlaygroundBundle\Entity\Article
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Scandio\PlaygroundBundle\Entity\ArticleRepository")
*/
class Article
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;
 
    /**
     * @var string $title
     *
     * @ORM\Column(name="title", type="string", length=255)
     */
    private $title;
 
    /**
     * @var string $description
     *
     * @ORM\Column(name="description", type="string", length=255)
     */
    private $description;
 
    /**
     * @var datetime $createdAt
     *
     * @ORM\Column(name="createdAt", type="datetime", nullable=true)
     * @ScandioAnnotation\CreatedAt()
     */
    private $createdAt;
 
    // ...
}