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
|
|
|
* |
86
|
|
|
*/ |
87
|
|
|
public function delete() |
88
|
|
|
{ |
89
|
|
|
//TODO: will be implemented after query builder delete finish |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* INSERT AND UPDATE Model |
94
|
|
|
*/ |
95
|
|
|
public function save() |
96
|
|
|
{ |
97
|
|
|
|
98
|
|
|
static::$db->setModel(get_class(new static()), static::$table); |
99
|
|
|
if ($this->isNewRow && static::$db->insert($this->data, true)) { |
100
|
|
|
$this->isNewRow = false; |
101
|
|
|
|
102
|
|
|
if ($lastInsertID = static::$db->lastInsertId()) { |
103
|
|
|
$newData = static::$db->table(static::$table) |
104
|
|
|
->select() |
105
|
|
|
->where($this->primaryKey, $lastInsertID) |
106
|
|
|
->first(); |
107
|
|
|
|
108
|
|
|
$this->data = is_array($newData)?$newData:$this->data; |
109
|
|
|
} |
110
|
|
|
return true; |
111
|
|
|
} else { |
112
|
|
|
return static::$db->update($this->data, $this->primaryKey); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return false; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.