Last active
May 20, 2024 18:06
-
-
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.
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\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; | |
} | |
} |
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\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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Maybe you need to use attributes in php8 :