-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUser.php
More file actions
85 lines (53 loc) · 1.76 KB
/
User.php
File metadata and controls
85 lines (53 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
namespace App\Model;
class User extends \Core\Model {
private static $tableUserColNames = ['id', 'first_name', 'last_name', 'user_name', 'password', 'create_time'];
public function getUserByUserName(string $userName) {
$sql = 'SELECT * FROM users WHERE user_name=?';
$stmt = $this -> dba -> prepare($sql);
$stmt -> setFetchMode(\PDO::FETCH_ASSOC);
if (!$stmt -> execute([$userName]))
throw new \Exception('stmtErr:' . __METHOD__);
$user = $stmt -> fetch();
if (!$user)
throw new \Exception('notFound:' . __METHOD__);
return $user;
}
public function getUserById(int $userId) {
$sql = 'SELECT * FROM users WHERE id=?';
$stmt = $this -> dba -> prepare($sql);
$stmt -> setFetchMode(\PDO::FETCH_ASSOC);
if (!$stmt -> execute([$userId]))
throw new \Exception('stmtErr:' . __METHOD__);
$user = $stmt -> fetch();
if (!$user)
throw new \Exception('notFound:' . __METHOD__);
return $user;
}
public function updateUser(int $userId, array $userKeyValueArr) {
if ($userId < 1)
throw new \InvalidArgumentException(__METHOD__ . ':UserId gerekli');
if (empty($userKeyValueArr))
throw new \InvalidArgumentException(__METHOD__ . ':Uservalues gerekli');
$cols = array_keys($userKeyValueArr);
$values = array_values($userKeyValueArr);
foreach ($cols as $value)
if (!in_array($value, self::$tableUserColNames))
throw new \InvalidArgumentException(__METHOD__ . ':stmErr');
$colSqlArr = [];
foreach ($cols AS $value)
$colSqlArr[] = $value . '=?';
$sql = '
UPDATE users
SET
' . implode(', ', $colSqlArr) . '
WHERE
id=?
';
$values[] = $userId;
$stmt = $this -> dba -> prepare($sql);
if (!$stmt -> execute($values))
throw new \Exception('stmtErr:' . __METHOD__);
return true;
}
}