Skip to content

Instantly share code, notes, and snippets.

@muzfr7
Last active February 8, 2024 09:14
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save muzfr7/7ec1b726702e3d353e83246dce22eab3 to your computer and use it in GitHub Desktop.
Save muzfr7/7ec1b726702e3d353e83246dce22eab3 to your computer and use it in GitHub Desktop.
Timestampable Trait for doctrine entities to use in order to not to repeat them in individual entities.
<?php
namespace AppBundle\Entity\Traits;
use Doctrine\ORM\Mapping as ORM;
/**
* Adds created at and updated at timestamps to entities.
* Entities using this must have HasLifecycleCallbacks annotation.
*
* @ORM\HasLifecycleCallbacks
*/
trait Timestampable
{
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var \DateTime
*
* @ORM\Column(name="updated_at", type="datetime")
*/
private $updatedAt;
/**
* Gets triggered only on insert
*
* @ORM\PrePersist
*/
public function onPrePersist()
{
$this->createdAt = new \DateTime();
$this->updatedAt = new \DateTime();
}
/**
* Gets triggered every time on update
*
* @ORM\PreUpdate
*/
public function onPreUpdate()
{
$this->updatedAt = new \DateTime();
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
*/
public function setCreatedAt($createdAt)
{
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
*/
public function setUpdatedAt($updatedAt)
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updatedAt;
}
}
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use AppBundle\Entity\Traits\Timestampable;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*
* @ORM\HasLifecycleCallbacks
*/
class User
{
/*
* Timestampable trait
*/
use Timestampable;
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="firstname", type="string", length=64)
*/
private $firstname;
/**
* @var string
*
* @ORM\Column(name="lastname", type="string", length=64)
*/
private $lastname;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=255, unique=true)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=64)
*/
private $password;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set firstname
*
* @param string $firstname
*
* @return User
*/
public function setFirstname($firstname)
{
$this->firstname = $firstname;
return $this;
}
/**
* Get firstname
*
* @return string
*/
public function getFirstname()
{
return $this->firstname;
}
/**
* Set lastname
*
* @param string $lastname
*
* @return User
*/
public function setLastname($lastname)
{
$this->lastname = $lastname;
return $this;
}
/**
* Get lastname
*
* @return string
*/
public function getLastname()
{
return $this->lastname;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
}
@Tofandel
Copy link

Tofandel commented Feb 8, 2022

The #[ORM\HasLifecycleCallbacks] is required on all classes using the trait for it to work

@e-vural
Copy link

e-vural commented Nov 14, 2022

php 8 version not working? any solution?

@djo57
Copy link

djo57 commented Feb 8, 2024

Maybe you need to use attributes in php8 :

<?php

namespace App\Entity\Traits;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\HasLifecycleCallbacks]
trait Timestampable
{
    #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $createdAt = null;

    #[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
    private ?\DateTimeInterface $updatedAt = null;

    #[ORM\PrePersist]
    public function onPrePersist(): never
    {
        $this->createdAt = new \DateTime();
        $this->updatedAt = new \DateTime();
    }

    #[ORM\PreUpdate]
    public function onPreUpdate(): never
    {
        $this->updatedAt = new \DateTime();
    }

    /**
     * @param \DateTimeInterface $createdAt
     * @return static
     */
    public function setCreatedAt(\DateTimeInterface $createdAt): static
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    /**
     * @return \DateTimeInterface
     */
    public function getCreatedAt(): \DateTimeInterface
    {
        return $this->createdAt;
    }

    /**
     * @param \DateTimeInterface $updatedAt
     * @return static
     */
    public function setUpdatedAt(\DateTimeInterface $updatedAt): static
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * @return \DateTimeInterface
     */
    public function getUpdatedAt(): \DateTimeInterface
    {
        return $this->updatedAt;
    }
}

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