Skip to content

Instantly share code, notes, and snippets.

@merk
Created September 16, 2013 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merk/6576376 to your computer and use it in GitHub Desktop.
Save merk/6576376 to your computer and use it in GitHub Desktop.
Timestampable trait
<?php
/**
* This file is part of the Infinite Helper Library
*
* (c) Infinite Networks Pty Ltd <http://www.infinite.net.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Infinite\Helper\Doctrine;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serialize;
/**
* Adds created at and updated at timestamps to entities.
*/
trait Timestampable
{
/**
* @ORM\Column(type="datetime")
* @Serialize\Expose
* @Serialize\Type("DateTime")
* @Serialize\XmlAttribute
* @var \DateTime
*/
protected $dateAdded;
/**
* @ORM\Column(type="datetime")
* @Serialize\Expose
* @Serialize\Type("DateTime")
* @Serialize\XmlAttribute
* @var \DateTime
*/
protected $dateUpdated;
/**
* @return \DateTime
*/
public function getDateAdded()
{
return $this->dateAdded;
}
/**
* @return \DateTime
*/
public function getDateUpdated()
{
return $this->dateUpdated;
}
/**
* @ORM\PrePersist
*/
public function _timestampable_prePersist()
{
$this->dateAdded = new \DateTime;
$this->dateUpdated = new \DateTime;
}
/**
* @ORM\PreUpdate
*/
public function _timestampable_preUpdate()
{
$this->dateUpdated = new \DateTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment