for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* This file is part of the FreshTimestampableEntityBundle
*
* (c) Artem Henvald <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Fresh\TimestampableEntityBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Events;
use Fresh\TimestampableEntityBundle\Model\TimestampableInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* DoctrineEventSubscriber.
* @author Artem Henvald <[email protected]>
class DoctrineEventSubscriber implements EventSubscriberInterface
{
* {@inheritdoc}
public static function getSubscribedEvents(): array
return [
Events::prePersist => 'prePersist',
Events::preUpdate => 'preUpdate',
];
}
* @param LifecycleEventArgs $args
public function prePersist(LifecycleEventArgs $args): void
if ($args->getEntity() instanceof TimestampableInterface) {
$args->getEntity()->setCreatedAt(new \DateTimeImmutable('now'))
->setUpdatedAt(new \DateTime('now'));
public function preUpdate(LifecycleEventArgs $args): void
$args->getEntity()->setUpdatedAt(new \DateTime('now'));