1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\Database\Model; |
3
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Database\Database; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Model |
8
|
|
|
* @package JayaCode\Framework\Core\Database\Model |
9
|
|
|
*/ |
10
|
|
|
abstract class Model |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var Database |
14
|
|
|
*/ |
15
|
|
|
public static $db; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $primaryKey = "id"; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var null |
24
|
|
|
*/ |
25
|
|
|
protected static $table = null; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $data = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $isNewRow = true; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $data |
39
|
|
|
* @param bool $isNewRow |
40
|
|
|
*/ |
41
|
|
|
public function __construct($data = array(), $isNewRow = true) |
42
|
|
|
{ |
43
|
|
|
$this->data = $data; |
44
|
|
|
$this->isNewRow = $isNewRow; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $data |
49
|
|
|
* @return static |
50
|
|
|
*/ |
51
|
|
|
public static function create($data = array()) |
52
|
|
|
{ |
53
|
|
|
return new static($data); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param null $columns |
58
|
|
|
* @return Database |
59
|
|
|
*/ |
60
|
|
|
public static function select($columns = null) |
61
|
|
|
{ |
62
|
|
|
static::$db->setModel(get_class(new static()), static::$table); |
63
|
|
|
return static::$db->select($columns); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $name |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function __get($name) |
71
|
|
|
{ |
72
|
|
|
return arr_get($this->data, $name); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $name |
77
|
|
|
* @param $value |
78
|
|
|
*/ |
79
|
|
|
public function __set($name, $value) |
80
|
|
|
{ |
81
|
|
|
$this->data[$name] = $value; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return bool |
86
|
|
|
* @throws \Exception |
87
|
|
|
*/ |
88
|
|
|
public function delete() |
89
|
|
|
{ |
90
|
|
|
static::$db->setModel(get_class(new static()), static::$table); |
91
|
|
|
if (static::$db->delete($this->primaryKey, $this->data[$this->primaryKey])) { |
92
|
|
|
$this->isNewRow = true; |
93
|
|
|
return true; |
94
|
|
|
} |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return bool|mixed |
100
|
|
|
* @throws \Exception |
101
|
|
|
*/ |
102
|
|
|
public function save() |
103
|
|
|
{ |
104
|
|
|
|
105
|
|
|
static::$db->setModel(get_class(new static()), static::$table); |
106
|
|
|
if ($this->isNewRow) { |
107
|
|
|
if (static::$db->insert($this->data, true)) { |
108
|
|
|
$this->isNewRow = false; |
109
|
|
|
|
110
|
|
|
if ($lastInsertID = static::$db->lastInsertId()) { |
111
|
|
|
$newData = static::$db->table(static::$table) |
112
|
|
|
->select() |
113
|
|
|
->where($this->primaryKey, $lastInsertID) |
114
|
|
|
->first(); |
115
|
|
|
|
116
|
|
|
$this->data = is_array($newData)?$newData:$this->data; |
117
|
|
|
} |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
return false; |
121
|
|
|
} else { |
122
|
|
|
return static::$db->update($this->data, $this->primaryKey); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|