|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Nette\Database\Context; |
|
6
|
|
|
use Nette\Object; |
|
7
|
|
|
use Nette\Caching\Cache; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* BaseModel |
|
11
|
|
|
* |
|
12
|
|
|
* Base model is a base class for all components. |
|
13
|
|
|
* |
|
14
|
|
|
* @created 2012-12-16 |
|
15
|
|
|
* @author Tomas Litera <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
1 |
|
abstract class BaseModel extends Object |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var string */ |
|
20
|
|
|
protected $table = null; |
|
21
|
|
|
|
|
22
|
|
|
/** @var array */ |
|
23
|
|
|
protected $columns = []; |
|
24
|
|
|
|
|
25
|
|
|
/** @var Context */ |
|
26
|
|
|
protected $database; |
|
27
|
|
|
|
|
28
|
|
|
/** @var integer */ |
|
29
|
|
|
protected $meetingId; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Cache |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $cache; |
|
35
|
|
|
|
|
36
|
|
|
/** Constructor */ |
|
37
|
|
|
public function __construct($table = null, $database = null) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->setTable($table); |
|
40
|
|
|
$this->setDatabase($database); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Create new or return existing instance of class |
|
45
|
|
|
* |
|
46
|
|
|
* @return mixed instance of class |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function getInstance() |
|
49
|
|
|
{ |
|
50
|
|
|
if(self::$instance === false) { |
|
51
|
|
|
self::$instance = new self(); |
|
52
|
|
|
} |
|
53
|
|
|
return self::$instance; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @return Nette\Database\Table\ActiveRow |
|
58
|
|
|
*/ |
|
59
|
|
|
public function all() |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->getDatabase() |
|
62
|
|
|
->table($this->getTable()) |
|
63
|
|
|
->where('deleted', '0') |
|
64
|
|
|
->fetchAll(); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param integer $id |
|
69
|
|
|
* @return Nette\Database\Table\ActiveRow |
|
70
|
|
|
*/ |
|
71
|
|
|
public function find($id) |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->getDatabase() |
|
74
|
|
|
->table($this->getTable()) |
|
75
|
|
|
->where('id ? AND deleted ?', $id, '0') |
|
76
|
|
|
->limit(1) |
|
77
|
|
|
->fetch(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $column |
|
82
|
|
|
* @param mixed $value |
|
83
|
|
|
* @return ActiveRow |
|
84
|
|
|
*/ |
|
85
|
|
|
public function findBy($column, $value) |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->getDatabase() |
|
88
|
|
|
->table($this->getTable()) |
|
89
|
|
|
->where($column . ' ? AND deleted ?', $value, '0') |
|
90
|
|
|
->limit(1) |
|
91
|
|
|
->fetch(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Create a new record |
|
96
|
|
|
* |
|
97
|
|
|
* @param mixed array of data |
|
98
|
|
|
* @return boolean |
|
99
|
|
|
*/ |
|
100
|
|
|
public function create(array $data) |
|
101
|
|
|
{ |
|
102
|
|
|
$data['guid'] = $this->generateGuid(); |
|
103
|
|
|
$result = $this->getDatabase()->table($this->getTable())->insert($data); |
|
104
|
|
|
|
|
105
|
|
|
return $result; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param int $id |
|
110
|
|
|
* @param array $data |
|
111
|
|
|
* @return bool |
|
112
|
|
|
*/ |
|
113
|
|
|
public function update($id, array $data) |
|
114
|
|
|
{ |
|
115
|
|
|
$result = $this->getDatabase() |
|
116
|
|
|
->table($this->getTable()) |
|
117
|
|
|
->where('id', $id) |
|
118
|
|
|
->update($data); |
|
119
|
|
|
|
|
120
|
|
|
return $result; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param string $column |
|
125
|
|
|
* @param string|integer $value |
|
126
|
|
|
* @param array $data |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
|
|
public function updateBy($column, $value, array $data) |
|
130
|
|
|
{ |
|
131
|
|
|
$result = $this->getDatabase() |
|
132
|
|
|
->table($this->getTable()) |
|
133
|
|
|
->where($column, $value) |
|
134
|
|
|
->update($data); |
|
135
|
|
|
|
|
136
|
|
|
return $result; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param int ID/s of record |
|
141
|
|
|
* @return boolean |
|
142
|
|
|
*/ |
|
143
|
|
View Code Duplication |
public function delete($ids) |
|
|
|
|
|
|
144
|
|
|
{ |
|
145
|
|
|
$data = [ |
|
146
|
|
|
'deleted' => '1', |
|
147
|
|
|
]; |
|
148
|
|
|
|
|
149
|
|
|
$result = $this->getDatabase() |
|
150
|
|
|
->table($this->getTable()) |
|
151
|
|
|
->where('id', $ids) |
|
152
|
|
|
->update($data); |
|
153
|
|
|
|
|
154
|
|
|
return $result; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param integer $meetingId |
|
159
|
|
|
* @return $this |
|
160
|
|
|
*/ |
|
161
|
|
|
public function setMeetingId($meetingId) |
|
162
|
|
|
{ |
|
163
|
1 |
|
$this->meetingId = $meetingId; |
|
164
|
|
|
|
|
165
|
1 |
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @return integer |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function getMeetingId() |
|
172
|
|
|
{ |
|
173
|
1 |
|
return $this->meetingId; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @return Nette\Database\Context |
|
178
|
|
|
*/ |
|
179
|
|
|
protected function getDatabase() |
|
180
|
|
|
{ |
|
181
|
1 |
|
return $this->database; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param Nette\Database\Context $database |
|
186
|
|
|
* @return $this |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function setDatabase(Context $database) |
|
189
|
|
|
{ |
|
190
|
1 |
|
$this->database = $database; |
|
191
|
1 |
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* @return string |
|
196
|
|
|
*/ |
|
197
|
|
|
protected function getTable() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->table; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @param string $table |
|
204
|
|
|
* @return $this |
|
205
|
|
|
*/ |
|
206
|
|
|
protected function setTable($table) |
|
207
|
|
|
{ |
|
208
|
|
|
$this->table = $table; |
|
209
|
|
|
|
|
210
|
|
|
return $this; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @return string |
|
215
|
|
|
*/ |
|
216
|
|
|
protected function generateGuid() |
|
217
|
|
|
{ |
|
218
|
|
|
return md5(uniqid()); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* @return Cache |
|
223
|
|
|
*/ |
|
224
|
|
|
protected function getCache() |
|
225
|
|
|
{ |
|
226
|
|
|
return $this->cache; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param Cache $cache |
|
231
|
|
|
* @return $this |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function setCache(Cache $cache) |
|
234
|
|
|
{ |
|
235
|
1 |
|
$this->cache = $cache; |
|
236
|
|
|
|
|
237
|
1 |
|
return $this; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
} |
|
241
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.