From 114783516b402a69d574fa97bef2d2eace3e1488 Mon Sep 17 00:00:00 2001 From: Quentin Dequippe Date: Fri, 28 Oct 2022 14:51:29 +0200 Subject: [PATCH] Use Doctrine Types in entities --- src/Entity/Comment.php | 7 ++++--- src/Entity/Post.php | 13 +++++++------ src/Entity/Tag.php | 5 +++-- src/Entity/User.php | 13 +++++++------ 4 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Entity/Comment.php b/src/Entity/Comment.php index b7bc5d097..2ac275b82 100644 --- a/src/Entity/Comment.php +++ b/src/Entity/Comment.php @@ -11,6 +11,7 @@ namespace App\Entity; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use function Symfony\Component\String\u; use Symfony\Component\Validator\Constraints as Assert; @@ -31,19 +32,19 @@ class Comment { #[ORM\Id] #[ORM\GeneratedValue] - #[ORM\Column(type: 'integer')] + #[ORM\Column(type: Types::INTEGER)] private ?int $id = null; #[ORM\ManyToOne(targetEntity: Post::class, inversedBy: 'comments')] #[ORM\JoinColumn(nullable: false)] private ?Post $post = null; - #[ORM\Column(type: 'text')] + #[ORM\Column(type: Types::TEXT)] #[Assert\NotBlank(message: 'comment.blank')] #[Assert\Length(min: 5, minMessage: 'comment.too_short', max: 10000, maxMessage: 'comment.too_long')] private ?string $content = null; - #[ORM\Column(type: 'datetime')] + #[ORM\Column(type: Types::DATETIME_MUTABLE)] private \DateTime $publishedAt; #[ORM\ManyToOne(targetEntity: User::class)] diff --git a/src/Entity/Post.php b/src/Entity/Post.php index cf1237979..70fe4be89 100644 --- a/src/Entity/Post.php +++ b/src/Entity/Post.php @@ -14,6 +14,7 @@ use App\Repository\PostRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Validator\Constraints as Assert; @@ -37,27 +38,27 @@ class Post { #[ORM\Id] #[ORM\GeneratedValue] - #[ORM\Column(type: 'integer')] + #[ORM\Column(type: Types::INTEGER)] private ?int $id = null; - #[ORM\Column(type: 'string')] + #[ORM\Column(type: Types::STRING)] #[Assert\NotBlank] private ?string $title = null; - #[ORM\Column(type: 'string')] + #[ORM\Column(type: Types::STRING)] private ?string $slug = null; - #[ORM\Column(type: 'string')] + #[ORM\Column(type: Types::STRING)] #[Assert\NotBlank(message: 'post.blank_summary')] #[Assert\Length(max: 255)] private ?string $summary = null; - #[ORM\Column(type: 'text')] + #[ORM\Column(type: Types::TEXT)] #[Assert\NotBlank(message: 'post.blank_content')] #[Assert\Length(min: 10, minMessage: 'post.too_short_content')] private ?string $content = null; - #[ORM\Column(type: 'datetime')] + #[ORM\Column(type: Types::DATETIME_MUTABLE)] private \DateTime $publishedAt; #[ORM\ManyToOne(targetEntity: User::class)] diff --git a/src/Entity/Tag.php b/src/Entity/Tag.php index 8a6be2c57..511edab64 100644 --- a/src/Entity/Tag.php +++ b/src/Entity/Tag.php @@ -11,6 +11,7 @@ namespace App\Entity; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; /** @@ -26,10 +27,10 @@ class Tag implements \JsonSerializable { #[ORM\Id] #[ORM\GeneratedValue] - #[ORM\Column(type: 'integer')] + #[ORM\Column(type: Types::INTEGER)] private ?int $id = null; - #[ORM\Column(type: 'string', unique: true)] + #[ORM\Column(type: Types::STRING, unique: true)] private readonly string $name; public function __construct(string $name) diff --git a/src/Entity/User.php b/src/Entity/User.php index 1cc3c9c2f..76a191d17 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -12,6 +12,7 @@ namespace App\Entity; use App\Repository\UserRepository; +use Doctrine\DBAL\Types\Types; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; @@ -33,26 +34,26 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface { #[ORM\Id] #[ORM\GeneratedValue] - #[ORM\Column(type: 'integer')] + #[ORM\Column(type: Types::INTEGER)] private ?int $id = null; - #[ORM\Column(type: 'string')] + #[ORM\Column(type: Types::STRING)] #[Assert\NotBlank] private ?string $fullName = null; - #[ORM\Column(type: 'string', unique: true)] + #[ORM\Column(type: Types::STRING, unique: true)] #[Assert\NotBlank] #[Assert\Length(min: 2, max: 50)] private ?string $username = null; - #[ORM\Column(type: 'string', unique: true)] + #[ORM\Column(type: Types::STRING, unique: true)] #[Assert\Email] private ?string $email = null; - #[ORM\Column(type: 'string')] + #[ORM\Column(type: Types::STRING)] private ?string $password = null; - #[ORM\Column(type: 'json')] + #[ORM\Column(type: Types::JSON)] private array $roles = []; public function getId(): ?int