/
CrudClassDemo.php.php
executable file
·215 lines (183 loc) · 5.19 KB
/
CrudClassDemo.php.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
<?php
/**
* Created by PhpStorm.
* User: aa
* Date: 10-Jun-19
* Time: 8:27 PM
*
* Class to implement common database CRUD functions - create read update delete. Work with any table .
* Also included helper classes that should be moved to separate files in a real app.
*/
interface DatabaseInterface
{
public function execute($sqlString, $colNameValues, $colTypes);
public function fetch(PDOStatement $stmt);
public function affectedRows(PDOStatement $stmt);
public function lastInsertId();
public function bindStuff($colNameValues, $colTypes, PDOStatement $stmt): void;
}
class DatabaseWrapper implements DatabaseInterface
{
private $pdo;
/**
* DbClass constructor.
* @param $pdo
*/
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
function lastInsertId()
{
return $this->pdo->lastInsertId();
}
function affectedRows(PDOStatement $stmt)
{
return $stmt->rowCount();
}
function execute($sql, $colNameValues, $colTypes)
{
$stmt = $this->pdo->prepare($sql);
$this->bindStuff($colNameValues, $colTypes, $stmt);
$result = $stmt->execute();
return [$result, $stmt];
}
/**
* @param $colNameValues
* @param $colTypes
* @param $stmt
*/
public function bindStuff($colNameValues, $colTypes, PDOStatement $stmt): void
{
foreach ($colNameValues as $columnName => $value) {
$stmt->bindValue($columnName, $value, $colTypes[$columnName]);
}
}
public function fetch(PDOStatement $stmt)
{
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
class CrudClass
{
protected $db;
protected $tableName;
protected $allColumns;
/**
* Generic class with CRUD insert create update delete functions.
* CrudClass constructor.
* @param DatabaseInterface $db
*/
public function __construct(DatabaseInterface $db)
{
$this->db = $db;
}
/**
* @param $colNameValues
* In format colName => Value
* @return mixed
*
*/
function update($colNameValues)
{
$sql = 'UPDATE ' . $this->tableName . ' SET ';
foreach ($colNameValues as $col => $value) {
$sql .= ' `' . $col . '` = :' . $col . ' , ';
}
$sql = substr($sql, 0, -2);
list($result, $stmt) = $this->db->execute($sql, $colNameValues, $this->allColumns);
if ($result) {
return $this->db->affectedRows($stmt);
}
return $result;
}
/**
* Returns all rows for id
* @param $id
* @return mixed
*/
function fetchForId($id)
{
$sql = ' Select * FROM ' . $this->tableName;
$sql .= ' WHERE id = :id';
list($result, $stmt) = $this->db->execute($sql, ['id' => $id], $this->allColumns);
if ($result) {
$rows = $this->db->fetch($stmt);
return $rows;
}
return $result;
}
/**
* @param $columns
* Array of columns to insert values in
* @param $values
* Multidimensional array of values to insert corresponding to the first column parameter
* @return mixed
*/
function insert($columns, $values)
{
$colCount = 1;
$colTypes = $colNameValues = [];
$sql = ' INSERT INTO ' . $this->tableName;
$sql .= '( ' . implode(' , ', $columns) . ' ) VALUES ';
foreach ($values as $keyOuter => $row) {
$sql .= '( ';
// Construct bindIndex => values array
foreach ($row as $keyRow => $val) {
$sql .= ' ?, ';
$colNameValues[$colCount++] = $val;
}
//populate numbered column types for binding purposes
foreach ($columns as $column) {
$colTypes[$colCount] = $this->allColumns[$column];
}
$sql .= substr($sql, 0, -2) . ' ), ';
}
$sql = substr($sql, 0, -2);
list($result, $stmt) = $this->db->execute($sql, $colNameValues, $colTypes);
if ($result) {
return $this->db->lastInsertId();
}
return $result;
}
/**
* Delete for ID
* @param $id
* @return mixed
* stuff
*/
function delete($id)
{
$sql = 'DELETE FROM ' . $this->tableName;
$sql .= ' WHERE id = :id';
list($result, $stmt) = $this->db->execute($sql, ['id' => $id], $this->allColumns);
if ($result) {
$rows = $this->db->fetch($stmt);
return $rows;
}
if ($result) {
return $this->db->affectedRows($stmt);
}
return $result;
}
}
/*
* SAMPLE USAGE
*/
class Comments extends CrudClass
{
protected $tableName = 'comments';
protected $allColumns = ['id' => PDO::PARAM_INT, 'text' => PDO::PARAM_STR, 'email' => PDO::PARAM_STR, 'name' => PDO::PARAM_STR, 'date_created' => PDO::PARAM_STR];
/**
* Comments constructor.
* @param DatabaseInterface $db
*/
public function __construct(DatabaseInterface $db)
{
parent::__construct($db);
}
}
$pdo = new PDO('DSNSTRING');
$db = new DatabaseWrapper($pdo);
$comments = new Comments($db);
$comments->fetchForId(1);