/
Model.php
188 lines (170 loc) · 5.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
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
<?php
/**
* KK-Framework
* Author: kookxiang <r18@ikk.me>
*/
namespace Core;
use ReflectionObject;
use ReflectionProperty;
abstract class Model
{
/**
* @ignore
* @var LazyLoadObject[] $lazyLoadObj
*/
protected $_lazyLoad = array();
const SAVE_AUTO = 0;
const SAVE_INSERT = 1;
const SAVE_UPDATE = 2;
public function __construct()
{
$this->lazyLoad();
}
protected function lazyLoad()
{
}
public function delete()
{
$reflection = new ReflectionObject($this);
$primaryKey = $this->getPrimaryKeyName($reflection);
$property = $reflection->getProperty($primaryKey);
$property->setAccessible(true);
$primaryValue = $property->getValue($this);
if (!$primaryValue) {
throw new Error('Cannot delete object without id');
}
$tableName = $this->getTableName($reflection);
$statement = Database::getInstance()->prepare("DELETE FROM `{$tableName}` WHERE `{$primaryKey}`=:value");
$statement->bindValue(':value', $primaryValue);
$statement->execute();
}
public function update()
{
$reflection = new ReflectionObject($this);
$map = $this->getTableMap($reflection);
$primaryKey = $this->getPrimaryKeyName($reflection);
$tableName = $this->getTableName($reflection);
if (empty($map[$primaryKey])) {
throw new Error('Cannot update a record without id');
}
$sql = "UPDATE `{$tableName}` SET ";
foreach ($map as $key => $value) {
$sql .= "`{$key}` = :{$key},";
}
$sql = rtrim($sql, ',');
$sql .= " WHERE {$primaryKey} = :id";
$statement = Database::getInstance()->prepare($sql);
$statement->bindValue(':id', $map[$primaryKey]);
foreach ($map as $key => $value) {
$statement->bindValue(":{$key}", $value);
}
$statement->execute();
}
public function insert()
{
$reflection = new ReflectionObject($this);
$map = $this->getTableMap($reflection);
$primaryKey = $this->getPrimaryKeyName($reflection);
$tableName = $this->getTableName($reflection);
$sql = "INSERT INTO `{$tableName}` SET ";
foreach ($map as $key => $value) {
$sql .= "`{$key}` = :{$key},";
}
$sql = rtrim($sql, ',');
$statement = Database::getInstance()->prepare($sql);
foreach ($map as $key => $value) {
$statement->bindValue(":{$key}", $value);
}
$statement->execute();
$insertId = Database::getInstance()->lastInsertId();
if ($insertId) {
$reflection->getProperty($primaryKey)->setValue($this, $insertId);
}
}
public function save($mode = self::SAVE_AUTO)
{
if ($mode == self::SAVE_UPDATE) {
$this->update();
} elseif ($mode == self::SAVE_INSERT) {
$this->insert();
} else {
$reflection = new ReflectionObject($this);
$primaryKeyName = $this->getPrimaryKeyName($reflection);
$primaryKey = $reflection->getProperty($primaryKeyName);
$primaryKey->setAccessible(true);
$identifier = $primaryKey->getValue($this);
if ($identifier) {
$this->update();
} else {
$this->insert();
}
}
}
private function getPrimaryKeyName(ReflectionObject $reflection)
{
$primaryKeyName = 'id';
$reflectionProp = $reflection->getProperties(ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC);
foreach ($reflectionProp as $property) {
if (stripos($property->getDocComment(), '@PrimaryKey')) {
$primaryKeyName = $property->getName();
}
}
return $primaryKeyName;
}
private function getTableName(ReflectionObject $reflection)
{
$docComment = $reflection->getDocComment();
if (!preg_match('/@table ?([A-Za-z\-_0-9]+)/i', $docComment, $matches) || !$matches[1]) {
return strtolower($reflection->getShortName());
} else {
return $matches[1];
}
}
private function getTableMap(ReflectionObject $reflection)
{
$map = array();
$reflectionProp = $reflection->getProperties(ReflectionProperty::IS_PRIVATE | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC);
foreach ($reflectionProp as $property) {
if (strpos($property->getDocComment(), '@ignore')) {
continue;
}
$propertyName = $property->getName();
if (isset($this->_lazyLoad[$propertyName])) {
$map[$propertyName] = $this->_lazyLoad[$propertyName]->getValue();
continue;
}
$property->setAccessible(true);
$propertyValue = $property->getValue($this);
$map[$propertyName] = $propertyValue;
}
return $map;
}
protected function setLazyLoad($propertyName, callable $callback)
{
// First, save the value
$value = $this->$propertyName;
// Create LazyLoadObject
$this->_lazyLoad[$propertyName] = new LazyLoadObject($value, $callback);
// Remove the property to active getter and setter
unset($this->$propertyName);
}
public function __get($propertyName)
{
if (isset($this->_lazyLoad[$propertyName])) {
return ($this->_lazyLoad[$propertyName])();
}
return null;
}
public function __set($propertyName, $value)
{
if (isset($this->_lazyLoad[$propertyName])) {
($this->_lazyLoad[$propertyName])->updateValue($value);
} else {
$this->$propertyName = $value;
}
}
public function __wakeup()
{
$this->lazyLoad();
}
}