|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Credo; |
|
4
|
|
|
|
|
5
|
|
|
use Rougin\Credo\Helpers\InstanceHelper; |
|
6
|
|
|
use Rougin\Credo\Helpers\MethodHelper; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Model |
|
10
|
|
|
* |
|
11
|
|
|
* @package Credo |
|
12
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
13
|
|
|
* |
|
14
|
|
|
* @property \CI_DB_query_builder $db |
|
15
|
|
|
*/ |
|
16
|
|
|
class Model extends \CI_Model |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var array |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $criteria = array(); |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Initializes the model instance. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
|
|
InstanceHelper::create($this->db); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Returns all of the models from the database. |
|
33
|
|
|
* |
|
34
|
|
|
* @return array |
|
35
|
|
|
*/ |
|
36
|
|
|
public function all() |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->findBy(array()); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Returns a total rows from the specified table. |
|
43
|
|
|
* |
|
44
|
|
|
* @return integer |
|
45
|
|
|
*/ |
|
46
|
|
|
public function countAll() |
|
47
|
|
|
{ |
|
48
|
|
|
$primary = (string) $this->primary(); |
|
49
|
|
|
|
|
50
|
|
|
$table = $this->table(); |
|
51
|
|
|
|
|
52
|
|
|
$builder = InstanceHelper::get()->createQueryBuilder(); |
|
53
|
|
|
|
|
54
|
|
|
$builder->select($builder->expr()->count($table . '.' . $primary)); |
|
55
|
|
|
|
|
56
|
|
|
$builder->from((string) get_class($this), $table); |
|
57
|
|
|
|
|
58
|
|
|
return $builder->getQuery()->getSingleScalarResult(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Deletes the specified ID of the model from the database. |
|
63
|
|
|
* |
|
64
|
|
|
* @param integer $id |
|
65
|
|
|
* @return void |
|
66
|
|
|
*/ |
|
67
|
|
|
public function delete($id) |
|
68
|
|
|
{ |
|
69
|
|
|
$item = $this->find((integer) $id); |
|
70
|
|
|
|
|
71
|
|
|
InstanceHelper::get()->remove($item); |
|
72
|
|
|
|
|
73
|
|
|
InstanceHelper::get()->flush(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Finds an entity by its primary key / identifier. |
|
78
|
|
|
* |
|
79
|
|
|
* @param mixed $id |
|
80
|
|
|
* @param integer|null $mode |
|
81
|
|
|
* @param integer|null $version |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
*/ |
|
84
|
|
|
public function find($id, $mode = null, $version = null) |
|
85
|
|
|
{ |
|
86
|
|
|
$repository = InstanceHelper::get()->getRepository(get_class($this)); |
|
87
|
|
|
|
|
88
|
|
|
return $repository->find($id, $mode, $version); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Finds models by a set of criteria. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $criteria |
|
95
|
|
|
* @param array|null $order |
|
96
|
|
|
* @param integer|null $limit |
|
97
|
|
|
* @param integer|null $offset |
|
98
|
|
|
* @return array |
|
99
|
|
|
*/ |
|
100
|
|
|
public function findBy(array $criteria, array $order = null, $limit = null, $offset = null) |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->where($criteria)->get($limit, $offset, $order); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns an array of rows from a specified entity. |
|
107
|
|
|
* |
|
108
|
|
|
* @param string $entity |
|
|
|
|
|
|
109
|
|
|
* @param integer|null $limit |
|
110
|
|
|
* @param integer|null $page |
|
111
|
|
|
* @param array|null $order |
|
112
|
|
|
* @return mixed |
|
113
|
|
|
*/ |
|
114
|
|
|
public function get($limit = null, $page = null, array $order = null) |
|
115
|
|
|
{ |
|
116
|
|
|
$repository = InstanceHelper::get()->getRepository(get_class($this)); |
|
117
|
|
|
|
|
118
|
|
|
($criteria = $this->criteria) && $this->criteria = array(); |
|
119
|
|
|
|
|
120
|
|
|
return $repository->findBy($criteria, $order, $limit, $page); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Sets the "WHERE" criteria. |
|
125
|
|
|
* |
|
126
|
|
|
* @param array|string $key |
|
127
|
|
|
* @param mixed|null $value |
|
128
|
|
|
* @return self |
|
129
|
|
|
*/ |
|
130
|
|
|
public function where($key, $value = null) |
|
131
|
|
|
{ |
|
132
|
|
|
if (is_array($key) === true) { |
|
133
|
|
|
$this->criteria = (array) $key; |
|
134
|
|
|
} else { |
|
135
|
|
|
$this->criteria[$key] = $value; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Inserts a new row into the table. |
|
143
|
|
|
* |
|
144
|
|
|
* @param array $data |
|
145
|
|
|
* @return integer |
|
146
|
|
|
*/ |
|
147
|
|
|
public function insert(array $data) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->db->insert($this->table(), $data); |
|
150
|
|
|
|
|
151
|
|
|
$lastId = $this->db->insert_id(); |
|
|
|
|
|
|
152
|
|
|
|
|
153
|
|
|
InstanceHelper::get()->refresh($this->find($lastId)); |
|
154
|
|
|
|
|
155
|
|
|
return $lastId; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Updates the selected row from the table. |
|
160
|
|
|
* |
|
161
|
|
|
* @param integer $id |
|
162
|
|
|
* @param array $data |
|
163
|
|
|
* @return boolean |
|
164
|
|
|
*/ |
|
165
|
|
|
public function update($id, array $data) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->db->where($this->primary(), $id); |
|
168
|
|
|
|
|
169
|
|
|
$this->db->set((array) $data); |
|
170
|
|
|
|
|
171
|
|
|
$result = $this->db->update($this->table()); |
|
172
|
|
|
|
|
173
|
|
|
$item = $this->find((integer) $id); |
|
174
|
|
|
|
|
175
|
|
|
InstanceHelper::get()->refresh($item); |
|
176
|
|
|
|
|
177
|
|
|
return $result; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Returns the primary key. |
|
182
|
|
|
* |
|
183
|
|
|
* @return string |
|
184
|
|
|
*/ |
|
185
|
|
|
public function primary() |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->metadata()->getSingleIdentifierColumnName(); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Returns the name of the table. |
|
192
|
|
|
* |
|
193
|
|
|
* @return string |
|
194
|
|
|
*/ |
|
195
|
|
|
public function table() |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->metadata()->getTableName(); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Returns the metadata of an entity. |
|
202
|
|
|
* |
|
203
|
|
|
* @return \Doctrine\Common\Persistence\Mapping\ClassMetadata |
|
204
|
|
|
*/ |
|
205
|
|
|
protected function metadata() |
|
206
|
|
|
{ |
|
207
|
|
|
$class = (string) get_class($this); |
|
208
|
|
|
|
|
209
|
|
|
$manager = InstanceHelper::get(); |
|
210
|
|
|
|
|
211
|
|
|
$factory = $manager->getMetadataFactory(); |
|
212
|
|
|
|
|
213
|
|
|
return $factory->getMetadataFor($class); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Calls methods in underscore case. |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $method |
|
220
|
|
|
* @param mixed $parameters |
|
221
|
|
|
* @return mixed |
|
222
|
|
|
*/ |
|
223
|
|
|
public function __call($method, $parameters) |
|
224
|
|
|
{ |
|
225
|
|
|
return MethodHelper::call($this, $method, $parameters); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.