Bonjour ou bonsoir,
Je n'arrive pas à créer ma fixture Manuscript qui prend en référence un User afin d'ajouter l'id de ce dernier à author_id (contenu dans manuscrit)... J'ai cette 'belle' erreur :
Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`testbook2`.`manuscript`, CONSTRAINT `FK_5AF919CD69CCBE9A` FOREIGN KEY (`author_id_id`) REFERENCES `user` (`id`))
J'ai parcouru divers forum qui disent qu'il faut utiliser cascade:['persist'] (Je suis en php 8, c'est cette syntaxe qui est utilisée). J'ai donc essayer cela des deux côtés de mon entité, mais ça ne fonctionne pas... De plus, je n'arrive pas bien à déterminer qui est le parent de qui dans cette histoire. Je suis perdu, je l'avoue... Si l'un ou l'une d'entre vous pouvait m'éclairer je lui en serai reconnaissant. Merci d'avance !!
Manuscript.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 <?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use App\Repository\ManuscriptRepository; use Doctrine\Common\Collections\Collection; use Symfony\Component\HttpFoundation\File\File; use Doctrine\Common\Collections\ArrayCollection; use Vich\UploaderBundle\Mapping\Annotation as Vich; #[ORM\Entity(repositoryClass: ManuscriptRepository::class)] #[Vich\Uploadable] class Manuscript { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] private $id; #[ORM\Column(type: 'string', length: 100)] private $title; #[ORM\Column(type: 'text')] private $content; #[ORM\Column(type: 'string', length: 255, nullable: true)] private $description; // Pour cette propriété manuscript devrait prendre un s #[ORM\ManyToMany(targetEntity: User::class, mappedBy: 'manuscript')] private $users; #[ORM\ManyToMany(targetEntity: Genre::class, mappedBy: 'manuscripts')] private $genres; #[ORM\ManyToMany(targetEntity: Question::class, mappedBy: 'manuscripts')] private $questions; // author_id est la clés étrangère et fait référence à l'id du user, La classe manuscript devient parente de la classe User ? // Si un User est détruit est ce que tous les livres qu'il a écrit sont détruits ? N'arrive pas à créer la référence de la clé étrangère dans la colonne Manuscript ? #[ORM\ManyToOne(targetEntity: User::class, inversedBy: 'manuscripts', cascade: ["persist", "remove"])] private $author_id; #[ORM\Column(type: 'string', length: 255, nullable: true)] private $imageName; /** * NOTE: This is not a mapped field of entity metadata, just a simple property. */ #[Vich\UploadableField(mapping: 'manuscripts', fileNameProperty: 'imageName')] private ?File $imageFile = null; #[ORM\Column(type: 'datetime_immutable')] private $updatedAt; public function __construct() { $this->users = new ArrayCollection(); $this->genres = new ArrayCollection(); $this->questions = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getTitle(): ?string { return $this->title; } public function setTitle(string $title): self { $this->title = $title; return $this; } public function getContent(): ?string { return $this->content; } public function setContent(string $content): self { $this->content = $content; return $this; } public function getDescription(): ?string { return $this->description; } public function setDescription(?string $description): self { $this->description = $description; return $this; } /** * @return Collection|User[] */ public function getUsers(): Collection { return $this->users; } public function addUser(User $user): self { if (!$this->users->contains($user)) { $this->users[] = $user; $user->addManuscript($this); } return $this; } public function removeUser(User $user): self { if ($this->users->removeElement($user)) { $user->removeManuscript($this); } return $this; } /** * @return Collection|Genre[] */ public function getGenres(): Collection { return $this->genres; } public function addGenre(Genre $genre): self { if (!$this->genres->contains($genre)) { $this->genres[] = $genre; $genre->addManuscript($this); } return $this; } public function removeGenre(Genre $genre): self { if ($this->genres->removeElement($genre)) { $genre->removeManuscript($this); } return $this; } /** * @return Collection|Question[] */ public function getQuestions(): Collection { return $this->questions; } public function addQuestion(Question $question): self { if (!$this->questions->contains($question)) { $this->questions[] = $question; $question->addManuscript($this); } return $this; } public function removeQuestion(Question $question): self { if ($this->questions->removeElement($question)) { $question->removeManuscript($this); } return $this; } public function getAuthorId(): ?User { return $this->author_id; } public function setAuthorId(?User $author_id): self { $this->author_id = $author_id; return $this; } public function getImageName(): ?string { return $this->imageName; } public function setImageName(?string $imageName): self { $this->imageName = $imageName; return $this; } public function getUpdatedAt(): ?\DateTimeImmutable { return $this->updatedAt; } public function setUpdatedAt(\DateTimeImmutable $updatedAt): self { $this->updatedAt = $updatedAt; return $this; } /** * If manually uploading a file (i.e. not using Symfony Form) ensure an instance * of 'UploadedFile' is injected into this setter to trigger the update. If this * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter * must be able to accept an instance of 'File' as the bundle will inject one here * during Doctrine hydration. * * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile */ public function setImageFile(?File $imageFile = null): void { $this->imageFile = $imageFile; if (null !== $imageFile) { // It is required that at least one field changes if you are using doctrine // otherwise the event listeners won't be called and the file is lost $this->updatedAt = new \DateTimeImmutable(); } } public function getImageFile(): ?File { return $this->imageFile; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230 <?php namespace App\Entity; use App\Repository\UserRepository; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; use Symfony\Component\Security\Core\User\UserInterface; /** * @UniqueEntity(fields={"email"}, message="There is already an account with this email") */ #[ORM\Entity(repositoryClass: UserRepository::class)] #[ORM\Table(name: '`user`')] class User implements UserInterface, PasswordAuthenticatedUserInterface { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column(type: 'integer')] private $id; #[ORM\Column(type: 'string', length: 180, unique: true)] private $email; #[ORM\Column(type: 'json')] private $roles = []; #[ORM\Column(type: 'string')] private $password; #[ORM\Column(type: 'string', length: 100, nullable: true)] private $firstname; #[ORM\Column(type: 'string', length: 100, nullable: true)] private $name; #[ORM\Column(type: 'string', length: 255, nullable: true)] private $imageName; #[ORM\Column(type: 'datetime_immutable', nullable:true)] private $updatedAt; #[ORM\Column(type:"boolean")] private $isVerified = false; #[ORM\ManyToMany(targetEntity: Manuscript::class, inversedBy: 'users')] private $manuscript; #[ORM\OneToMany(mappedBy: 'author_id', targetEntity: Manuscript::class, cascade: ["persist", "remove"])] private $manuscripts; // A modifier, Pb on niveau du nommage la relation One to Many devrait s'appeler author_manuscript public function __construct() { $this->manuscript = new ArrayCollection(); $this->manuscripts = new ArrayCollection(); } public function getId(): ?int { return $this->id; } public function getEmail(): ?string { return $this->email; } public function setEmail(string $email): self { $this->email = $email; return $this; } /** * A visual identifier that represents this user. * * @see UserInterface */ public function getUserIdentifier(): string { return (string) $this->email; } /** * @see UserInterface */ public function getRoles(): array { $roles = $this->roles; // guarantee every user at least has ROLE_USER $roles[] = 'ROLE_USER'; return array_unique($roles); } public function setRoles(array $roles): self { $this->roles = $roles; return $this; } /** * @see PasswordAuthenticatedUserInterface */ public function getPassword(): string { return $this->password; } public function setPassword(string $password): self { $this->password = $password; return $this; } /** * @see UserInterface */ public function eraseCredentials() { // If you store any temporary, sensitive data on the user, clear it here // $this->plainPassword = null; } public function getFirstname(): ?string { return $this->firstname; } public function setFirstname(?string $firstname): self { $this->firstname = $firstname; return $this; } public function getName(): ?string { return $this->name; } public function setName(?string $name): self { $this->name = $name; return $this; } public function getImageName(): ?string { return $this->imageName; } public function setImageName(?string $imageName): self { $this->imageName = $imageName; return $this; } public function getUpdatedAt(): ?\DateTimeImmutable { return $this->updatedAt; } public function setUpdatedAt(\DateTimeImmutable $updatedAt): self { $this->updatedAt = $updatedAt; return $this; } public function isVerified(): bool { return $this->isVerified; } public function setIsVerified(bool $isVerified): self { $this->isVerified = $isVerified; return $this; } /** * @return Collection|Manuscript[] */ public function getManuscript(): Collection { return $this->manuscript; } public function addManuscript(Manuscript $manuscript): self { if (!$this->manuscript->contains($manuscript)) { $this->manuscript[] = $manuscript; } return $this; } public function removeManuscript(Manuscript $manuscript): self { $this->manuscript->removeElement($manuscript); return $this; } /** * @return Collection|Manuscript[] */ public function getManuscripts(): Collection { return $this->manuscripts; } }
Peut-être y-a-t'il besoin de ManuscriptFixtures.php
et UserFixtures.php
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 <?php namespace App\DataFixtures; use App\Entity\User; use App\Entity\Manuscript; use Doctrine\Persistence\ObjectManager; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Common\DataFixtures\DependentFixtureInterface; class ManuscriptFixtures extends Fixture implements DependentFixtureInterface { public function load(ObjectManager $manager): void { $manuscript = new Manuscript(); $manuscript->setTitle('Fixtures'); $manuscript->setContent('Je suis une fixture !'); $manuscript->setImageName('st.1'); $manuscript->setAuthorId($this->getReference(UserFixtures::USER_REFERENCE)); $manager->persist($manuscript); $manager->flush(); } public function getDependencies() { return [ UserFixtures::class ]; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 <?php namespace App\DataFixtures; use App\Entity\User; use DateTimeImmutable; use Doctrine\Bundle\FixturesBundle\Fixture; use Doctrine\Persistence\ObjectManager; class UserFixtures extends Fixture { public const USER_REFERENCE = 'users'; public function load(ObjectManager $manager): void { $user = new User(); $user->setFirstname('Gautier'); $user->setName('Fenaux'); $user->setPassword('pass123'); $user->setRoles(['ROLES_USER']); $user->setEmail('gautier.fenaux@gmail.com'); $user->setImageName('test.jpg'); $user->setIsVerified(true); $user->setUpdatedAt(new DateTimeImmutable()); $this->addReference(self::USER_REFERENCE, $user); $manager->persist($user); $manager->flush(); } }
Partager