9
9
use Doctrine \Common \Collections \ArrayCollection ;
10
10
use Doctrine \Common \Collections \Collection ;
11
11
use Doctrine \ORM \Mapping as ORM ;
12
+ use Symfony \Component \Serializer \Annotation \Groups ;
12
13
use Symfony \Component \Validator \Constraints as Assert ;
13
14
14
15
/**
17
18
* @see http://schema.org/Book Documentation on Schema.org
18
19
*
19
20
* @ORM\Entity
20
- * @ApiResource(iri="http://schema.org/Book")
21
+ * @ApiResource(
22
+ * iri="http://schema.org/Book",
23
+ * normalizationContext={"groups": {"book:read"}}
24
+ * )
21
25
* @ApiFilter(PropertyFilter::class)
22
26
*/
23
27
class Book
@@ -36,6 +40,7 @@ class Book
36
40
*
37
41
* @Assert\Isbn
38
42
* @ORM\Column(nullable=true)
43
+ * @Groups("book:read")
39
44
* @ApiProperty(iri="http://schema.org/isbn")
40
45
*/
41
46
public $ isbn ;
@@ -45,6 +50,7 @@ class Book
45
50
*
46
51
* @Assert\NotBlank
47
52
* @ORM\Column
53
+ * @Groups({"book:read", "review:read"})
48
54
* @ApiProperty(iri="http://schema.org/name")
49
55
*/
50
56
public $ title ;
@@ -54,6 +60,7 @@ class Book
54
60
*
55
61
* @Assert\NotBlank
56
62
* @ORM\Column(type="text")
63
+ * @Groups("book:read")
57
64
* @ApiProperty(iri="http://schema.org/description")
58
65
*/
59
66
public $ description ;
@@ -63,6 +70,7 @@ class Book
63
70
*
64
71
* @Assert\NotBlank
65
72
* @ORM\Column
73
+ * @Groups("book:read")
66
74
* @ApiProperty(iri="http://schema.org/author")
67
75
*/
68
76
public $ author ;
@@ -73,14 +81,16 @@ class Book
73
81
* @Assert\Date
74
82
* @Assert\NotNull
75
83
* @ORM\Column(type="date")
84
+ * @Groups("book:read")
76
85
* @ApiProperty(iri="http://schema.org/dateCreated")
77
86
*/
78
87
public $ publicationDate ;
79
88
80
89
/**
81
90
* @var Review[] The book's reviews
82
91
*
83
- * @ORM\OneToMany(targetEntity=Review::class, mappedBy="book", orphanRemoval=true, cascade={"all"})
92
+ * @ORM\OneToMany(targetEntity=Review::class, mappedBy="book", orphanRemoval=true, cascade={"persist", "remove"})
93
+ * @Groups("book:read")
84
94
* @ApiProperty(iri="http://schema.org/reviews")
85
95
*/
86
96
private $ reviews ;
@@ -95,27 +105,31 @@ public function getId(): ?int
95
105
return $ this ->id ;
96
106
}
97
107
98
- /**
99
- * @return Collection|Review[]
100
- */
101
- public function getReviews (): iterable
108
+ public function addReview (Review $ review , bool $ updateRelation = true ): void
102
109
{
103
- return $ this ->reviews ;
110
+ if ($ this ->reviews ->contains ($ review )) {
111
+ return ;
112
+ }
113
+
114
+ $ this ->reviews ->add ($ review );
115
+ if ($ updateRelation ) {
116
+ $ review ->setBook ($ this , false );
117
+ }
104
118
}
105
119
106
- public function addReview (Review $ review ): void
120
+ public function removeReview (Review $ review, bool $ updateRelation = true ): void
107
121
{
108
- if (! $ this ->reviews ->contains ($ review )) {
109
- $ this -> reviews -> add ( $ review );
110
- $ review ->book = $ this ;
122
+ $ this ->reviews ->removeElement ($ review );
123
+ if ( $ updateRelation ) {
124
+ $ review ->setBook ( null , false ) ;
111
125
}
112
126
}
113
127
114
- public function removeReview (Review $ review ): void
128
+ /**
129
+ * @return Collection|Review[]
130
+ */
131
+ public function getReviews (): iterable
115
132
{
116
- if ($ this ->reviews ->contains ($ review )) {
117
- $ review ->book = $ this ;
118
- $ this ->reviews ->removeElement ($ review );
119
- }
133
+ return $ this ->reviews ;
120
134
}
121
135
}
0 commit comments