Skip to content

Instantly share code, notes, and snippets.

@lelledaniele
Last active June 4, 2023 19:44
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save lelledaniele/be67e03b51e04ab9f9b04b985ccd94e2 to your computer and use it in GitHub Desktop.
Save lelledaniele/be67e03b51e04ab9f9b04b985ccd94e2 to your computer and use it in GitHub Desktop.
symfony doctrine updatedAt createdAt updated_at created_at fields timestamp
<?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;
}
}
<?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): 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
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Mapping\EntityBase;
/**
* MyEntity
*
* @ORM\HasLifecycleCallbacks
*/
class MyEntity extends EntityBase
{
// ...
}
@hubertperron
Copy link

Simple and effective.

There's some missing semicolon on the return type of most of the methods in the interface and the self return type should be removed.

@nrambeck
Copy link

nrambeck commented Jul 2, 2020

Also, the DoctrineExtensions bundle has a Trait that will add the same thing to an entity with just a couple lines of code. Documentation is confusing, but if you add the 2 use statements in the linked example and configure the timestampable property in the DoctrineExtensions configuration file, you should be good to go.

https://github.com/Atlantic18/DoctrineExtensions/blob/v2.4.x/doc/timestampable.md#traits

@jeandonaldroselin
Copy link

@lelledaniele your tip is just awesome.
Thank you !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment