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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
<?php
namespace Psecio\Gatekeeper\DataSource;
class Mysql extends \Psecio\Gatekeeper\DataSource
{
/**
* PDO instance
* @var \PDO
*/
protected $db;
/**
* Create our PDO connection, then call parent
*
* @param array $config Configuration options
* @param \PDO $pdo PDO instance
*/
public function __construct(array $config, \PDO $pdo = null)
{
$pdo = ($pdo === null) ? $this->buildPdo($config) : $pdo;
$this->setDb($pdo);
parent::__construct($config);
}
/**
* Build the PDO instance
*
* @param array $config Configuration options
* @return \PDO instance
*/
public function buildPdo(array $config)
{
return new \PDO(
'mysql:dbname='.$config['name'].';host='.$config['host'].';charset=utf8',
$config['username'], $config['password']
);
}
/**
* Get the set PDO instance
*
* @return \PDO instance
*/
public function getDb()
{
return $this->db;
}
/**
* Set the PDO instance
*
* @param \PDO $db PDO instance
*/
public function setDb($db)
{
$this->db = $db;
}
/**
* Save the model and its data (either create or update)
*
* @param \Modler\Model $model Model instance
* @return boolean Success/fail of save action
*/
public function save(\Modler\Model $model)
{
$data = $model->toArray();
// see if we have any pre-save
foreach ($data as $name => $value) {
$preMethod = 'pre'.ucwords($name);
if (method_exists($model, $preMethod)) {
$model->$name = $model->$preMethod($value);
}
}
if ($model->id === null) {
return $this->create($model);
} else {
return $this->update($model);
}
}
/**
* Create the record based on the data from the model
*
* @param \Modler\Model $model Model instance
* @return boolean Success/fail of create action
*/
public function create(\Modler\Model $model)
{
$relations = array();
$properties = $model->getProperties();
$data = $model->toArray();
// Remove the ones without a column
foreach ($data as $index => $item) {
// Check the property to see if it's a relation
if ($properties[$index]['type'] == 'relation') {
$relations[$index] = $item;
unset($data[$index]);
}
}
$data['created'] = date('Y-m-d H:i:s');
$data['updated'] = date('Y-m-d H:i:s');
list($columns, $bind) = $this->setup($data);
foreach ($columns as $index => $column) {
$colName = $properties[$column]['column'];
$columns[$index] = $colName;
}
$sql = 'insert into '.$model->getTableName()
.' ('.implode(',', $columns).') values ('.implode(',', array_values($bind)).')';
$result = $this->execute($sql, $data);
if ($result !== false) {
$model->id = $this->getDb()->lastInsertId();
// Now handle the relations - for each of them, get the model, make it and save it
foreach ($relations as $index => $item) {
$relation = $properties[$index];
$instance = new $relation['relation']['model']($this);
$instance->create($model, $item);
}
}
return $result;
}
/**
* Update a record
*
* @param \Modler\Model $model Model instance
* @return boolean Success/fail of operation
*/
public function update(\Modler\Model $model)
{
$data = $model->toArray();
$data['created'] = date('Y-m-d H:i:s');
$data['updated'] = date('Y-m-d H:i:s');
list($columns, $bind) = $this->setup($data);
$update = array();
$properties = $model->getProperties();
foreach ($bind as $column => $name) {
$colName = $properties[$column]['column'];
$update[] = $colName.' = '.$name;
}
$sql = 'update '.$model->getTableName().' set '.implode(',', $update).' where ID = '.$model->id;
return $this->execute($sql, $data);
}
/**
* Delete a record represented by the model
*
* @param \Modler\Model $model Model instance
* @return boolean Success/failure of deletion
*/
public function delete(\Modler\Model $model)
{
$where = $model->toArray();
$properties = $model->getProperties();
list($columns, $bind) = $this->setup($where);
$update = array();
foreach ($bind as $column => $name) {
// See if we keep to transfer it over to a column name
if (array_key_exists($column, $properties)) {
$column = $properties[$column]['column'];
}
$update[] = $column.' = '.$name;
}
$sql = 'delete from '.$model->getTableName().' where '.implode(' and ', $update);
return $this->execute($sql, $model->toArray());
}
/**
* Find records matching the "where" data given
* All "where" options are appended via "and"
*
* @param \Modler\Model $model Model instance
* @param array $where Data to use in "where" statement
* @param boolean $multiple Force return of single/multiple
* @return array Fetched data
*/
public function find(\Modler\Model $model, array $where = array(), $multiple = false)
{
$properties = $model->getProperties();
list($columns, $bind) = $this->setup($where);
$update = array();
foreach ($bind as $column => $name) {
// See if we keep to transfer it over to a column name
if (array_key_exists($column, $properties)) {
$column = $properties[$column]['column'];
}
$update[] = $column.' = '.$name;
}
$sql = 'select * from '.$model->getTableName();
if (!empty($update)) {
$sql .= ' where '.implode(' and ', $update);
}
$result = $this->fetch($sql, $where);
if ($result !== false && count($result) == 1 && $multiple === false) {
$model->load($result[0]);
return $model;
} elseif (count($result) > 1 || $multiple === true){
// Make a collection instead
$modelClass = get_class($model);
$collectionNs = str_replace('Model', 'Collection', $modelClass);
if (!class_exists($collectionNs)) {
throw new \InvalidArgumentException('Collection "'.$collectionNs.'" is invalid!');
}
$collection = new $collectionNs($this);
foreach ($result as $item) {
$itemModel = new $modelClass($this, $item);
$collection->add($itemModel);
}
return $collection;
}
return $model;
}
/**
* Find count of entities by where conditions.
* All where conditions applied with AND
*
* @param \Modler\Model $model Model instance
* @param array $where Data to use in "where" statement
* @return array Fetched data
*/
public function count(\Modler\Model $model, array $where = array())
{
$properties = $model->getProperties();
list($columns, $bind) = $this->setup($where);
$update = array();
foreach ($bind as $column => $name) {
// See if we keep to transfer it over to a column name
if (array_key_exists($column, $properties)) {
$column = $properties[$column]['column'];
}
$update[] = $column.' = '.$name;
}
$sql = 'select count(*) as `count` from '.$model->getTableName();
if (!empty($update)) {
$sql .= ' where '.implode(' and ', $update);
}
$result = $this->fetch($sql, $where, true);
return $result;
}
/**
* Execute the request (not a fetch)
*
* @param string $sql SQL statement to execute
* @param array $data Data to use in execution
* @return boolean Success/fail of the operation
*/
public function execute($sql, array $data)
{
$sth = $this->getDb()->prepare($sql);
$result = $sth->execute($data);
if ($result === false) {
$error = $sth->errorInfo();
$this->lastError = 'DB ERROR: ['.$sth->errorCode().'] '.$error[2];
}
return $result;
}
/**
* Fetch the data matching the results of the SQL operation
*
* @param string $sql SQL statement
* @param array $data Data to use in fetch operation
* @param boolean $single Only fetch a single record
* @return array|boolean Fetched data or boolean false on error
*/
public function fetch($sql, $data, $single = false)
{
$sth = $this->getDb()->prepare($sql);
$result = $sth->execute($data);
if ($result === false) {
$error = $sth->errorInfo();
$this->lastError = 'DB ERROR: ['.$sth->errorCode().'] '.$error[2];
return false;
}
$results = $sth->fetchAll(\PDO::FETCH_ASSOC);
return ($single === true) ? array_shift($results) : $results;
}
/**
* "Set up" the needed values for the database requests
* (for binding to queries)
*
* @param array $data Data to "set up"
* @return array Set containing the columns and bind values
*/
public function setup(array $data)
{
$bind = array();
foreach ($data as $column => $value) {
$bind[$column] = ':'.$column;
}
return array(array_keys($data), $bind);
}
/**
* Return the last error for the data source
*
* @return string Error string
*/
public function getLastError()
{
return $this->lastError;
}
}
|