Last active
August 27, 2019 15:09
-
-
Save guillaumedev/c9da569189ff91c15a59d9b296a8d607 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 <lelle.daniele@gmail.com> | |
* @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 <lelle.daniele@gmail.com> | |
*/ | |
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); | |
/** | |
* Get updatedAt | |
* | |
* @param DateTime $createdAt | |
* @return self | |
*/ | |
public function getUpdatedAt(): ?DateTime; | |
/** | |
* Set updatedAt | |
* | |
* @param DateTime $updatedAt | |
* @return self | |
*/ | |
public function setUpdatedAt(DateTime $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 | |
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
Il suffit d'ajouter use AppBundle\Mapping\EntityBase; dans l'entité et le gestionnaire de migrations se chargera du reste.