-
Notifications
You must be signed in to change notification settings - Fork 15
/
Model.php
133 lines (123 loc) · 4.29 KB
/
Model.php
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
<?php
namespace My\Engine;
class Model {
public $connect;
public function __construct()
{
$connect = getenv('PDO_CONNECT');
$user = getenv('PDO_USER');
$password = getenv('PDO_PASSWORD');
$this->connect = new \PDO($connect, $user, $password);
$this->connect->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->connect->exec("SET CHARACTER SET utf8");
}
static public function all()
{
$model = new static;
$result = $model->execute('SELECT * FROM '.$model->table);
return $result;
}
static public function find($id, $key = false)
{
$model = new static;
if (!$key) {
$key = $model->key;
}
$result = $model->execute('SELECT * FROM '.$model->table.' WHERE '.$key.' = ?', [$id]);
return $result;
}
static public function findBy($data)
{
$model = new static;
$sql = 'SELECT * FROM '.$model->table.' WHERE ';
$values = [];
foreach ($data as $key => $value) {
$sql .= $key.' = ? AND ';
$values[] = $value;
}
$sql = trim($sql, ' AND ');
$result = $model->execute($sql, $values);
return $result;
}
static public function save($data)
{
try {
$model = new static;
$attributes = array_flip($model->attributes);
$data = array_filter($data, function ($data) use ($attributes) {
return isset($attributes[$data]);
}, ARRAY_FILTER_USE_KEY);
$sql = 'UPDATE '.$model->table.' SET ';
if (empty($data[$model->key])) {
throw new \Exception('Not found primary key');
}
$primary_key = $data[$model->key];
unset($data[$model->key]);
foreach ($data as $key => $value) {
$sql .= ''.$key.' = :'.$key.', ';
}
$sql = trim($sql, ', ');
$sql .= ' WHERE '.$model->key.' = :'.$model->key;
$statement = $model->connect->prepare($sql);
foreach ($data as $column => $value) {
$statement->bindValue(':'.$column, $value);
}
$statement->bindValue(':'.$model->key, $primary_key);
try {
$statement->execute();
return $model->connect->lastInsertId();
} catch(PDOExecption $e) {
$model->connect->rollback();
return "Error" . $e->getMessage();
}
} catch( PDOExecption $e ) {
return "Error" . $e->getMessage();
}
}
static public function add($data)
{
try {
$model = new static;
$attributes = array_flip($model->attributes);
$data = array_filter($data, function ($data) use ($attributes) {
return isset($attributes[$data]);
}, ARRAY_FILTER_USE_KEY);
$sql = 'INSERT INTO '.$model->table.' (';
foreach ($data as $column => $value) {
$sql .= '`'.$column.'`, ';
}
$sql = trim($sql, ', ');
$sql .= ') VALUES (';
foreach ($data as $column => $value) {
$sql .= ':'.$column.', ';
}
$sql = trim($sql, ', ');
$sql .= ')';
$statement = $model->connect->prepare($sql);
foreach ($data as $column => $value) {
$statement->bindValue(':'.$column, $value);
}
try {
$statement->execute();
return $model->connect->lastInsertId();
} catch(PDOExecption $e) {
$model->connect->rollback();
return "Error" . $e->getMessage();
}
} catch( PDOExecption $e ) {
return "Error" . $e->getMessage();
}
}
public function execute($sql, $params = []) {
$attributes = array_flip($this->attributes);
$result = [];
$statement = $this->connect->prepare($sql);
$statement->execute($params);
foreach($statement as $row) {
$result[] = array_filter($row, function ($data) use ($attributes) {
return isset($attributes[$data]);
}, ARRAY_FILTER_USE_KEY);
}
return $result;
}
}