Last active
August 29, 2015 13:56
-
-
Save cifren/8945956 to your computer and use it in GitHub Desktop.
EntityStamp listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Ali Tiane | |
* | |
*/ | |
// src/App/OneBundle/Model/EntityStamp.php | |
namespace App\OneBundle\Model; | |
use Doctrine\ORM\Mapping as ORM; | |
class EntityStamp | |
{ | |
/* Attributes */ | |
/** | |
* @var datetime createdAt | |
* | |
* @ORM\Column(name="created_at", type="datetime", nullable=true) | |
*/ | |
protected $createdAt; | |
/** | |
* @var datetime updatedAt | |
* | |
* @ORM\Column(name="updated_at", type="datetime", nullable=true) | |
*/ | |
protected $updatedAt; | |
/** | |
* @var User createdBy | |
* @ORM\ManyToOne(targetEntity="User") | |
* @ORM\JoinColumn(name="created_by", referencedColumnName="id", nullable=true) | |
*/ | |
protected $createdBy; | |
/** | |
* @var User updatedBy | |
* @ORM\ManyToOne(targetEntity="User") | |
* @ORM\JoinColumn(name="updated_by", referencedColumnName="id", nullable=true) | |
*/ | |
protected $updatedBy; | |
/* Methods */ | |
public function setCreatedBy($user) | |
{ | |
$this->createdBy = $user; | |
} | |
public function getCreatedBy() | |
{ | |
return $this->createdBy; | |
} | |
public function setUpdatedBy($user) | |
{ | |
$this->updatedBy = $user; | |
} | |
public function getUpdatedBy() | |
{ | |
return $this->updatedBy; | |
} | |
public function setCreatedAt() | |
{ | |
$this->createdAt = new \DateTime("now"); | |
} | |
public function getCreatedAt() | |
{ | |
return $this->createdAt; | |
} | |
public function setUpdatedAt() | |
{ | |
$this->updatedAt = new \DateTime("now"); | |
} | |
public function getUpdatedAt() | |
{ | |
return $this->updatedAt; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Ali Tiane | |
* | |
*/ | |
// src/App/OneBundle/Listener/EntityStampListener.php | |
namespace App\OneBundle\Listener; | |
use Doctrine\ORM\Event\PreUpdateEventArgs; | |
use Doctrine\ORM\Event\LifecycleEventArgs; | |
/** | |
* App\OneBundle\Listener\EntityStampListener | |
*/ | |
class EntityStampListener | |
{ | |
const Create = "Created"; | |
const Update = "Updated"; | |
protected $container; | |
public function __construct($container) | |
{ | |
$this->container = $container; | |
} | |
protected function getContainer() | |
{ | |
return $this->container; | |
} | |
protected static function doAt($entity, $action) | |
{ | |
if (method_exists($entity, "set{$action}At")) { | |
$entity->{"set{$action}At"}(); | |
} | |
} | |
protected static function doBy($entity, $action, $user) | |
{ | |
if (method_exists($entity, "set{$action}By")) { | |
$entity->{"set{$action}By"}($user); | |
} | |
} | |
protected function getUser($em) | |
{ | |
if ($this->getContainer()->get('security.context')->getToken() != null) { | |
// Service security.context in container | |
$user = $this->getContainer()->get('security.context')->getToken()->getUser(); | |
return $em->getRepository('User')->find($user->getId()); | |
} | |
return null; | |
} | |
public function preUpdate(PreUpdateEventArgs $args) | |
{ | |
$entity = $args->getEntity(); | |
$em = $args->getEntityManager(); | |
EntityStampListener::doAt($entity, self::Update); | |
EntityStampListener::doBy($entity, self::Update, $this->getUser($em)); | |
// Recompute setUpdatedAt changes in the ChangeSet (basically a Symfony2 hack) | |
$metadata = $em->getClassMetadata(get_class($entity)); | |
$em->getUnitOfWork()->recomputeSingleEntityChangeSet($metadata, $entity); | |
} | |
public function prePersist(LifecycleEventArgs $args) | |
{ | |
$entity = $args->getEntity(); | |
$em = $args->getEntityManager(); | |
EntityStampListener::doAt($entity, self::Create); | |
EntityStampListener::doAt($entity, self::Update); | |
$user = $this->getUser($em); | |
EntityStampListener::doBy($entity, self::Create, $user); | |
EntityStampListener::doBy($entity, self::Update, $user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services: | |
entitystamplistener.listener: | |
class: App\OneBundle\Listener\EntityStampListener | |
arguments: ["@service_container"] | |
tags: | |
- { name: doctrine.event_listener, event: prePersist, method: prePersist } | |
- { name: doctrine.event_listener, event: preUpdate, method: preUpdate } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Ali Tiane | |
* | |
*/ | |
// src/App/OneBundle/Entity/User.php | |
namespace App\OneBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use App/OneBundle/Model/EntityStamp; | |
/** | |
* App\OneBundle\Entity\User | |
* | |
* @ORM\Table | |
* @ORM\Entity | |
*/ | |
class User extends EntityStamp | |
{ | |
/** | |
* @ORM\Id | |
* @ORM\Column(type="integer") | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
protected $id; | |
/** | |
* @ORM\Column(name="username", type="text") | |
*/ | |
protected $username; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment