diff --git a/src/DataFixtures/AppFixtures.php b/src/DataFixtures/AppFixtures.php index 319df2530..f1a1e1251 100644 --- a/src/DataFixtures/AppFixtures.php +++ b/src/DataFixtures/AppFixtures.php @@ -85,7 +85,7 @@ private function loadPosts(ObjectManager $manager): void $comment = new Comment(); $comment->setAuthor($commentAuthor); $comment->setContent($this->getRandomText(random_int(255, 512))); - $comment->setPublishedAt(new \DateTime('now + '.$i.'seconds')); + $comment->setPublishedAt(new \DateTimeImmutable('now + '.$i.'seconds')); $post->addComment($comment); } @@ -130,7 +130,7 @@ private function getTagData(): array /** * @throws \Exception * - * @return array}> + * @return array}> */ private function getPostData(): array { @@ -147,7 +147,7 @@ private function getPostData(): array $this->slugger->slug($title)->lower(), $this->getRandomText(), $this->getPostContent(), - (new \DateTime('now - '.$i.'days'))->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)), + (new \DateTimeImmutable('now - '.$i.'days'))->setTime(random_int(8, 17), random_int(7, 49), random_int(0, 59)), // Ensure that the first post is written by Jane Doe to simplify tests $user, $this->getRandomTags(), diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php index aa1b8e6bc..707601781 100644 --- a/src/Entity/Comment.php +++ b/src/Entity/Comment.php @@ -44,8 +44,8 @@ class Comment #[Assert\Length(min: 5, minMessage: 'comment.too_short', max: 10000, maxMessage: 'comment.too_long')] private ?string $content = null; - #[ORM\Column(type: Types::DATETIME_MUTABLE)] - private \DateTime $publishedAt; + #[ORM\Column] + private \DateTimeImmutable $publishedAt; #[ORM\ManyToOne(targetEntity: User::class)] #[ORM\JoinColumn(nullable: false)] @@ -53,7 +53,7 @@ class Comment public function __construct() { - $this->publishedAt = new \DateTime(); + $this->publishedAt = new \DateTimeImmutable(); } #[Assert\IsTrue(message: 'comment.is_spam')] @@ -79,12 +79,12 @@ public function setContent(string $content): void $this->content = $content; } - public function getPublishedAt(): \DateTime + public function getPublishedAt(): \DateTimeImmutable { return $this->publishedAt; } - public function setPublishedAt(\DateTime $publishedAt): void + public function setPublishedAt(\DateTimeImmutable $publishedAt): void { $this->publishedAt = $publishedAt; } diff --git a/src/Entity/Post.php b/src/Entity/Post.php index 4682e9825..e26524d81 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -58,8 +58,8 @@ class Post #[Assert\Length(min: 10, minMessage: 'post.too_short_content')] private ?string $content = null; - #[ORM\Column(type: Types::DATETIME_MUTABLE)] - private \DateTime $publishedAt; + #[ORM\Column] + private \DateTimeImmutable $publishedAt; #[ORM\ManyToOne(targetEntity: User::class)] #[ORM\JoinColumn(nullable: false)] @@ -83,7 +83,7 @@ class Post public function __construct() { - $this->publishedAt = new \DateTime(); + $this->publishedAt = new \DateTimeImmutable(); $this->comments = new ArrayCollection(); $this->tags = new ArrayCollection(); } @@ -123,12 +123,12 @@ public function setContent(?string $content): void $this->content = $content; } - public function getPublishedAt(): \DateTime + public function getPublishedAt(): \DateTimeImmutable { return $this->publishedAt; } - public function setPublishedAt(\DateTime $publishedAt): void + public function setPublishedAt(\DateTimeImmutable $publishedAt): void { $this->publishedAt = $publishedAt; } diff --git a/src/Form/Type/DateTimePickerType.php b/src/Form/Type/DateTimePickerType.php index 2400ec1eb..7dddd8182 100644 --- a/src/Form/Type/DateTimePickerType.php +++ b/src/Form/Type/DateTimePickerType.php @@ -31,6 +31,7 @@ public function configureOptions(OptionsResolver $resolver): void // @see https://symfony.com/doc/current/reference/forms/types/date.html#rendering-a-single-html5-text-box $resolver->setDefaults([ 'widget' => 'single_text', + 'input' => 'datetime_immutable', // if true, the browser will display the native date picker widget // however, this app uses a custom JavaScript widget, so it must be set to false 'html5' => false, diff --git a/src/Repository/PostRepository.php b/src/Repository/PostRepository.php index 62b026cda..bef34dafc 100644 --- a/src/Repository/PostRepository.php +++ b/src/Repository/PostRepository.php @@ -47,7 +47,7 @@ public function findLatest(int $page = 1, ?Tag $tag = null): Paginator ->leftJoin('p.tags', 't') ->where('p.publishedAt <= :now') ->orderBy('p.publishedAt', 'DESC') - ->setParameter('now', new \DateTime()) + ->setParameter('now', new \DateTimeImmutable()) ; if (null !== $tag) {