-
Notifications
You must be signed in to change notification settings - Fork 6
/
Model.php
189 lines (150 loc) · 4.04 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
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
<?php
namespace Pragma\ORM;
use Pragma\DB\DB;
use \PDO;
class Model extends QueryBuilder implements SerializableInterface{
static protected $table_desc = array();
protected $fields = array();
protected $new = true;
protected $desc = array();
public function __construct($tb_name){
parent::__construct($tb_name);
$this->fields = $this->describe();
}
public function __get($attr){
if(array_key_exists($attr, $this->describe())){
return $this->fields[$attr];
}
return null;
}
public function __set($attr, $value){
if(array_key_exists($attr, $this->describe())){
$this->fields[$attr] = $value;
}
}
public function __isset($attr) {
if(array_key_exists($attr, $this->describe())){
return (false === empty($this->fields[$attr]));
}
return null;
}
public function is_new(){
return $this->new;
}
public function get_table(){
return $this->table;
}
public function open($id){
$db = DB::getDB();
$res = $db->query("SELECT * FROM ".$this->table." WHERE id = :id", array(
':id' => array($id, PDO::PARAM_INT)
));
//it must return only one row
$data = $db->fetchrow($res);
if ($data) {
$this->fields = $data;
$this->new = false;
return $this;
}
else return null;
}
public static function find($id){
return self::forge()->where('id', '=', $id)->first();
}
public function openWithFields($data, $whitelist = null){
if( ! empty($data) && isset($data['id']) ){
//whitelist allows to get the description on an object and check if data is correct
//the idea is to optimize by doing the describe only once
if( ! is_null($whitelist) ){
foreach($data as $f => $val){
if( ! array_key_exists($f, $whitelist) ){
unset($data[$f]);
}
}
}
$this->fields = $data;
$this->new = false;
return $this;
}
return null;
}
public function delete(){
if( ! $this->new && ! is_null($this->id) && $this->id > 0){
$db = DB::getDB();
$db->query('DELETE FROM '.$this->table.' WHERE id = :id',
array(':id' => array($this->id, PDO::PARAM_INT)));
}
}
public static function all($idkey = true){
return self::forge()->get_objects($idkey);
}
public static function build($data = array()){
$obj = new static();
$obj->fields = $obj->describe();
$obj->fields = array_merge($obj->fields, $data);
return $obj;
}
public function merge($data){
$this->fields = array_merge($this->fields, $data);
}
public function save(){
$db = DB::getDB();
if($this->new){//INSERT
$sql = 'INSERT INTO `'.$this->table.'` (';
$first = true;
foreach($this->describe() as $col => $default){
if(!$first) $sql .= ', ';
else $first = false;
$sql .= '`'.$col.'`';
}
$sql .= ') VALUES (';
$values = array();
$first = true;
foreach($this->describe() as $col => $default){
if(!$first) $sql .= ', ';
else $first = false;
$sql .= ':'.$col;
$values[':'.$col] = array_key_exists($col, $this->fields) ? $this->$col : '';
}
$sql .= ")";
$res = $db->query($sql, $values);
$this->id = $db->getLastId();
$this->new = false;
}
else{//UPDATE
$sql = 'UPDATE `'.$this->table.'` SET ';
$first = true;
$values = array();
foreach($this->describe() as $col => $default){
if($col != 'id'){//the id is not updatable
if(!$first) $sql .= ', ';
else $first = false;
$sql .= '`'.$col.'` = :'.$col;
$values[':'.$col] = array_key_exists($col, $this->fields) ? $this->$col : '';
}
}
$sql .= ' WHERE id = :id';
$values[':id'] = $this->id;
$db->query($sql, $values);
}
}
public function toJSON(){
return json_encode($this->fields);
}
public function as_array(){
return $this->fields;
}
protected function describe() {
$db = DB::getDB();
if (empty(self::$table_desc[$this->table])) {
foreach ($db->describe($this->table) as $data) {
if (empty($data['default']) && !$data['null']) {
self::$table_desc[$this->table][$data['field']] = '';
} else {
self::$table_desc[$this->table][$data['field']] = $data['default'];
}
}
}
return self::$table_desc[$this->table];
}
}