質問編集履歴

1

テーブル構造について追記しました。

2021/08/07 05:25

投稿

cherry2020
cherry2020

スコア10

test CHANGED
File without changes
test CHANGED
@@ -55,3 +55,487 @@
55
55
  - PostgreSQLについては、docker Hubから、イメージをpullして作っています。
56
56
 
57
57
  - そもそも`UPDATE文`のコマンドが誤っているのではないかと思い、教えて欲しいです????
58
+
59
+
60
+
61
+ # 追記 データベースの構造について
62
+
63
+
64
+
65
+ - `Doctrine`を使用
66
+
67
+ - 管理者画面を作成するために`EasyAdmin`を使用
68
+
69
+
70
+
71
+ #### (1)Conferenceテーブル(Entityディレクトリ配下)
72
+
73
+
74
+
75
+ - city, string, 255, (nullable)no
76
+
77
+ - year, string, 4, (nullable)no
78
+
79
+ - isInternational, boolean, (nullable)no
80
+
81
+
82
+
83
+ #### (2)Commentテーブル(Entityディレクトリ配下)
84
+
85
+
86
+
87
+ - author, string, 255, (nullable)no
88
+
89
+ - text, text, (nullable)no
90
+
91
+ - email, string, 255, (nullable)no
92
+
93
+ - createdAt, datetime, (nullable)no
94
+
95
+ - photoFilename, string, 255, (nullable)yes
96
+
97
+
98
+
99
+ #### 関連づけ
100
+
101
+
102
+
103
+ - カンファンレスは n個のコメントを持つので、one-to-many の関連づけをしている
104
+
105
+
106
+
107
+
108
+
109
+
110
+
111
+ #### (3)Adminテーブル(Entityディレクトリ配下)
112
+
113
+
114
+
115
+ - カンファレンスへの参加者が、ユーザー登録をする機能は作らない
116
+
117
+ - 管理者を一つのユーザー登録とし、認証するシステムを作る
118
+
119
+ - そのために`Userエンティティ`を使って作成した
120
+
121
+
122
+
123
+
124
+
125
+ #### 補足 どうやってAdminテーブル作ったか?
126
+
127
+ - 管理者は Doctrine に格納し
128
+
129
+ - 管理者のユニークな表示名を username
130
+
131
+ - 各ユーザーがパスワードを1つ持つことに(yes)とした
132
+
133
+
134
+
135
+ ```terminal
136
+
137
+ noMacBook-Air guestbook % symfony console make:user Admin
138
+
139
+
140
+
141
+ Do you want to store user data in the database (via Doctrine)? (yes/no) [yes]:
142
+
143
+ > yes
144
+
145
+
146
+
147
+ Enter a property name that will be the unique "display" name for the user (e.g. email, username, uuid) [email]:
148
+
149
+ > username
150
+
151
+
152
+
153
+ Will this app need to hash/check user passwords? Choose No if passwords are not needed or will be checked/hashed by some other system (e.g. a single sign-on server).
154
+
155
+
156
+
157
+ Does this app need to hash/check user passwords? (yes/no) [yes]:
158
+
159
+ > yes
160
+
161
+
162
+
163
+ created: src/Entity/Admin.php
164
+
165
+ created: src/Repository/AdminRepository.php
166
+
167
+ updated: src/Entity/Admin.php
168
+
169
+ updated: config/packages/security.yaml
170
+
171
+
172
+
173
+
174
+
175
+ Success!
176
+
177
+
178
+
179
+
180
+
181
+ ```
182
+
183
+
184
+
185
+ - 上記のように、マイグレーションファイルを作成するときは、ユーザーネームとパスワードの指定しかしていないが、その後の、SQL文を書くときは、"ロール"を書いた。
186
+
187
+
188
+
189
+ ```terminal
190
+
191
+
192
+
193
+ $ symfony run psql -c "INSERT INTO admin (id, username, roles, password) \
194
+
195
+ VALUES (nextval('admin_id_seq'), 'admin', '[\"ROLE_ADMIN\"]', \
196
+
197
+ '$argon2id$v=19$m=65536,t=4,p=1$BQG+jovPcunctc30xG5PxQ$TiGbx451NKdo+g9vLtfkMy4KjASKSOcnNxjij4gTX1s')"
198
+
199
+
200
+
201
+
202
+
203
+ ```
204
+
205
+
206
+
207
+
208
+
209
+ ###### (参考)Adminテーブルのマイグレーションファイル
210
+
211
+
212
+
213
+ ```php
214
+
215
+ <?php
216
+
217
+
218
+
219
+ declare(strict_types=1);
220
+
221
+
222
+
223
+ namespace DoctrineMigrations;
224
+
225
+
226
+
227
+ use Doctrine\DBAL\Schema\Schema;
228
+
229
+ use Doctrine\Migrations\AbstractMigration;
230
+
231
+
232
+
233
+ /**
234
+
235
+ * Auto-generated Migration: Please modify to your needs!
236
+
237
+ */
238
+
239
+ final class Version20210804210848 extends AbstractMigration
240
+
241
+ {
242
+
243
+ public function getDescription(): string
244
+
245
+ {
246
+
247
+ return '';
248
+
249
+ }
250
+
251
+
252
+
253
+ public function up(Schema $schema): void
254
+
255
+ {
256
+
257
+ // this up() migration is auto-generated, please modify it to your needs
258
+
259
+ $this->addSql('CREATE SEQUENCE admin_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
260
+
261
+ $this->addSql('CREATE TABLE admin (id INT NOT NULL, username VARCHAR(180) NOT NULL, roles JSON NOT NULL, password VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
262
+
263
+ $this->addSql('CREATE UNIQUE INDEX UNIQ_880E0D76F85E0677 ON admin (username)');
264
+
265
+ $this->addSql('CREATE UNIQUE INDEX UNIQ_911533C8989D9B62 ON conference (slug)');
266
+
267
+ }
268
+
269
+
270
+
271
+ public function down(Schema $schema): void
272
+
273
+ {
274
+
275
+ // this down() migration is auto-generated, please modify it to your needs
276
+
277
+ $this->addSql('CREATE SCHEMA public');
278
+
279
+ $this->addSql('DROP SEQUENCE admin_id_seq CASCADE');
280
+
281
+ $this->addSql('DROP TABLE admin');
282
+
283
+ $this->addSql('DROP INDEX UNIQ_911533C8989D9B62');
284
+
285
+ }
286
+
287
+ }
288
+
289
+
290
+
291
+
292
+
293
+ ```
294
+
295
+
296
+
297
+
298
+
299
+ ###### (参考)Adminテーブルのモデル(エンティティファイル)
300
+
301
+
302
+
303
+ ```php
304
+
305
+ <?php
306
+
307
+
308
+
309
+ namespace App\Entity;
310
+
311
+
312
+
313
+ use App\Repository\AdminRepository;
314
+
315
+ use Doctrine\ORM\Mapping as ORM;
316
+
317
+ use Symfony\Component\Security\Core\User\UserInterface;
318
+
319
+
320
+
321
+ /**
322
+
323
+ * @ORM\Entity(repositoryClass=AdminRepository::class)
324
+
325
+ */
326
+
327
+ class Admin implements UserInterface
328
+
329
+ {
330
+
331
+ /**
332
+
333
+ * @ORM\Id
334
+
335
+ * @ORM\GeneratedValue
336
+
337
+ * @ORM\Column(type="integer")
338
+
339
+ */
340
+
341
+ private $id;
342
+
343
+
344
+
345
+ /**
346
+
347
+ * @ORM\Column(type="string", length=180, unique=true)
348
+
349
+ */
350
+
351
+ private $username;
352
+
353
+
354
+
355
+ /**
356
+
357
+ * @ORM\Column(type="json")
358
+
359
+ */
360
+
361
+ private $roles = [];
362
+
363
+
364
+
365
+ /**
366
+
367
+ * @var string The hashed password
368
+
369
+ * @ORM\Column(type="string")
370
+
371
+ */
372
+
373
+ private $password;
374
+
375
+
376
+
377
+ public function getId(): ?int
378
+
379
+ {
380
+
381
+ return $this->id;
382
+
383
+ }
384
+
385
+
386
+
387
+ /**
388
+
389
+ * A visual identifier that represents this user.
390
+
391
+ *
392
+
393
+ * @see UserInterface
394
+
395
+ */
396
+
397
+ public function getUsername(): string
398
+
399
+ {
400
+
401
+ return (string) $this->username;
402
+
403
+ }
404
+
405
+
406
+
407
+ public function setUsername(string $username): self
408
+
409
+ {
410
+
411
+ $this->username = $username;
412
+
413
+
414
+
415
+ return $this;
416
+
417
+ }
418
+
419
+
420
+
421
+ public function __toString(): string
422
+
423
+ {
424
+
425
+ return $this->username;
426
+
427
+ }
428
+
429
+
430
+
431
+ /**
432
+
433
+ * @see UserInterface
434
+
435
+ */
436
+
437
+ public function getRoles(): array
438
+
439
+ {
440
+
441
+ $roles = $this->roles;
442
+
443
+ // guarantee every user at least has ROLE_USER
444
+
445
+ $roles[] = 'ROLE_USER';
446
+
447
+
448
+
449
+ return array_unique($roles);
450
+
451
+ }
452
+
453
+
454
+
455
+ public function setRoles(array $roles): self
456
+
457
+ {
458
+
459
+ $this->roles = $roles;
460
+
461
+
462
+
463
+ return $this;
464
+
465
+ }
466
+
467
+
468
+
469
+ /**
470
+
471
+ * @see UserInterface
472
+
473
+ */
474
+
475
+ public function getPassword(): string
476
+
477
+ {
478
+
479
+ return $this->password;
480
+
481
+ }
482
+
483
+
484
+
485
+ public function setPassword(string $password): self
486
+
487
+ {
488
+
489
+ $this->password = $password;
490
+
491
+
492
+
493
+ return $this;
494
+
495
+ }
496
+
497
+
498
+
499
+ /**
500
+
501
+ * Returning a salt is only needed, if you are not using a modern
502
+
503
+ * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
504
+
505
+ *
506
+
507
+ * @see UserInterface
508
+
509
+ */
510
+
511
+ public function getSalt(): ?string
512
+
513
+ {
514
+
515
+ return null;
516
+
517
+ }
518
+
519
+
520
+
521
+ /**
522
+
523
+ * @see UserInterface
524
+
525
+ */
526
+
527
+ public function eraseCredentials()
528
+
529
+ {
530
+
531
+ // If you store any temporary, sensitive data on the user, clear it here
532
+
533
+ // $this->plainPassword = null;
534
+
535
+ }
536
+
537
+ }
538
+
539
+
540
+
541
+ ```