-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModel.php
More file actions
115 lines (99 loc) · 2.61 KB
/
Model.php
File metadata and controls
115 lines (99 loc) · 2.61 KB
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
<?php
/**
* Resource\Model
* PHP version 7
*
* @category Model
* @package Resource
* @author Bill Rocha <prbr@ymail.com>
* @copyright 2016 Bill Rocha <http://google.com/+BillRocha>
* @license <https://opensource.org/licenses/MIT> MIT
* @version GIT: 0.0.1
* @link http://dbrasil.tk/devbr
*/
namespace Devbr;
use Devbr\Database;
use Config\Devbr\Database as DbConf;
/**
* Model Class
*
* @category Model
* @package Resource
* @author Bill Rocha <prbr@ymail.com>
* @license <https://opensource.org/licenses/MIT> MIT
* @link http://dbrasil.tk/devbr
*/
class Model
{
public $db = null;
public $table = null;
public $error = false;
function __construct()
{
$this->db = new Database(DbConf::get());
}
function getError()
{
return $this->error;
}
function setError($error = true)
{
return $this->error = $error;
}
function setTable($table)
{
$this->table = $table;
}
//Lista todos || paginado
final public function doIndex($start = 0, $len = 30, $search = null)
{
//SEarch
if ($search !== null && is_array($search)) {
$tmp = ' WHERE ';
$and = '';
foreach ($search as $k => $v) {
$tmp .= $and.$k.' LIKE "%'.$v.'%" ';
$and = ' AND ';
}
$search = $tmp;
} else {
$search = '';
}
//Execute
$this->db->query('SELECT * FROM '.$this->table.$search.' LIMIT '.(0+$start).', '.(0+$len));
return $this->db->result();
}
//Lista o selecionado
final public function doShow($id)
{
$this->db->query('SELECT * FROM '.$this->table.' WHERE id = :id', [':id'=>$id]);
$result = $this->db->result();
return $result ? $result : false;
}
//Insert/Update
final public function doSave($values)
{
if (isset($values['id'])) {
$action = 'UPDATE ';
$where = ' WHERE id = :id';
} else {
$action = 'INSERT INTO ';
$where = '';
}
$cols = '';
$vals = [];
foreach ($values as $k => $v) {
if ($k !== 'id') {
$cols .= $k.' = :'.$k.',';
}
$vals[':'.$k] = $v;
}
$cols = substr($cols, 0, -1); //tirando a ultima vírgula
return $this->db->query($action.$this->table.' SET '.$cols.$where, $vals);
}
//Deletar
final public function doDelete($id)
{
return $this->db->query('DELETE FROM '.$this->table.' WHERE id = :id', [':id'=>$id]);
}
}