Skip to content
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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions src/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -25,9 +28,11 @@
*
* @author Ryan Weaver <weaverryan@gmail.com>
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Mecanik <contact@mecanik.dev>
*/
#[ORM\Entity]
#[ORM\Table(name: 'symfony_demo_comment')]
#[HasLifecycleCallbacks]
class Comment
{
#[ORM\Id]
Expand All @@ -51,6 +56,12 @@
#[ORM\JoinColumn(nullable: false)]
private ?User $author = null;

#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think that we want to use the timezone-aware datetime field here.

private $createdAt;

Check failure on line 60 in src/Entity/Comment.php

View workflow job for this annotation

GitHub Actions / Linters (8.1)

Property App\Entity\Comment::$createdAt has no type specified.
Copy link
Member

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.


#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE)]
private $updatedAt;

Check failure on line 63 in src/Entity/Comment.php

View workflow job for this annotation

GitHub Actions / Linters (8.1)

Property App\Entity\Comment::$updatedAt has no type specified.

public function __construct()
{
$this->publishedAt = new \DateTime();
Expand Down Expand Up @@ -108,4 +119,46 @@
{
$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;
Copy link
Member

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More over, there is not reason to add theses public method

}

#[PrePersist]

Check failure on line 147 in src/Entity/Comment.php

View workflow job for this annotation

GitHub Actions / Linters (8.1)

Method App\Entity\Comment::onPrePersist() has no return type specified.
public function onPrePersist()
{
if (null === $this->getCreatedAt()) {
$this->setCreatedAt(new \DateTimeImmutable('now'));
}

if ($this->getUpdatedAt() == null) {
$this->setUpdatedAt(new \DateTimeImmutable('now'));
}
}

#[PreUpdate]

Check failure on line 159 in src/Entity/Comment.php

View workflow job for this annotation

GitHub Actions / Linters (8.1)

Method App\Entity\Comment::onPreUpdate() has no return type specified.
public function onPreUpdate()
{
$this->setUpdatedAt(new \DateTimeImmutable('now'));
}
}
53 changes: 53 additions & 0 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,10 +33,12 @@
* @author Ryan Weaver <weaverryan@gmail.com>
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Yonel Ceruto <yonelceruto@gmail.com>
* @author Mecanik <contact@mecanik.dev>
*/
#[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]
Expand Down Expand Up @@ -81,6 +86,12 @@
#[Assert\Count(max: 4, maxMessage: 'post.too_many_tags')]
private Collection $tags;

#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE)]
private $createdAt;

Check failure on line 90 in src/Entity/Post.php

View workflow job for this annotation

GitHub Actions / Linters (8.1)

Property App\Entity\Post::$createdAt has no type specified.

#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE)]
private $updatedAt;

Check failure on line 93 in src/Entity/Post.php

View workflow job for this annotation

GitHub Actions / Linters (8.1)

Property App\Entity\Post::$updatedAt has no type specified.

public function __construct()
{
$this->publishedAt = new \DateTime();
Expand Down Expand Up @@ -195,4 +206,46 @@
{
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]

Check failure on line 234 in src/Entity/Post.php

View workflow job for this annotation

GitHub Actions / Linters (8.1)

Method App\Entity\Post::onPrePersist() has no return type specified.
public function onPrePersist()
{
if (null === $this->getCreatedAt()) {
$this->setCreatedAt(new \DateTimeImmutable('now'));
}

if ($this->getUpdatedAt() == null) {
$this->setUpdatedAt(new \DateTimeImmutable('now'));
}
}

#[PreUpdate]

Check failure on line 246 in src/Entity/Post.php

View workflow job for this annotation

GitHub Actions / Linters (8.1)

Method App\Entity\Post::onPreUpdate() has no return type specified.
public function onPreUpdate()
{
$this->setUpdatedAt(new \DateTimeImmutable('now'));
}
}
53 changes: 53 additions & 0 deletions src/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <yonelceruto@gmail.com>
* @author Mecanik <contact@mecanik.dev>
*/
#[ORM\Entity]
#[ORM\Table(name: 'symfony_demo_tag')]
#[HasLifecycleCallbacks]
class Tag implements \JsonSerializable
{
#[ORM\Id]
Expand All @@ -33,6 +38,12 @@
#[ORM\Column(type: Types::STRING, unique: true)]
private readonly string $name;

#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE)]
private $createdAt;

Check failure on line 42 in src/Entity/Tag.php

View workflow job for this annotation

GitHub Actions / Linters (8.1)

Property App\Entity\Tag::$createdAt has no type specified.

#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE)]
private $updatedAt;

Check failure on line 45 in src/Entity/Tag.php

View workflow job for this annotation

GitHub Actions / Linters (8.1)

Property App\Entity\Tag::$updatedAt has no type specified.

public function __construct(string $name)
{
$this->name = $name;
Expand Down Expand Up @@ -61,4 +72,46 @@
{
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]
public function onPrePersist()
{
if (null === $this->getCreatedAt()) {
$this->setCreatedAt(new \DateTimeImmutable('now'));
}

if ($this->getUpdatedAt() == null) {
$this->setUpdatedAt(new \DateTimeImmutable('now'));
}
}

#[PreUpdate]
public function onPreUpdate()
{
$this->setUpdatedAt(new \DateTimeImmutable('now'));
}
}
53 changes: 53 additions & 0 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,9 +30,11 @@
*
* @author Ryan Weaver <weaverryan@gmail.com>
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
* @author Mecanik <contact@mecanik.dev>
*/
#[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
Expand Down Expand Up @@ -59,6 +64,12 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(type: Types::STRING)]
private ?string $password = null;

#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE)]
private $createdAt;

#[ORM\Column(type: Types::DATETIMETZ_IMMUTABLE)]
private $updatedAt;

/**
* @var string[]
*/
Expand Down Expand Up @@ -180,4 +191,46 @@ public function __unserialize(array $data): void
// 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]
public function onPrePersist()
{
if (null === $this->getCreatedAt()) {
$this->setCreatedAt(new \DateTimeImmutable('now'));
}

if ($this->getUpdatedAt() == null) {
$this->setUpdatedAt(new \DateTimeImmutable('now'));
}
}

#[PreUpdate]
public function onPreUpdate()
{
$this->setUpdatedAt(new \DateTimeImmutable('now'));
}
}
Loading