Created
November 25, 2018 17:46
-
-
Save ArchTaqi/260791d9e88192174f50f3a0319b4c4f to your computer and use it in GitHub Desktop.
Doctrine 2 Timestamps Trait
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 | |
namespace App\Traits; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* Trait TimeStampTrait | |
* @package App\Traits | |
*/ | |
trait TimestampsTrait | |
{ | |
/** | |
* @var \DateTime | |
* @ORM\Column(name="date_created", type="datetime") | |
*/ | |
private $dateCreated; | |
/** | |
* @var \DateTime | |
* @ORM\Column(name="date_updated", type="datetime") | |
*/ | |
private $dateUpdated; | |
###################################### | |
###### Getters Setters ###### | |
###################################### | |
/** | |
* @param \DateTime $dateCreated | |
* @return $this | |
*/ | |
public function setDateCreated($dateCreated) | |
{ | |
$this->dateCreated = $dateCreated; | |
return $this; | |
} | |
/** | |
* @return \DateTime | |
*/ | |
public function getDateCreated() | |
{ | |
return $this->dateCreated; | |
} | |
/** | |
* @param \DateTime $dateUpdated | |
* @return $this | |
*/ | |
public function setDateUpdated($dateUpdated) | |
{ | |
$this->dateUpdated = $dateUpdated; | |
return $this; | |
} | |
/** | |
* @return \DateTime | |
*/ | |
public function getDateUpdated() | |
{ | |
return $this->dateUpdated; | |
} | |
###################################### | |
###### Life Cycle Callbacks ###### | |
###################################### | |
/** | |
* @ORM\PrePersist() | |
*/ | |
public function prePersist() | |
{ | |
$this->dateCreated = new \DateTime(); | |
$this->preUpdate(); | |
} | |
/** | |
* @ORM\PreUpdate() | |
*/ | |
public function preUpdate() | |
{ | |
$this->dateUpdated = new \DateTime(); | |
} | |
} |
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 | |
namespace App\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use App\Traits\TimestampsTrait; | |
/** | |
* @ORM\Table(name="tbl_users") | |
* @ORM\Entity(repositoryClass="App\Repository\UserRepository") | |
* @ORM\HasLifecycleCallbacks() | |
*/ | |
class User | |
{ | |
use TimestampsTrait; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment