Skip to content

Commit 44bdd31

Browse files
committed
Update
1 parent a0dbc76 commit 44bdd31

4 files changed

Lines changed: 80 additions & 46 deletions

File tree

‎src/ErrorController.php‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,38 @@ class ErrorController extends FrontController
2222
* @param int $code
2323
* @param string $message
2424
* @param string $error
25+
* @param bool $render
2526
* @return void
2627
*/
27-
public function __construct($code = 404, $error = null)
28+
public function __construct($code = 404, $error = null, $render = true)
2829
{
2930
// Init configuration
3031
$this->initConfig();
31-
$template = $this->applyFilter('error-template','/system/error');
32-
$type = $this->applyFilter('error-response-type','text/html; charset=utf-8');
33-
Response::setHttpHeaders($code,$type);
34-
$this->render([
35-
'status' => Response::getMessage($code),
36-
'code' => $code,
37-
'error' => $this->translate($error)
38-
], $template);
39-
die();
32+
33+
$message = $this->applyFilter('error-message',Response::getMessage($code));
34+
35+
if ( !$error ) {
36+
$error = $message;
37+
}
38+
39+
if ( $render ) {
40+
41+
$type = $this->applyFilter('error-response-type','text/html;charset=utf-8');
42+
$template = $this->applyFilter('error-template','/system/error');
43+
$error = $this->applyFilter('error',$this->translate($error));
44+
45+
Response::setHttpHeaders($code,$type);
46+
$this->render([
47+
'status' => $message,
48+
'code' => $code,
49+
'error' => $error
50+
], $template);
51+
die();
52+
53+
} else {
54+
55+
$args = $this->applyFilter('error-http-args',[]);
56+
Response::set($error,$args,'error',$code);
57+
}
4058
}
4159
}

‎src/Middleware.php‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private function doCallable()
126126
}
127127

128128
/**
129-
* Instance controller
129+
* Instance controller.
130130
*
131131
* @access private
132132
* @param void
@@ -178,7 +178,7 @@ private function doInstance()
178178
}
179179

180180
/**
181-
* Instance module
181+
* Instance module.
182182
*
183183
* @access private
184184
* @param void
@@ -218,7 +218,7 @@ private function doModuleInstance()
218218
}
219219

220220
/**
221-
* 404
221+
* 404.
222222
*
223223
* @access private
224224
* @param void
@@ -230,7 +230,7 @@ private function do404()
230230
}
231231

232232
/**
233-
* Is front controller
233+
* Is front controller.
234234
*
235235
* @access private
236236
* @param string $class

‎src/Orm.php‎

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function select(OrmQueryInterface $data)
9696
}
9797
$sql = trim("SELECT $column FROM `{$table}` {$where} {$orderby} {$limit}");
9898
$sql .= ';';
99-
return $this->query($sql,$bind,[
99+
return $this->query($sql,[],[
100100
'isSingle' => $isSingle,
101101
'isColumn' => $isColumn,
102102
'isRow' => $isRow,
@@ -115,9 +115,9 @@ public function select(OrmQueryInterface $data)
115115
*/
116116
public function query($sql, $bind = null, $args = [])
117117
{
118-
$isSingle = isset($args['isSingle']) ? $args['isSingle'] : false;
119-
$isColumn = isset($args['isColumn']) ? $args['isColumn'] : false;
120-
$isRow = isset($args['isRow']) ? $args['isRow'] : false;
118+
$isSingle = isset($args['isSingle']) ? $args['isSingle'] : false;
119+
$isColumn = isset($args['isColumn']) ? $args['isColumn'] : false;
120+
$isRow = isset($args['isRow']) ? $args['isRow'] : false;
121121
$fetchMode = isset($args['fetchMode']) ? $args['fetchMode'] : null;
122122

123123
if ( $isSingle ) {
@@ -145,19 +145,19 @@ public function save($id = null)
145145
$id = !empty($this->data[$this->key])
146146
? intval($this->data[$this->key]) : 0;
147147
}
148-
$field = '';
148+
$fields = '';
149149
$columns = Arrayify::keys($this->data);
150150
foreach($columns as $column) {
151151
if ( $column !== $this->key ) {
152-
$field .= "{$column} = :{$column},";
152+
$fields .= "`{$column}` = :{$column},";
153153
}
154154
}
155-
$field = Stringify::subreplace($field,'',-1,1);
155+
$fields = Stringify::subreplace($fields,'',-1,1);
156156
if ( count($columns) > 1 ) {
157-
$sql = "UPDATE `{$this->table}` SET {$field} WHERE {$this->key} = :{$this->key};";
157+
$sql = "UPDATE `{$this->table}` SET {$fields} WHERE `{$this->key}` = :{$this->key};";
158158
if ( $id === 0 ) {
159159
unset($this->data[$this->key]);
160-
$sql = "UPDATE `{$this->table}` SET {$field};";
160+
$sql = "UPDATE `{$this->table}` SET {$fields};";
161161
}
162162
return $this->execute($sql);
163163
}
@@ -175,9 +175,14 @@ public function create() : int
175175
{
176176
$bind = $this->data;
177177
if ( !empty($bind) ) {
178-
$fields = Arrayify::keys($bind);
179-
$field = [implode(',',$fields),":" . implode(',:',$fields)];
180-
$sql = "INSERT INTO `{$this->table}` ({$field[0]}) VALUES ({$field[1]});";
178+
$bind = Arrayify::keys($bind);
179+
$columns = [];
180+
foreach ($bind as $key => $column) {
181+
$columns[$key] = "`{$column}`";
182+
}
183+
$fields = implode(',',$columns);
184+
$values = ':' . implode(',:',$bind);
185+
$sql = "INSERT INTO `{$this->table}` ({$fields}) VALUES ({$values});";
181186
} else {
182187
$sql = "INSERT INTO `{$this->table}` () VALUES ();";
183188
}
@@ -197,7 +202,7 @@ public function delete($id = null) : int
197202
$id = !empty($this->data[$this->key])
198203
? intval($this->data[$this->key]) : 0;
199204
}
200-
$sql = "DELETE FROM `{$this->table}` WHERE {$this->key} = :{$this->key} LIMIT 1;";
205+
$sql = "DELETE FROM `{$this->table}` WHERE `{$this->key}` = :{$this->key} LIMIT 1;";
201206
$bind = [$this->key => $id];
202207
return $this->execute($sql,$bind);
203208
}
@@ -215,31 +220,31 @@ public function find($id = null)
215220
$id = !empty($this->data[$this->key])
216221
? intval($this->data[$this->key]) : 0;
217222
}
218-
$sql = "SELECT * FROM `{$this->table}` WHERE {$this->key} = :{$this->key} LIMIT 1;";
223+
$sql = "SELECT * FROM `{$this->table}` WHERE `{$this->key}` = :{$this->key} LIMIT 1;";
219224
$bind = [$this->key => $id];
220225
$result = $this->db->row($sql,$bind);
221226
return $this->data = ($result) ? $result : null;
222227
}
223228

224229
/**
225-
* Search table objects by fields
230+
* Search table objects by bind
226231
*
227232
* @access public
228-
* @param array $fields
233+
* @param array $bind
229234
* @param array $sort
230235
* @return mixed
231236
*/
232-
public function search($fields = [], $sort = [])
237+
public function search($bind = [], $sort = [])
233238
{
234-
$bind = empty($fields) ? $this->data : $fields;
239+
$bind = empty($bind) ? $this->data : $bind;
235240
$sql = "SELECT * FROM `{$this->table}`";
236241
if ( !empty($bind) ) {
237-
$field = [];
242+
$fields = [];
238243
$columns = Arrayify::keys($bind);
239244
foreach($columns as $column) {
240-
$field [] = "{$column} = :{$column}";
245+
$fields [] = "`{$column}` = :{$column}";
241246
}
242-
$sql .= " WHERE " . implode(" AND ",$field);
247+
$sql .= " WHERE " . implode(" AND ",$fields);
243248
}
244249
if ( !empty($sort) ) {
245250
$sorted = [];
@@ -252,24 +257,24 @@ public function search($fields = [], $sort = [])
252257
}
253258

254259
/**
255-
* Search table object by fields
260+
* Search table object by bind
256261
*
257262
* @access public
258-
* @param array $fields
263+
* @param array $bind
259264
* @param array $sort
260265
* @return mixed
261266
*/
262-
public function searchOne($fields = [], $sort = [])
267+
public function searchOne($bind = [], $sort = [])
263268
{
264-
$bind = empty($fields) ? $this->data : $fields;
269+
$bind = empty($bind) ? $this->data : $bind;
265270
$sql = "SELECT * FROM `{$this->table}`";
266271
if ( !empty($bind) ) {
267-
$field = [];
272+
$fields = [];
268273
$columns = Arrayify::keys($bind);
269274
foreach($columns as $column) {
270-
$field [] = "{$column} = :{$column}";
275+
$fields[] = "`{$column}` = :{$column}";
271276
}
272-
$sql .= ' WHERE ' . implode(' AND ',$field);
277+
$sql .= ' WHERE ' . implode(' AND ',$fields);
273278
}
274279
if ( !empty($sort) ) {
275280
$sorted = [];
@@ -391,7 +396,7 @@ public function deleteAll($table = '') : int
391396
}
392397

393398
/**
394-
* Delete all from table
399+
* Reset table Id.
395400
*
396401
* @access public
397402
* @param string $table
@@ -446,9 +451,22 @@ public function createDatabase()
446451
die("ERROR : {$e->getMessage()}");
447452
}
448453
}
454+
455+
/**
456+
* Check table.
457+
*
458+
* @access public
459+
* @param string $table
460+
* @return bool
461+
*/
462+
public function hasTable($table = '')
463+
{
464+
$sql = "SHOW TABLES LIKE '{$table}';";
465+
return (bool)$this->db->query($sql);
466+
}
449467

450468
/**
451-
* Get application tables
469+
* Get tables.
452470
*
453471
* @access public
454472
* @param void

‎src/OrmQuery.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ public function __construct($query = [])
4242
private function setDefault($query = [])
4343
{
4444
$query = Arrayify::merge([
45-
4645
'table' => '',
4746
'column' => '*',
4847
'where' => '',
@@ -53,7 +52,6 @@ private function setDefault($query = [])
5352
'isColumn' => false,
5453
'isRow' => false,
5554
'fetchMode' => null
56-
5755
], $query);
5856

5957

0 commit comments

Comments
 (0)