teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

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

2021/08/07 05:25

投稿

cherry2020
cherry2020

スコア10

title CHANGED
File without changes
body CHANGED
@@ -26,4 +26,246 @@
26
26
 
27
27
  - [チュートリアルの作業環境](https://symfony.com/doc/current/the-fast-track/ja/1-tools.html)と同じように、dockerや、PostgreSQL、php、symfony CLIを使って実践していました。
28
28
  - PostgreSQLについては、docker Hubから、イメージをpullして作っています。
29
- - そもそも`UPDATE文`のコマンドが誤っているのではないかと思い、教えて欲しいです????
29
+ - そもそも`UPDATE文`のコマンドが誤っているのではないかと思い、教えて欲しいです????
30
+
31
+ # 追記 データベースの構造について
32
+
33
+ - `Doctrine`を使用
34
+ - 管理者画面を作成するために`EasyAdmin`を使用
35
+
36
+ #### (1)Conferenceテーブル(Entityディレクトリ配下)
37
+
38
+ - city, string, 255, (nullable)no
39
+ - year, string, 4, (nullable)no
40
+ - isInternational, boolean, (nullable)no
41
+
42
+ #### (2)Commentテーブル(Entityディレクトリ配下)
43
+
44
+ - author, string, 255, (nullable)no
45
+ - text, text, (nullable)no
46
+ - email, string, 255, (nullable)no
47
+ - createdAt, datetime, (nullable)no
48
+ - photoFilename, string, 255, (nullable)yes
49
+
50
+ #### 関連づけ
51
+
52
+ - カンファンレスは n個のコメントを持つので、one-to-many の関連づけをしている
53
+
54
+
55
+
56
+ #### (3)Adminテーブル(Entityディレクトリ配下)
57
+
58
+ - カンファレンスへの参加者が、ユーザー登録をする機能は作らない
59
+ - 管理者を一つのユーザー登録とし、認証するシステムを作る
60
+ - そのために`Userエンティティ`を使って作成した
61
+
62
+
63
+ #### 補足 どうやってAdminテーブル作ったか?
64
+ - 管理者は Doctrine に格納し
65
+ - 管理者のユニークな表示名を username
66
+ - 各ユーザーがパスワードを1つ持つことに(yes)とした
67
+
68
+ ```terminal
69
+ noMacBook-Air guestbook % symfony console make:user Admin
70
+
71
+ Do you want to store user data in the database (via Doctrine)? (yes/no) [yes]:
72
+ > yes
73
+
74
+ Enter a property name that will be the unique "display" name for the user (e.g. email, username, uuid) [email]:
75
+ > username
76
+
77
+ 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).
78
+
79
+ Does this app need to hash/check user passwords? (yes/no) [yes]:
80
+ > yes
81
+
82
+ created: src/Entity/Admin.php
83
+ created: src/Repository/AdminRepository.php
84
+ updated: src/Entity/Admin.php
85
+ updated: config/packages/security.yaml
86
+
87
+
88
+ Success!
89
+
90
+
91
+ ```
92
+
93
+ - 上記のように、マイグレーションファイルを作成するときは、ユーザーネームとパスワードの指定しかしていないが、その後の、SQL文を書くときは、"ロール"を書いた。
94
+
95
+ ```terminal
96
+
97
+ $ symfony run psql -c "INSERT INTO admin (id, username, roles, password) \
98
+ VALUES (nextval('admin_id_seq'), 'admin', '[\"ROLE_ADMIN\"]', \
99
+ '$argon2id$v=19$m=65536,t=4,p=1$BQG+jovPcunctc30xG5PxQ$TiGbx451NKdo+g9vLtfkMy4KjASKSOcnNxjij4gTX1s')"
100
+
101
+
102
+ ```
103
+
104
+
105
+ ###### (参考)Adminテーブルのマイグレーションファイル
106
+
107
+ ```php
108
+ <?php
109
+
110
+ declare(strict_types=1);
111
+
112
+ namespace DoctrineMigrations;
113
+
114
+ use Doctrine\DBAL\Schema\Schema;
115
+ use Doctrine\Migrations\AbstractMigration;
116
+
117
+ /**
118
+ * Auto-generated Migration: Please modify to your needs!
119
+ */
120
+ final class Version20210804210848 extends AbstractMigration
121
+ {
122
+ public function getDescription(): string
123
+ {
124
+ return '';
125
+ }
126
+
127
+ public function up(Schema $schema): void
128
+ {
129
+ // this up() migration is auto-generated, please modify it to your needs
130
+ $this->addSql('CREATE SEQUENCE admin_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
131
+ $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))');
132
+ $this->addSql('CREATE UNIQUE INDEX UNIQ_880E0D76F85E0677 ON admin (username)');
133
+ $this->addSql('CREATE UNIQUE INDEX UNIQ_911533C8989D9B62 ON conference (slug)');
134
+ }
135
+
136
+ public function down(Schema $schema): void
137
+ {
138
+ // this down() migration is auto-generated, please modify it to your needs
139
+ $this->addSql('CREATE SCHEMA public');
140
+ $this->addSql('DROP SEQUENCE admin_id_seq CASCADE');
141
+ $this->addSql('DROP TABLE admin');
142
+ $this->addSql('DROP INDEX UNIQ_911533C8989D9B62');
143
+ }
144
+ }
145
+
146
+
147
+ ```
148
+
149
+
150
+ ###### (参考)Adminテーブルのモデル(エンティティファイル)
151
+
152
+ ```php
153
+ <?php
154
+
155
+ namespace App\Entity;
156
+
157
+ use App\Repository\AdminRepository;
158
+ use Doctrine\ORM\Mapping as ORM;
159
+ use Symfony\Component\Security\Core\User\UserInterface;
160
+
161
+ /**
162
+ * @ORM\Entity(repositoryClass=AdminRepository::class)
163
+ */
164
+ class Admin implements UserInterface
165
+ {
166
+ /**
167
+ * @ORM\Id
168
+ * @ORM\GeneratedValue
169
+ * @ORM\Column(type="integer")
170
+ */
171
+ private $id;
172
+
173
+ /**
174
+ * @ORM\Column(type="string", length=180, unique=true)
175
+ */
176
+ private $username;
177
+
178
+ /**
179
+ * @ORM\Column(type="json")
180
+ */
181
+ private $roles = [];
182
+
183
+ /**
184
+ * @var string The hashed password
185
+ * @ORM\Column(type="string")
186
+ */
187
+ private $password;
188
+
189
+ public function getId(): ?int
190
+ {
191
+ return $this->id;
192
+ }
193
+
194
+ /**
195
+ * A visual identifier that represents this user.
196
+ *
197
+ * @see UserInterface
198
+ */
199
+ public function getUsername(): string
200
+ {
201
+ return (string) $this->username;
202
+ }
203
+
204
+ public function setUsername(string $username): self
205
+ {
206
+ $this->username = $username;
207
+
208
+ return $this;
209
+ }
210
+
211
+ public function __toString(): string
212
+ {
213
+ return $this->username;
214
+ }
215
+
216
+ /**
217
+ * @see UserInterface
218
+ */
219
+ public function getRoles(): array
220
+ {
221
+ $roles = $this->roles;
222
+ // guarantee every user at least has ROLE_USER
223
+ $roles[] = 'ROLE_USER';
224
+
225
+ return array_unique($roles);
226
+ }
227
+
228
+ public function setRoles(array $roles): self
229
+ {
230
+ $this->roles = $roles;
231
+
232
+ return $this;
233
+ }
234
+
235
+ /**
236
+ * @see UserInterface
237
+ */
238
+ public function getPassword(): string
239
+ {
240
+ return $this->password;
241
+ }
242
+
243
+ public function setPassword(string $password): self
244
+ {
245
+ $this->password = $password;
246
+
247
+ return $this;
248
+ }
249
+
250
+ /**
251
+ * Returning a salt is only needed, if you are not using a modern
252
+ * hashing algorithm (e.g. bcrypt or sodium) in your security.yaml.
253
+ *
254
+ * @see UserInterface
255
+ */
256
+ public function getSalt(): ?string
257
+ {
258
+ return null;
259
+ }
260
+
261
+ /**
262
+ * @see UserInterface
263
+ */
264
+ public function eraseCredentials()
265
+ {
266
+ // If you store any temporary, sensitive data on the user, clear it here
267
+ // $this->plainPassword = null;
268
+ }
269
+ }
270
+
271
+ ```