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
|
|
|
* @param $method |
46
|
|
|
* @param $atgs |
47
|
|
|
*/ |
48
|
|
|
public function __call($method, $args) |
49
|
|
|
{ |
50
|
|
|
return call_user_func_array([$this->app->db, $method], $args); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get all the Rows |
55
|
|
|
* |
56
|
|
|
* @param array $order |
57
|
|
|
* @param int $limit |
58
|
|
|
*/ |
59
|
|
|
public function getAll(array $order = ['id', 'DESC'], $limit = null, $table = null) |
60
|
|
|
{ |
61
|
|
|
return $this->orderBy($order[0], $order[1])->limit($limit)->fetchAll($table ? $table : $this->table); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get a Row |
66
|
|
|
* |
67
|
|
|
* @param string $value |
68
|
|
|
* @param string $coulmn |
69
|
|
|
*/ |
70
|
|
|
public function get($value, $coulmn = 'id') |
71
|
|
|
{ |
72
|
|
|
return $this->where($coulmn . ' = ?', $value)->fetch($this->table); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check if row exists |
77
|
|
|
* |
78
|
|
|
* @param string $value |
79
|
|
|
* @param string $key |
80
|
|
|
*/ |
81
|
|
|
public function exists($value, $key = 'id') |
82
|
|
|
{ |
83
|
|
|
return (bool) $this->select($key)->where($key . ' = ? ', $value)->fetch($this->table); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Drop a row |
88
|
|
|
* |
89
|
|
|
* @param string $value |
90
|
|
|
* @param string $key |
91
|
|
|
*/ |
92
|
|
|
public function delete($id) |
93
|
|
|
{ |
94
|
|
|
return $this->where('id = ?', $id)->delete($this->table); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Join |
99
|
|
|
* |
100
|
|
|
* @param string $value |
101
|
|
|
* @param string $key |
102
|
|
|
*/ |
103
|
|
|
public function joinGetAll($select, $joins, $table = null) |
104
|
|
|
{ |
105
|
|
|
return $this->db->select($select)->from($table ? $table : $this->table)->join($joins); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get a row after Joining |
110
|
|
|
* |
111
|
|
|
* @param string $select |
112
|
|
|
* @param string $table |
113
|
|
|
* @param string $joins |
114
|
|
|
* @param string $table |
115
|
|
|
*/ |
116
|
|
|
public function joinGetRow($select, $joins, $where, $table = null) |
117
|
|
|
{ |
118
|
|
|
return $this->db->select($select)->from($table ? $table : $this->table)->join($joins)->where($table ? $table : $this->tablee . '.id = ?', $where); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|