-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.php
More file actions
150 lines (115 loc) · 2.56 KB
/
Copy pathmodel.php
File metadata and controls
150 lines (115 loc) · 2.56 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
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
<?php
class Model
{
protected $table;
protected $data = null;
protected $data_new = null;
// special fields
protected $id = 0;
protected $adate = false;
protected $udate = false;
function __construct($table, $id = 0)
{
$this->table = str_replace("`", "", $table);
$this->id = (int)$id;
}
function __get($name)
{
if(!is_null($this->data_new) && property_exists($this->data_new, $name))
return $this->data_new->$name;
if($this->id)
{
if(is_null($this->data))
$this->data = Db::lookup("SELECT * FROM `{$this->table}` WHERE id=?", $this->id);
if($this->data && property_exists($this->data, $name))
return $this->data->$name;
}
return "";
}
function __set($name, $value)
{
if(is_null($this->data_new))
$this->data_new = new stdClass;
$this->data_new->$name = $value;
}
function set($data)
{
if(is_array($data) || is_object($data))
{
foreach($data as $key => $val)
{
$this->$key = $val;
}
}
}
function save()
{
// update
if($this->id)
{
if(is_null($this->data_new))
return;
$fields = "";
$values = array();
if($this->udate)
$fields = "udate = NOW()";
foreach($this->data_new as $key => $val)
{
if($fields) $fields .= ", ";
$fields .= "`" . str_replace("`", "", $key) . "` = ?";
$values[] = $val;
}
if(count($values))
{
$sql = "UPDATE `{$this->table}` SET $fields WHERE id = " . (int)$this->id;
return Db::query($sql, $values);
}
return true;
}
// insert
else
{
$fields = "";
$values = array();
$explicit_values = "";
// adate
if($this->adate)
{
if($fields) $fields .= ", ";
$fields .= "adate";
if($explicit_values) $explicit_values .= ", ";
$explicit_values .= "NOW()";
}
// udate
if($this->udate)
{
if($fields) $fields .= ", ";
$fields .= "udate";
if($explicit_values) $explicit_values .= ", ";
$explicit_values .= "NOW()";
}
foreach($this->data_new as $key => $val)
{
if($fields) $fields .= ", ";
$fields .= "`" . str_replace("`", "", $key) . "`";
$values[] = $val;
}
$placeholders = rtrim(str_repeat("?, ", count($values)), ", ");
if($explicit_values)
$placeholders = ",{$placeholders}";
$sql = "INSERT INTO `{$this->table}` ({$fields}) VALUES ({$explicit_values}{$placeholders})";
if(Db::query($sql, $values))
{
$this->id = Db::lastInsertId();
return true;
}
return false;
}
}
function delete()
{
if($this->id)
return Db::query("DELETE FROM `{$this->table}` WHERE id = ?", (int)$this->id);
return true;
}
}