Created
December 3, 2017 22:00
-
-
Save culttm/6226333300cabe478f2729d2e2cddb3b to your computer and use it in GitHub Desktop.
symfony doctrine updatedAt createdAt updated_at created_at fields timestamp
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 AppBundle\Mapping; | |
use Doctrine\ORM\Mapping as ORM; | |
use DateTime; | |
/** | |
* Class EntityBase | |
* | |
* PHP version 7.1 | |
* | |
* LICENSE: MIT | |
* | |
* @package AppBundle\Mapping | |
* @author Lelle - Daniele Rostellato <[email protected]> | |
* @license MIT | |
* @version 1.0.0 | |
* @since File available since Release 1.0.0 | |
* @ORM\HasLifecycleCallbacks | |
*/ | |
class EntityBase implements EntityBaseInterface | |
{ | |
/** | |
* @var DateTime $created | |
* | |
* @ORM\Column(name="created_at", type="datetime", nullable=false) | |
*/ | |
protected $createdAt; | |
/** | |
* @var DateTime $updated | |
* | |
* @ORM\Column(name="updated_at", type="datetime", nullable=false) | |
*/ | |
protected $updatedAt; | |
/** | |
* @ORM\PrePersist | |
* @ORM\PreUpdate | |
*/ | |
public function updatedTimestamps(): void | |
{ | |
$dateTimeNow = new DateTime('now'); | |
$this->setUpdatedAt($dateTimeNow); | |
if ($this->getCreatedAt() === null) { | |
$this->setCreatedAt($dateTimeNow); | |
} | |
} | |
public function getCreatedAt() :?DateTime | |
{ | |
return $this->createdAt; | |
} | |
public function setCreatedAt(DateTime $createdAt): self | |
{ | |
$this->createdAt = $createdAt; | |
return $this; | |
} | |
public function getUpdatedAt() :?DateTime | |
{ | |
return $this->updatedAt; | |
} | |
public function setUpdatedAt(DateTime $updatedAt): self | |
{ | |
$this->updatedAt = $updatedAt; | |
return $this; | |
} | |
} |
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 AppBundle\Mapping; | |
use Doctrine\ORM\Mapping as ORM; | |
use DateTime; | |
/** | |
* EntityBase Interface | |
* | |
* @author Lelle - Daniele Rostellato <[email protected]> | |
*/ | |
interface EntityBaseInterface | |
{ | |
/** | |
* @ORM\PrePersist | |
* @ORM\PreUpdate | |
*/ | |
public function updatedTimestamps(): void; | |
/** | |
* Get createdAt | |
* | |
* @return null|DateTime | |
*/ | |
public function getCreatedAt(): ?DateTime | |
/** | |
* Set createdAt | |
* | |
* @param DateTime $createdAt | |
* @return self | |
*/ | |
public function setCreatedAt(DateTime $createdAt): self | |
/** | |
* Get updatedAt | |
* | |
* @param DateTime $createdAt | |
* @return self | |
*/ | |
public function getUpdatedAt(): ?DateTime | |
/** | |
* Set updatedAt | |
* | |
* @param DateTime $updatedAt | |
* @return self | |
*/ | |
public function setUpdatedAt(DateTime $updatedAt): self | |
} |
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 AppBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
use AppBundle\Mapping\EntityBase; | |
/** | |
* MyEntity | |
* | |
* @ORM\HasLifecycleCallbacks | |
*/ | |
class MyEntity extends EntityBase | |
{ | |
// ... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment