-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Using Doctrine LifecycleCallbacks #1439
base: main
Are you sure you want to change the base?
Changes from 2 commits
41d6cfd
735ff5f
f2be38c
eb99b8d
1682875
2e28d19
b88a058
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,9 @@ | |
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Doctrine\ORM\Mapping\HasLifecycleCallbacks; | ||
use Doctrine\ORM\Mapping\PrePersist; | ||
use Doctrine\ORM\Mapping\PreUpdate; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
use function Symfony\Component\String\u; | ||
|
||
|
@@ -25,9 +28,11 @@ | |
* | ||
* @author Ryan Weaver <[email protected]> | ||
* @author Javier Eguiluz <[email protected]> | ||
* @author Mecanik <[email protected]> | ||
*/ | ||
#[ORM\Entity] | ||
#[ORM\Table(name: 'symfony_demo_comment')] | ||
#[HasLifecycleCallbacks] | ||
class Comment | ||
{ | ||
#[ORM\Id] | ||
|
@@ -51,6 +56,12 @@ | |
#[ORM\JoinColumn(nullable: false)] | ||
private ?User $author = null; | ||
|
||
#[ORM\Column(type: 'datetime_immutable')] | ||
private $createdAt; | ||
|
||
#[ORM\Column(type: 'datetime_immutable')] | ||
private $updatedAt; | ||
|
||
public function __construct() | ||
{ | ||
$this->publishedAt = new \DateTime(); | ||
|
@@ -108,4 +119,40 @@ | |
{ | ||
$this->post = $post; | ||
} | ||
|
||
public function getCreatedAt(): ?\DateTimeImmutable | ||
{ | ||
return $this->createdAt; | ||
} | ||
|
||
public function setCreatedAt(\DateTimeImmutable $createdAt): self | ||
{ | ||
$this->createdAt = $createdAt; | ||
|
||
return $this; | ||
} | ||
|
||
public function getUpdatedAt(): ?\DateTimeImmutable | ||
{ | ||
return $this->updatedAt; | ||
} | ||
|
||
public function setUpdatedAt(\DateTimeImmutable $updatedAt): self | ||
{ | ||
$this->updatedAt = $updatedAt; | ||
|
||
return $this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no reason to make those setters fluent. Other setters in the entity are not. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More over, there is not reason to add theses public method |
||
} | ||
|
||
#[PrePersist] | ||
#[PreUpdate] | ||
public function updatedTimestamps() | ||
{ | ||
if ($this->getUpdatedAt() == null) { | ||
$this->setUpdatedAt(new \DateTimeImmutable('now')); | ||
} | ||
if ($this->getCreatedAt() == null) { | ||
$this->setCreatedAt(new \DateTimeImmutable('now')); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,9 @@ | |
use Doctrine\Common\Collections\Collection; | ||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Doctrine\ORM\Mapping\HasLifecycleCallbacks; | ||
use Doctrine\ORM\Mapping\PrePersist; | ||
use Doctrine\ORM\Mapping\PreUpdate; | ||
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
|
@@ -30,10 +33,12 @@ | |
* @author Ryan Weaver <[email protected]> | ||
* @author Javier Eguiluz <[email protected]> | ||
* @author Yonel Ceruto <[email protected]> | ||
* @author Mecanik <[email protected]> | ||
*/ | ||
#[ORM\Entity(repositoryClass: PostRepository::class)] | ||
#[ORM\Table(name: 'symfony_demo_post')] | ||
#[UniqueEntity(fields: ['slug'], errorPath: 'title', message: 'post.slug_unique')] | ||
#[HasLifecycleCallbacks] | ||
class Post | ||
{ | ||
#[ORM\Id] | ||
|
@@ -64,7 +69,7 @@ | |
#[ORM\ManyToOne(targetEntity: User::class)] | ||
#[ORM\JoinColumn(nullable: false)] | ||
private ?User $author = null; | ||
|
||
/** | ||
* @var Collection<int, Comment> | ||
*/ | ||
|
@@ -81,6 +86,12 @@ | |
#[Assert\Count(max: 4, maxMessage: 'post.too_many_tags')] | ||
private Collection $tags; | ||
|
||
#[ORM\Column(type: 'datetime_immutable')] | ||
private $createdAt; | ||
|
||
#[ORM\Column(type: 'datetime_immutable')] | ||
private $updatedAt; | ||
|
||
public function __construct() | ||
{ | ||
$this->publishedAt = new \DateTime(); | ||
|
@@ -195,4 +206,40 @@ | |
{ | ||
return $this->tags; | ||
} | ||
|
||
public function getCreatedAt(): ?\DateTimeImmutable | ||
{ | ||
return $this->createdAt; | ||
} | ||
|
||
public function setCreatedAt(\DateTimeImmutable $createdAt): self | ||
{ | ||
$this->createdAt = $createdAt; | ||
|
||
return $this; | ||
} | ||
|
||
public function getUpdatedAt(): ?\DateTimeImmutable | ||
{ | ||
return $this->updatedAt; | ||
} | ||
|
||
public function setUpdatedAt(\DateTimeImmutable $updatedAt): self | ||
{ | ||
$this->updatedAt = $updatedAt; | ||
|
||
return $this; | ||
} | ||
|
||
#[PrePersist] | ||
#[PreUpdate] | ||
public function updatedTimestamps() | ||
{ | ||
if ($this->getUpdatedAt() == null) { | ||
$this->setUpdatedAt(new \DateTimeImmutable('now')); | ||
} | ||
if ($this->getCreatedAt() == null) { | ||
$this->setCreatedAt(new \DateTimeImmutable('now')); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,16 +13,21 @@ | |
|
||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Doctrine\ORM\Mapping\HasLifecycleCallbacks; | ||
use Doctrine\ORM\Mapping\PrePersist; | ||
use Doctrine\ORM\Mapping\PreUpdate; | ||
|
||
/** | ||
* Defines the properties of the Tag entity to represent the post tags. | ||
* | ||
* See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class | ||
* | ||
* @author Yonel Ceruto <[email protected]> | ||
* @author Mecanik <[email protected]> | ||
*/ | ||
#[ORM\Entity] | ||
#[ORM\Table(name: 'symfony_demo_tag')] | ||
#[HasLifecycleCallbacks] | ||
class Tag implements \JsonSerializable | ||
{ | ||
#[ORM\Id] | ||
|
@@ -33,6 +38,12 @@ | |
#[ORM\Column(type: Types::STRING, unique: true)] | ||
private readonly string $name; | ||
|
||
#[ORM\Column(type: 'datetime_immutable')] | ||
private $createdAt; | ||
|
||
#[ORM\Column(type: 'datetime_immutable')] | ||
private $updatedAt; | ||
|
||
public function __construct(string $name) | ||
{ | ||
$this->name = $name; | ||
|
@@ -61,4 +72,40 @@ | |
{ | ||
return $this->name; | ||
} | ||
|
||
public function getCreatedAt(): ?\DateTimeImmutable | ||
{ | ||
return $this->createdAt; | ||
} | ||
|
||
public function setCreatedAt(\DateTimeImmutable $createdAt): self | ||
{ | ||
$this->createdAt = $createdAt; | ||
|
||
return $this; | ||
} | ||
|
||
public function getUpdatedAt(): ?\DateTimeImmutable | ||
{ | ||
return $this->updatedAt; | ||
} | ||
|
||
public function setUpdatedAt(\DateTimeImmutable $updatedAt): self | ||
{ | ||
$this->updatedAt = $updatedAt; | ||
|
||
return $this; | ||
} | ||
|
||
#[PrePersist] | ||
#[PreUpdate] | ||
public function updatedTimestamps() | ||
{ | ||
if ($this->getUpdatedAt() == null) { | ||
$this->setUpdatedAt(new \DateTimeImmutable('now')); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The null check does not make sense here. With this logic, we will basically set the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. True, sorry as mentioned I copied from an older project that needed only one time to update. |
||
if ($this->getCreatedAt() == null) { | ||
$this->setCreatedAt(new \DateTimeImmutable('now')); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I just copied from an older project. The point was to demonstrate how it can be used and should be used in an official demo. I will update shortly. |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,9 @@ | |
use App\Repository\UserRepository; | ||
use Doctrine\DBAL\Types\Types; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Doctrine\ORM\Mapping\HasLifecycleCallbacks; | ||
use Doctrine\ORM\Mapping\PrePersist; | ||
use Doctrine\ORM\Mapping\PreUpdate; | ||
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; | ||
use Symfony\Component\Security\Core\User\UserInterface; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
@@ -27,9 +30,11 @@ | |
* | ||
* @author Ryan Weaver <[email protected]> | ||
* @author Javier Eguiluz <[email protected]> | ||
* @author Mecanik <[email protected]> | ||
*/ | ||
#[ORM\Entity(repositoryClass: UserRepository::class)] | ||
#[ORM\Table(name: 'symfony_demo_user')] | ||
#[HasLifecycleCallbacks] | ||
class User implements UserInterface, PasswordAuthenticatedUserInterface | ||
{ | ||
// We can use constants for roles to find usages all over the application rather | ||
|
@@ -59,6 +64,12 @@ | |
#[ORM\Column(type: Types::STRING)] | ||
private ?string $password = null; | ||
|
||
#[ORM\Column(type: 'datetime_immutable')] | ||
private $createdAt; | ||
|
||
#[ORM\Column(type: 'datetime_immutable')] | ||
private $updatedAt; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
|
@@ -180,4 +191,40 @@ | |
// add $this->salt too if you don't use Bcrypt or Argon2i | ||
[$this->id, $this->username, $this->password] = $data; | ||
} | ||
|
||
public function getCreatedAt(): ?\DateTimeImmutable | ||
{ | ||
return $this->createdAt; | ||
} | ||
|
||
public function setCreatedAt(\DateTimeImmutable $createdAt): self | ||
{ | ||
$this->createdAt = $createdAt; | ||
|
||
return $this; | ||
} | ||
|
||
public function getUpdatedAt(): ?\DateTimeImmutable | ||
{ | ||
return $this->updatedAt; | ||
} | ||
|
||
public function setUpdatedAt(\DateTimeImmutable $updatedAt): self | ||
{ | ||
$this->updatedAt = $updatedAt; | ||
|
||
return $this; | ||
} | ||
|
||
#[PrePersist] | ||
#[PreUpdate] | ||
public function updatedTimestamps() | ||
{ | ||
if ($this->getUpdatedAt() == null) { | ||
$this->setUpdatedAt(new \DateTimeImmutable('now')); | ||
} | ||
if ($this->getCreatedAt() == null) { | ||
$this->setCreatedAt(new \DateTimeImmutable('now')); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Our linters rightfully complain about missing property types. Please add them.