Created
September 16, 2013 03:15
-
-
Save merk/6576376 to your computer and use it in GitHub Desktop.
Timestampable trait
This file contains hidden or 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 | |
/** | |
* 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