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

質問編集履歴

1

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

2019/08/10 11:38

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -10,6 +10,7 @@
10
10
  Fatal error: Uncaught Error: Call to a member function prepare() on bool in C:\xampp\htdocs\php-blog.localhost\core\DbRepository.php:19 Stack trace: #0 C:\xampp\htdocs\php-blog.localhost\core\DbRepository.php(32): DbRepository->execute('select a.*, u.u...', Array) #1 C:\xampp\htdocs\php-blog.localhost\models\StatusRepository.php(22): DbRepository->fetchAll('select a.*, u.u...', Array) #2 C:\xampp\htdocs\php-blog.localhost\controllers\StatusController.php(8): StatusRepository->fetchAllPersonalArchivesByUserId(NULL) #3 C:\xampp\htdocs\php-blog.localhost\core\Controller.php(41): StatusController->indexAction(Array) #4 C:\xampp\htdocs\php-blog.localhost\core\Application.php(123): Controller->run('index', Array) #5 C:\xampp\htdocs\php-blog.localhost\core\Application.php(103): Application->runAction('status', 'index', Array) #6 C:\xampp\htdocs\php-blog.localhost\web\index_dev.php(7): Application->run() #7 {main} thrown in **C:\xampp\htdocs\php-blog.localhost\core\DbRepository.php on line 19**
11
11
 
12
12
  ### 該当のソースコード
13
+ DbRepository.php
13
14
 
14
15
  ```php
15
16
  <?php
@@ -49,10 +50,86 @@
49
50
 
50
51
  ```
51
52
 
53
+ UserRepository.php
54
+ ```php
55
+ <?php
56
+
57
+ class UserRepository extends DbRepository
58
+ {
59
+ public function insert($user_name, $password)
60
+ {
61
+ $password = $this->hashPassword($password);
62
+ $now = new DateTime();
63
+
64
+ $sql = "insert into user(user_name, password, created_at) values(:user_name, :password, :created_at)";
65
+ $stmt = $this->execute($sql, array(
66
+ ':user_name' => $user_name,
67
+ ':password' => $password,
68
+ ':created_at' => $now->format('Y-m-d H:i:s'),
69
+ ));
70
+ }
71
+
72
+ public function hashPassword($password)
73
+ {
74
+ return sha1($password . 'SecretKey');
75
+ }
76
+
77
+ public function fetchByUserName($user_name)
78
+ {
79
+ $sql = "select * from user where user_name = :user_name";
80
+
81
+ return $this->fetch($sql, array(':user_name' => $user_name));
82
+ }
83
+
84
+ public function isUniqueUserName($user_name)
85
+ {
86
+ $sql = "select count(id) as count from user where user_name = :user_name";
87
+
88
+ $row = $this->fetch($sql, array(':user_name' => $user_name));
89
+ if($row['count'] === '0'){
90
+ return true;
91
+ }
92
+
93
+ return false;
94
+ }
95
+ }
96
+
97
+ ```
98
+
99
+ StatusRepository.php
100
+ ```
101
+ <?php
102
+
103
+ class StatusRepository extends DbRepository
104
+ {
105
+ public function insert($user_id, $body)
106
+ {
107
+ $now = new DateTime();
108
+
109
+ $sql = "insert into status(user_id, body, created_at) values(:user_id, :body, :created_at)";
110
+
111
+ $stmt = $this->execute($sql, array(
112
+ ':user_id' => $user_id,
113
+ ':body' => $body,
114
+ ':created_at' => $now->format('Y-m-d H:i:s'),
115
+ ));
116
+ }
117
+
118
+ public function fetchAllPersonalArchivesByUserId($user_id)
119
+ {
120
+ $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";
121
+
122
+ return $this->fetchAll($sql, array(':user_id' => $user_id));
123
+ }
124
+ }
125
+
126
+ ```
127
+
52
128
  ### 試したこと
53
129
 
54
130
  本に書かれている通りのコードを写経しています。
55
131
 
56
132
  ### 補足情報(FW/ツールのバージョンなど)
57
133
 
58
- php 7.3.8
134
+ php 7.3.8
135
+ Xampp v.3.2.4