1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System; |
4
|
|
|
|
5
|
|
|
abstract class Model |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Application Object |
9
|
|
|
* |
10
|
|
|
* @var \System\Application |
11
|
|
|
*/ |
12
|
|
|
private $app; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Table of a model |
16
|
|
|
* |
17
|
|
|
* @var $table |
18
|
|
|
*/ |
19
|
|
|
protected $table; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Constructor |
23
|
|
|
* |
24
|
|
|
* @param \System\Application $app |
25
|
|
|
*/ |
26
|
|
|
public function __construct(Application $app) |
27
|
|
|
{ |
28
|
|
|
$this->app = $app; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Call shared Application Objects dynamically |
33
|
|
|
* |
34
|
|
|
* @param string $key |
35
|
|
|
* @return mixed |
36
|
|
|
*/ |
37
|
|
|
public function __get($key) |
38
|
|
|
{ |
39
|
|
|
return $this->app->get($key); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Call the methods from Database Object |
44
|
|
|
* |
45
|
|
|
* @property object $db |
46
|
|
|
* @param method $method |
47
|
|
|
* @param array $args |
48
|
|
|
*/ |
49
|
|
|
public function __call($method, $args) |
50
|
|
|
{ |
51
|
|
|
return call_user_func_array([$this->app->db, $method], $args); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get all the Rows |
56
|
|
|
* |
57
|
|
|
* @method orderBy |
58
|
|
|
* @method limit |
59
|
|
|
* @method fetchAll |
60
|
|
|
* @param array $order |
61
|
|
|
* @param int $limit |
62
|
|
|
* @param string $table |
63
|
|
|
*/ |
64
|
|
|
public function getAll(array $order = ['id', 'DESC'], int $limit = null, string $table = null) |
65
|
|
|
{ |
66
|
|
|
return $this->orderBy($order[0], $order[1])->limit($limit)->fetchAll($table ? $table : $this->table); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get a Row |
71
|
|
|
* |
72
|
|
|
* @method where |
73
|
|
|
* @method fetch |
74
|
|
|
* @param string $value |
75
|
|
|
* @param string $coulmn |
76
|
|
|
*/ |
77
|
|
|
public function get(string $value, string $coulmn = 'id') |
78
|
|
|
{ |
79
|
|
|
return $this->where($coulmn . ' = ?', $value)->fetch($this->table); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Check if row exists |
84
|
|
|
* |
85
|
|
|
* @method select |
86
|
|
|
* @method where |
87
|
|
|
* @method fetch |
88
|
|
|
* @param string $value |
89
|
|
|
* @param string $key |
90
|
|
|
*/ |
91
|
|
|
public function exists(string $value, string $key = 'id') |
92
|
|
|
{ |
93
|
|
|
return (bool) $this->select($key)->where($key . ' = ? ', $value)->fetch($this->table); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Drop a row |
98
|
|
|
* |
99
|
|
|
* @method where |
100
|
|
|
* @method delete |
101
|
|
|
* @param string $id |
102
|
|
|
*/ |
103
|
|
|
public function delete(string $id) |
104
|
|
|
{ |
105
|
|
|
return $this->where('id = ?', $id)->delete($this->table); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Join |
110
|
|
|
* |
111
|
|
|
* @method select |
112
|
|
|
* @method from |
113
|
|
|
* @method join |
114
|
|
|
* @param string $select |
115
|
|
|
* @param string $joins |
116
|
|
|
* @param string $table |
117
|
|
|
*/ |
118
|
|
|
public function joinGetAll(string $select, string $joins, string $table = null) |
119
|
|
|
{ |
120
|
|
|
return $this->db->select($select)->from($table ? $table : $this->table)->join($joins); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get a row after Joining |
125
|
|
|
* |
126
|
|
|
* @method select |
127
|
|
|
* @method from |
128
|
|
|
* @method join |
129
|
|
|
* @method where |
130
|
|
|
* @param string $select |
131
|
|
|
* @param string $table |
132
|
|
|
* @param string $joins |
133
|
|
|
* @param string $table |
134
|
|
|
*/ |
135
|
|
|
public function joinGetRow(string $select, string $joins, string $where, string $table = null) |
136
|
|
|
{ |
137
|
|
|
$table = $table ? $table : $this->table; |
138
|
|
|
return $this->db->select($select)->from($table)->join($joins)->where($table . '.id = ?', $where); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.