-
Notifications
You must be signed in to change notification settings - Fork 0
/
Model.php
151 lines (128 loc) · 3.83 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
<?php
namespace Model;
use Flight;
use Logger;
use Tools;
use Tools\Cls_basic_data;
abstract class Model extends cls_basic_data {
var $dbname = 'test'; // database name
var $tablename = ''; // table name
var $fields = array(); // list of fields in this table
var $primary_key = array(); // column(s) which form the primary key
var $required_keys = array(); // required key specifications (optional)
var $unique_keys = array(); // unique key specifications (optional)
var $with_stamp = false; // created_at, updated_at
var $errors = array(); // array of error messages
public function __construct(array $data = array()) {
parent::__construct($data);
}
public function getTableName () {
return $this->tablename;
}
public static function paging_clause($params, $default_size=0) {
$offset = 0;
if(isset($params['offset']) && is_numeric($params['offset']))
$offset = $params['offset'];
$pagesize = $default_size;
if(isset($params['size']) && is_numeric($params['size']))
$pagesize = $params['size'];
/* NOTE: if we want to fetch all, we cannot specify offset */
if($pagesize == 0)
return "";
else
return " limit $offset, $pagesize";
}
// count
public function getCount () {
$conds = array();
foreach ($this->keys() as $key) {
$conds[] = $key . " = '{$this->$key}' ";
}
$sql = "select count(1) from " . $this->getTableName() . " where " . implode(" and ", $conds) . ";";
return Flight::db()->getOne($sql);
}
// get
public function getRecord ($selected_fields=null, $first_row=true) {
$conds = array();
foreach ($this->keys() as $key) {
$conds[] = $key . " = '{$this->$key}' ";
}
$query_fields = is_null($selected_fields) ? "*" : implode(",", $selected_fields);
$sql = "select " . $query_fields. " from " . $this->getTableName() . " where " . implode(" and ", $conds) . ";";
if ($first_row === true) {
$data = Flight::db()->getRow($sql);
if(!empty($data)){
$this->setData($data);
}
} else {
$data = Flight::db()->getAll($sql);
}
return $data;
}
// update
public function updateRecord () {
$this->reset();
$this->preUpdate();
$conds = array();
foreach ($this->primary_key as $key) {
$conds[] = $key . " = '{$this->$key}' ";
}
$where = implode(" and ", $conds);
return Flight::db()->update($this->getTableName(), $this->getData(), $where);
}
// insert
public function insertRecord () {
$this->reset();
$this->preInsert();
$insert_id = 0;
if($this->validate()) {
$insert_id = Flight::db()->insert($this->getTableName(), $this->getData());
}else{
$insert_id = 0;
}
return $insert_id;
}
// delete
protected function deleteRecord ($where_key) {
$where = $where_key . "='" .$this->$where_key."'";
return Flight::db()->delete($this->getTableName(), $where);
}
// prepare update
protected function preUpdate () {
if ($this->with_stamp === true) {
$this->last_time = date("Y-m-d H:i:s", time());
}
return;
}
// prepare insert
protected function preInsert () {
if ($this->with_stamp === true) {
$this->last_time = date("Y-m-d H:i:s", time());
$this->create_time = $this->last_time;
}
return;
}
// reset
protected function reset () {
foreach ($this->keys() as $key) {
if(!in_array($key, $this->fields)){
unset($this->$key);
}
}
}
// validate
protected function validate () {
$errors = array();
foreach ($this->required_keys as $key) {
if(!isset($this->$key)) {
$errors[$key] = "$key cannot be blank";
}
}
if (count($errors) > 0) {
$this->errors = $errors;
return false;
}
return true;
}
}
?>