Skip to content

Commit

Permalink
minor #1363 Use Doctrine Types in entities (qdequippe)
Browse files Browse the repository at this point in the history
This PR was merged into the main branch.

Discussion
----------

Use Doctrine Types in entities

Benefit built int Doctrine Types directly on each entity

Commits
-------

1147835 Use Doctrine Types in entities
  • Loading branch information
javiereguiluz committed Nov 16, 2022
2 parents 2767915 + 1147835 commit 4651bcc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
7 changes: 4 additions & 3 deletions src/Entity/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)]
Expand Down
13 changes: 7 additions & 6 deletions src/Entity/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)]
Expand Down
5 changes: 3 additions & 2 deletions src/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace App\Entity;

use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

/**
Expand All @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down

0 comments on commit 4651bcc

Please sign in to comment.