質問編集履歴

1

コードの追加(UserRepository,StatusRepository)、環境の説明追加

2019/08/10 11:38

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -22,6 +22,8 @@
22
22
 
23
23
  ### 該当のソースコード
24
24
 
25
+ DbRepository.php
26
+
25
27
 
26
28
 
27
29
  ```php
@@ -100,6 +102,156 @@
100
102
 
101
103
 
102
104
 
105
+ UserRepository.php
106
+
107
+ ```php
108
+
109
+ <?php
110
+
111
+
112
+
113
+ class UserRepository extends DbRepository
114
+
115
+ {
116
+
117
+ public function insert($user_name, $password)
118
+
119
+ {
120
+
121
+ $password = $this->hashPassword($password);
122
+
123
+ $now = new DateTime();
124
+
125
+
126
+
127
+ $sql = "insert into user(user_name, password, created_at) values(:user_name, :password, :created_at)";
128
+
129
+ $stmt = $this->execute($sql, array(
130
+
131
+ ':user_name' => $user_name,
132
+
133
+ ':password' => $password,
134
+
135
+ ':created_at' => $now->format('Y-m-d H:i:s'),
136
+
137
+ ));
138
+
139
+ }
140
+
141
+
142
+
143
+ public function hashPassword($password)
144
+
145
+ {
146
+
147
+ return sha1($password . 'SecretKey');
148
+
149
+ }
150
+
151
+
152
+
153
+ public function fetchByUserName($user_name)
154
+
155
+ {
156
+
157
+ $sql = "select * from user where user_name = :user_name";
158
+
159
+
160
+
161
+ return $this->fetch($sql, array(':user_name' => $user_name));
162
+
163
+ }
164
+
165
+
166
+
167
+ public function isUniqueUserName($user_name)
168
+
169
+ {
170
+
171
+ $sql = "select count(id) as count from user where user_name = :user_name";
172
+
173
+
174
+
175
+ $row = $this->fetch($sql, array(':user_name' => $user_name));
176
+
177
+ if($row['count'] === '0'){
178
+
179
+ return true;
180
+
181
+ }
182
+
183
+
184
+
185
+ return false;
186
+
187
+ }
188
+
189
+ }
190
+
191
+
192
+
193
+ ```
194
+
195
+
196
+
197
+ StatusRepository.php
198
+
199
+ ```
200
+
201
+ <?php
202
+
203
+
204
+
205
+ class StatusRepository extends DbRepository
206
+
207
+ {
208
+
209
+ public function insert($user_id, $body)
210
+
211
+ {
212
+
213
+ $now = new DateTime();
214
+
215
+
216
+
217
+ $sql = "insert into status(user_id, body, created_at) values(:user_id, :body, :created_at)";
218
+
219
+
220
+
221
+ $stmt = $this->execute($sql, array(
222
+
223
+ ':user_id' => $user_id,
224
+
225
+ ':body' => $body,
226
+
227
+ ':created_at' => $now->format('Y-m-d H:i:s'),
228
+
229
+ ));
230
+
231
+ }
232
+
233
+
234
+
235
+ public function fetchAllPersonalArchivesByUserId($user_id)
236
+
237
+ {
238
+
239
+ $sql = "select a.*, u.user_name from status a left join user u on a.user_id = u.id where u.id = :user_id order by a.created_at desc";
240
+
241
+
242
+
243
+ return $this->fetchAll($sql, array(':user_id' => $user_id));
244
+
245
+ }
246
+
247
+ }
248
+
249
+
250
+
251
+ ```
252
+
253
+
254
+
103
255
  ### 試したこと
104
256
 
105
257
 
@@ -113,3 +265,5 @@
113
265
 
114
266
 
115
267
  php 7.3.8
268
+
269
+ Xampp v.3.2.4