質問編集履歴

1

説明不足を追加

2019/09/16 12:55

投稿

yushi.s
yushi.s

スコア8

test CHANGED
@@ -1 +1 @@
1
- Symfony OneToOneリレーションでのエラー
1
+ Symfony OneToOneリレーションでのエラー Integrity constraint violation: 1048 Column 'my_event_id' cannot be null
test CHANGED
@@ -6,14 +6,16 @@
6
6
 
7
7
  ```php
8
8
 
9
+ # マイイベントエンティティ
10
+
11
+
12
+
9
13
  /**
10
14
 
11
15
  * @ORM\Entity(repositoryClass="App\Repository\MyEventRepository")
12
16
 
13
17
  * @ORM\HasLifecycleCallbacks
14
18
 
15
- * @Vich\Uploadable
16
-
17
19
  */
18
20
 
19
21
  class MyEvent
@@ -44,7 +46,7 @@
44
46
 
45
47
 
46
48
 
47
- public function getId(): ?int
49
+ public function getId(): ?int
48
50
 
49
51
  {
50
52
 
@@ -78,4 +80,218 @@
78
80
 
79
81
 
80
82
 
83
+ }
84
+
81
- }```
85
+ ```
86
+
87
+ ```php
88
+
89
+ # マイイベントスケジュールエンティティ
90
+
91
+
92
+
93
+ /**
94
+
95
+ * @ORM\Entity(repositoryClass="App\Repository\MyEventScheduleRepository")
96
+
97
+ * @ORM\HasLifecycleCallbacks
98
+
99
+ */
100
+
101
+ class MyEventSchedule
102
+
103
+ {
104
+
105
+ /**
106
+
107
+ * @ORM\Id()
108
+
109
+ * @ORM\GeneratedValue()
110
+
111
+ * @ORM\Column(type="integer")
112
+
113
+ */
114
+
115
+ private $id;
116
+
117
+
118
+
119
+ /**
120
+
121
+ * @ORM\OneToOne(targetEntity="App\Entity\MyEvent", cascade={"persist", "remove"})
122
+
123
+ * @ORM\JoinColumn(nullable=false)
124
+
125
+ */
126
+
127
+ private $myEvent;
128
+
129
+
130
+
131
+ public function getId(): ?int
132
+
133
+ {
134
+
135
+ return $this->id;
136
+
137
+ }
138
+
139
+
140
+
141
+ public function getMyEvent(): ?MyEvent
142
+
143
+ {
144
+
145
+ return $this->myEvent;
146
+
147
+ }
148
+
149
+
150
+
151
+ public function setMyEvent(MyEvent $myEvent): self
152
+
153
+ {
154
+
155
+ $this->myEvent = $myEvent;
156
+
157
+
158
+
159
+ return $this;
160
+
161
+ }
162
+
163
+ }
164
+
165
+ ```
166
+
167
+
168
+
169
+ ```php
170
+
171
+ class MyEventType extends AbstractType
172
+
173
+ {
174
+
175
+ public function buildForm(FormBuilderInterface $builder, array $options)
176
+
177
+ {
178
+
179
+ $builder
180
+
181
+ ->add('myEventSchedule', MyEventScheduleType::class, [
182
+
183
+ 'required' => true,
184
+
185
+ ])
186
+
187
+ ;
188
+
189
+ }
190
+
191
+
192
+
193
+ /**
194
+
195
+ * {@inheritdoc}
196
+
197
+ */
198
+
199
+ public function configureOptions(OptionsResolver $resolver): void
200
+
201
+ {
202
+
203
+ $resolver->setDefaults([
204
+
205
+ 'data_class' => MyEvent::class,
206
+
207
+ ]);
208
+
209
+ }
210
+
211
+ }
212
+
213
+ ```
214
+
215
+
216
+
217
+ ```php
218
+
219
+ /**
220
+
221
+ * @Route("admin/my/event")
222
+
223
+ */
224
+
225
+ class MyEventController extends AbstractController
226
+
227
+ {
228
+
229
+
230
+
231
+ /**
232
+
233
+ * @Route("/new", name="admin_my_event_new", methods={"GET","POST"})
234
+
235
+ */
236
+
237
+ public function new(Request $request): Response
238
+
239
+ {
240
+
241
+ $myEvent = new MyEvent();
242
+
243
+
244
+
245
+ $form = $this->createForm(MyEventType::class, $myEvent);
246
+
247
+ $form->handleRequest($request);
248
+
249
+
250
+
251
+
252
+
253
+ if ($form->isSubmitted() && $form->isValid()) {
254
+
255
+ $entityManager = $this->getDoctrine()->getManager();
256
+
257
+ $entityManager->persist($myEvent);
258
+
259
+ $entityManager->flush();
260
+
261
+
262
+
263
+ $this->addFlash('success', 'イベントを登録しました');
264
+
265
+
266
+
267
+ return $this->redirectToRoute('admin_my_event_index');
268
+
269
+ }
270
+
271
+
272
+
273
+ return $this->render('admin/my_event/new.html.twig', [
274
+
275
+ 'my_event' => $myEvent,
276
+
277
+ 'form' => $form->createView(),
278
+
279
+ ]);
280
+
281
+ }
282
+
283
+
284
+
285
+ ```
286
+
287
+
288
+
289
+ コントローラーにスケージュールの保存処理を書く必要があるのでしょうか?
290
+
291
+ また、フォームが間違っているのでしょうか?
292
+
293
+ どなたか教えていただけないしょうか?
294
+
295
+
296
+
297
+ よろしくお願いいたします。