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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
<?php
class ModelNotFoundException extends Exception {}
class ModelCreateFailedException extends Exception {}
class ModelIllegalAccessException extends Exception {}
class ModelSetFailedException extends Exception {}
abstract class Model {
public static
$table = '',
$primary_key = 'id',
$protected_columns = ['id'],
$fillable_columns = [],
$timestamps = [],
$dates = [],
$booleans = [];
protected $pdo, $data;
const TABLE_PREFIX = '';
public function __construct($pdo, $id) {
$this->pdo = $pdo;
$stmt = $this->pdo->prepare("SELECT * FROM `".self::table()."` WHERE `".static::$primary_key."`=?");
$stmt->execute([$id]);
if ($stmt->rowCount() == 0) {
throw new ModelNotFoundException("The ".static::$table." with id '$id' could not be found.");
}
$this->data = $stmt->fetch(PDO::FETCH_ASSOC);
}
public function __set($key, $value) {
if (!in_array($key, static::$fillable_columns)) {
throw new ModelIllegalAccessException("Column `".self::table()."`.`$key` cannot be edited.");
}
if ($this->data[$key] == $value) {
return;
}
$stmt = $this->pdo->prepare("UPDATE `".self::table()."` SET `$key`=? WHERE `".static::$primary_key."`=?");
$stmt->execute([
$this->mutator($key, $value),
$this->data[static::$primary_key]
]);
if ($stmt->rowCount() != 1) {
throw new ModelSetFailedException("Failed to update `".self::table()."`.`$key` to '$value'.");
}
$this->data[$key] = $value;
}
public function __get($key) {
return $this->accessor($key, $this->data[$key]);
}
public static function create($pdo, $values) {
$class = get_called_class();
$columns = array_combine(static::$fillable_columns, $values);
$questions = [];
foreach ($columns as $column => $value) {
$columns[$column] = $class::mutator($column, $value);
$questions[] = '?';
}
$stmt = $pdo->prepare(
"INSERT INTO `".self::table()."` " .
"(`" . implode('`, `', array_keys($columns)) . "`) " .
"VALUES (" . implode(',', $questions) . ")");
$stmt->execute(array_values($columns));
if ($stmt->rowCount() != 1)
throw new ModelCreateFailedException();
return new $class($pdo, $pdo->lastInsertId());
}
public static function searchIds($pdo, $where = [], $values = []) {
$stmt = $pdo->prepare("SELECT `id` FROM `".static::table()."`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""));
$stmt->execute($values);
$ids = [];
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
$ids[] = $row['id'];
}
return $ids;
}
public static function search($pdo, $where = [], $values = []) {
$class = get_called_class();
$items = [];
foreach (self::searchIds($pdo, $where, $values) as $id) {
$items[] = new $class($pdo, $id);
}
return $items;
}
public static function count($pdo, $where = [], $values = []) {
$class = get_called_class();
$stmt = $pdo->prepare("SELECT COUNT(*) FROM `".static::table()."`" . ((count($where) > 0) ? (" WHERE (" . implode(') AND (', $where) . ")") : ""));
$stmt->execute($values);
return $stmt->fetchColumn();
}
protected static function accessor($key, $value) {
if (is_null($value)) {
return null;
} elseif (in_array($key, static::$booleans)) {
return (bool) $value;
} elseif (in_array($key, static::$dates) || in_array($key, static::$timestamps)) {
return strtotime($value);
} else {
return $value;
}
}
protected static function mutator($key, $value) {
if (in_array($key, static::$dates) && is_int($value)) {
return date('Y-m-d', $value);
} elseif (in_array($key, static::$timestamps) && is_int($value)) {
return date('Y-m-d H:i:s', $value);
} else {
return (string) $value;
}
}
public function delete() {
$stmt = $this->pdo->prepare("DELETE FROM `".self::table()."` WHERE `".static::$primary_key."`=?");
$stmt->execute([$this->data[static::$primary_key]]);
return $stmt->rowCount() != 0;
}
private static function table() {
return self::TABLE_PREFIX . static::$table;
}
}
|