Skip to content

Commit 1c8c4b3

Browse files
committed
Use attributes to define ORM metadata
1 parent 7a0ebdb commit 1c8c4b3

File tree

6 files changed

+60
-101
lines changed

6 files changed

+60
-101
lines changed

config/packages/doctrine.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ doctrine:
1212
mappings:
1313
App:
1414
is_bundle: false
15-
type: annotation
15+
type: attribute
1616
dir: '%kernel.project_dir%/src/Entity'
1717
prefix: 'App\Entity'
1818
alias: App

phpstan-baseline.neon

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ parameters:
175175
count: 1
176176
path: src/Entity/Post.php
177177

178+
-
179+
message: "#^Parameter \\$repositoryClass of attribute class Doctrine\\\\ORM\\\\Mapping\\\\Entity constructor expects class\\-string\\<Doctrine\\\\ORM\\\\EntityRepository\\<T of object\\>\\>\\|null, 'App\\\\\\\\Repository\\\\\\\\PostRepository' given\\.$#"
180+
count: 1
181+
path: src/Entity/Post.php
182+
178183
-
179184
message: "#^Property App\\\\Entity\\\\Post\\:\\:\\$comments with generic interface Doctrine\\\\Common\\\\Collections\\\\Collection does not specify its types\\: TKey, T$#"
180185
count: 1
@@ -215,6 +220,11 @@ parameters:
215220
count: 1
216221
path: src/Entity/User.php
217222

223+
-
224+
message: "#^Parameter \\$repositoryClass of attribute class Doctrine\\\\ORM\\\\Mapping\\\\Entity constructor expects class\\-string\\<Doctrine\\\\ORM\\\\EntityRepository\\<T of object\\>\\>\\|null, 'App\\\\\\\\Repository\\\\\\\\UserRepository' given\\.$#"
225+
count: 1
226+
path: src/Entity/User.php
227+
218228
-
219229
message: "#^Property App\\\\Entity\\\\User\\:\\:\\$roles type has no value type specified in iterable type array\\.$#"
220230
count: 1

src/Entity/Comment.php

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,29 @@
1616
use Symfony\Component\Validator\Constraints as Assert;
1717

1818
/**
19-
* @ORM\Entity
20-
* @ORM\Table(name="symfony_demo_comment")
21-
*
2219
* Defines the properties of the Comment entity to represent the blog comments.
23-
* See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class
20+
* See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class.
2421
*
2522
* Tip: if you have an existing database, you can generate these entity class automatically.
2623
* See https://symfony.com/doc/current/doctrine/reverse_engineering.html
2724
*
2825
* @author Ryan Weaver <[email protected]>
2926
* @author Javier Eguiluz <[email protected]>
3027
*/
28+
#[ORM\Entity]
29+
#[ORM\Table(name: 'symfony_demo_comment')]
3130
class Comment
3231
{
33-
/**
34-
* @ORM\Id
35-
* @ORM\GeneratedValue
36-
* @ORM\Column(type="integer")
37-
*/
32+
#[ORM\Id]
33+
#[ORM\GeneratedValue]
34+
#[ORM\Column(type: 'integer')]
3835
private ?int $id = null;
3936

40-
/**
41-
* @ORM\ManyToOne(targetEntity="Post", inversedBy="comments")
42-
* @ORM\JoinColumn(nullable=false)
43-
*/
37+
#[ORM\ManyToOne(targetEntity: Post::class, inversedBy: 'comments')]
38+
#[ORM\JoinColumn(nullable: false)]
4439
private ?Post $post = null;
4540

46-
/**
47-
* @ORM\Column(type="text")
48-
*/
41+
#[ORM\Column(type: 'text')]
4942
#[
5043
Assert\NotBlank(message: 'comment.blank'),
5144
Assert\Length(
@@ -57,15 +50,11 @@ class Comment
5750
]
5851
private ?string $content = null;
5952

60-
/**
61-
* @ORM\Column(type="datetime")
62-
*/
53+
#[ORM\Column(type: 'datetime')]
6354
private \DateTime $publishedAt;
6455

65-
/**
66-
* @ORM\ManyToOne(targetEntity="App\Entity\User")
67-
* @ORM\JoinColumn(nullable=false)
68-
*/
56+
#[ORM\ManyToOne(targetEntity: User::class)]
57+
#[ORM\JoinColumn(nullable: false)]
6958
private ?User $author = null;
7059

7160
public function __construct()

src/Entity/Post.php

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111

1212
namespace App\Entity;
1313

14+
use App\Repository\PostRepository;
1415
use Doctrine\Common\Collections\ArrayCollection;
1516
use Doctrine\Common\Collections\Collection;
1617
use Doctrine\ORM\Mapping as ORM;
1718
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
1819
use Symfony\Component\Validator\Constraints as Assert;
1920

2021
/**
21-
* @ORM\Entity(repositoryClass="App\Repository\PostRepository")
22-
* @ORM\Table(name="symfony_demo_post")
23-
* @UniqueEntity(fields={"slug"}, errorPath="title", message="post.slug_unique")
24-
*
2522
* Defines the properties of the Post entity to represent the blog posts.
2623
*
2724
* See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class
@@ -33,77 +30,57 @@
3330
* @author Javier Eguiluz <[email protected]>
3431
* @author Yonel Ceruto <[email protected]>
3532
*/
33+
#[ORM\Entity(repositoryClass: PostRepository::class)]
34+
#[ORM\Table(name: 'symfony_demo_post')]
35+
#[UniqueEntity(fields: ['slug'], errorPath: 'title', message: 'post.slug_unique')]
3636
class Post
3737
{
38-
/**
39-
* @ORM\Id
40-
* @ORM\GeneratedValue
41-
* @ORM\Column(type="integer")
42-
*/
38+
#[ORM\Id]
39+
#[ORM\GeneratedValue]
40+
#[ORM\Column(type: 'integer')]
4341
private ?int $id = null;
4442

45-
/**
46-
* @ORM\Column(type="string")
47-
*/
43+
#[ORM\Column(type: 'string')]
4844
#[Assert\NotBlank]
4945
private ?string $title = null;
5046

51-
/**
52-
* @ORM\Column(type="string")
53-
*/
47+
#[ORM\Column(type: 'string')]
5448
private ?string $slug = null;
5549

56-
/**
57-
* @ORM\Column(type="string")
58-
*/
50+
#[ORM\Column(type: 'string')]
5951
#[
6052
Assert\NotBlank(message: 'post.blank_summary'),
6153
Assert\Length(max: 255)
6254
]
6355
private ?string $summary = null;
6456

65-
/**
66-
* @var string
67-
*
68-
* @ORM\Column(type="text")
69-
*/
57+
#[ORM\Column(type: 'text')]
7058
#[
7159
Assert\NotBlank(message: 'post.blank_content'),
7260
Assert\Length(min: 10, minMessage: 'post.too_short_content')
7361
]
7462
private ?string $content = null;
7563

76-
/**
77-
* @ORM\Column(type="datetime")
78-
*/
64+
#[ORM\Column(type: 'datetime')]
7965
private \DateTime $publishedAt;
8066

81-
/**
82-
* @ORM\ManyToOne(targetEntity="App\Entity\User")
83-
* @ORM\JoinColumn(nullable=false)
84-
*/
67+
#[ORM\ManyToOne(targetEntity: User::class)]
68+
#[ORM\JoinColumn(nullable: false)]
8569
private ?User $author = null;
8670

8771
/**
8872
* @var Comment[]|Collection
89-
*
90-
* @ORM\OneToMany(
91-
* targetEntity="Comment",
92-
* mappedBy="post",
93-
* orphanRemoval=true,
94-
* cascade={"persist"}
95-
* )
96-
* @ORM\OrderBy({"publishedAt": "DESC"})
9773
*/
74+
#[ORM\OneToMany(targetEntity: Comment::class, mappedBy: 'post', orphanRemoval: true, cascade: ['persist'])]
75+
#[ORM\OrderBy(['publishedAt' => 'DESC'])]
9876
private Collection $comments;
9977

10078
/**
10179
* @var Tag[]|Collection
102-
*
103-
* @ORM\ManyToMany(targetEntity="App\Entity\Tag", cascade={"persist"})
104-
* @ORM\JoinTable(name="symfony_demo_post_tag")
105-
* @ORM\OrderBy({"name": "ASC"})
10680
*/
81+
#[ORM\ManyToMany(targetEntity: Tag::class, cascade: ['persist'])]
82+
#[ORM\JoinTable(name: 'symfony_demo_post_tag')]
83+
#[ORM\OrderBy(['name' => 'ASC'])]
10784
#[Assert\Count(max: 4, maxMessage: 'post.too_many_tags')]
10885
private Collection $tags;
10986

src/Entity/Tag.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,22 @@
1414
use Doctrine\ORM\Mapping as ORM;
1515

1616
/**
17-
* @ORM\Entity()
18-
* @ORM\Table(name="symfony_demo_tag")
19-
*
2017
* Defines the properties of the Tag entity to represent the post tags.
2118
*
2219
* See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class
2320
*
2421
* @author Yonel Ceruto <[email protected]>
2522
*/
23+
#[ORM\Entity]
24+
#[ORM\Table(name: 'symfony_demo_tag')]
2625
class Tag implements \JsonSerializable
2726
{
28-
/**
29-
* @ORM\Id
30-
* @ORM\GeneratedValue
31-
* @ORM\Column(type="integer")
32-
*/
27+
#[ORM\Id]
28+
#[ORM\GeneratedValue]
29+
#[ORM\Column(type: 'integer')]
3330
private ?int $id = null;
3431

35-
/**
36-
* @ORM\Column(type="string", unique=true)
37-
*/
32+
#[ORM\Column(type: 'string', unique: true)]
3833
private ?string $name = null;
3934

4035
public function getId(): ?int

src/Entity/User.php

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,50 @@
1111

1212
namespace App\Entity;
1313

14+
use App\Repository\UserRepository;
1415
use Doctrine\ORM\Mapping as ORM;
1516
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
1617
use Symfony\Component\Security\Core\User\UserInterface;
1718
use Symfony\Component\Validator\Constraints as Assert;
1819

1920
/**
20-
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
21-
* @ORM\Table(name="symfony_demo_user")
22-
*
2321
* Defines the properties of the User entity to represent the application users.
24-
* See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class
22+
* See https://symfony.com/doc/current/doctrine.html#creating-an-entity-class.
2523
*
2624
* Tip: if you have an existing database, you can generate these entity class automatically.
2725
* See https://symfony.com/doc/current/doctrine/reverse_engineering.html
2826
*
2927
* @author Ryan Weaver <[email protected]>
3028
* @author Javier Eguiluz <[email protected]>
3129
*/
30+
#[ORM\Entity(repositoryClass: UserRepository::class)]
31+
#[ORM\Table(name: 'symfony_demo_user')]
3232
class User implements UserInterface, PasswordAuthenticatedUserInterface
3333
{
34-
/**
35-
* @ORM\Id
36-
* @ORM\GeneratedValue
37-
* @ORM\Column(type="integer")
38-
*/
34+
#[ORM\Id]
35+
#[ORM\GeneratedValue]
36+
#[ORM\Column(type: 'integer')]
3937
private ?int $id = null;
4038

41-
/**
42-
* @ORM\Column(type="string")
43-
*/
39+
#[ORM\Column(type: 'string')]
4440
#[Assert\NotBlank]
4541
private ?string $fullName = null;
4642

47-
/**
48-
* @ORM\Column(type="string", unique=true)
49-
*/
43+
#[ORM\Column(type: 'string', unique: true)]
5044
#[
5145
Assert\NotBlank,
5246
Assert\Length(min: 2, max: 50)
5347
]
5448
private ?string $username = null;
5549

56-
/**
57-
* @ORM\Column(type="string", unique=true)
58-
*/
50+
#[ORM\Column(type: 'string', unique: true)]
5951
#[Assert\Email]
6052
private ?string $email = null;
6153

62-
/**
63-
* @ORM\Column(type="string")
64-
*/
54+
#[ORM\Column(type: 'string')]
6555
private ?string $password = null;
6656

67-
/**
68-
* @ORM\Column(type="json")
69-
*/
57+
#[ORM\Column(type: 'json')]
7058
private array $roles = [];
7159

7260
public function getId(): ?int

0 commit comments

Comments
 (0)