|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the O2System Framework package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Steeve Andrian Salim |
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------ |
|
13
|
|
|
|
|
14
|
|
|
namespace O2System\Framework\Models\Sql\Traits; |
|
15
|
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
|
17
|
|
|
|
|
18
|
|
|
use O2System\Framework\Models\Sql\DataObjects; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class FinderTrait |
|
22
|
|
|
* |
|
23
|
|
|
* @package O2System\Framework\Models\Sql\Traits |
|
24
|
|
|
*/ |
|
25
|
|
|
trait FinderTrait |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* FinderTrait::all |
|
29
|
|
|
* |
|
30
|
|
|
* @param array|string|null $fields |
|
31
|
|
|
* @param int|null $limit |
|
32
|
|
|
* |
|
33
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result |
|
34
|
|
|
*/ |
|
35
|
|
|
public function all($fields = null, $limit = null) |
|
36
|
|
|
{ |
|
37
|
|
|
if (isset($fields)) { |
|
38
|
|
|
$this->qb->select($fields); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
if (isset($limit)) { |
|
42
|
|
|
$this->qb->limit($limit); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (property_exists($this, 'hierarchical')) { |
|
46
|
|
|
$this->qb->orderBy($this->table . '.record_ordering', 'ASC'); |
|
47
|
|
|
$this->qb->orderBy($this->table . '.record_left', 'ASC'); |
|
48
|
|
|
} elseif (property_exists($this, 'adjacency')) { |
|
49
|
|
|
$this->qb->orderBy($this->table . '.record_ordering', 'ASC'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if ($result = $this->qb->from($this->table)->get()) { |
|
53
|
|
|
if ($result->count() > 0) { |
|
54
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
return $this->result; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// ------------------------------------------------------------------------ |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* FinderTrait::allWithPaging |
|
67
|
|
|
* |
|
68
|
|
|
* @param array|string|null $fields |
|
69
|
|
|
* @param int|null $numPerPage |
|
70
|
|
|
* |
|
71
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result |
|
72
|
|
|
*/ |
|
73
|
|
|
public function allWithPaging($fields = null, $numPerPage = null) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->withPaging(null, $numPerPage)->all($fields, $numPerPage); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// ------------------------------------------------------------------------ |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* FinderTrait::withPaging |
|
82
|
|
|
* |
|
83
|
|
|
* @param int|null $page |
|
84
|
|
|
* @param int|null $numPerPage |
|
85
|
|
|
* |
|
86
|
|
|
* @return bool|\O2System\Framework\Models\Sql\Model |
|
87
|
|
|
*/ |
|
88
|
|
|
public function withPaging($page = null, $numPerPage = null) |
|
89
|
|
|
{ |
|
90
|
|
|
$getPage = $this->input->get('page'); |
|
91
|
|
|
$getLimit = $this->input->get('limit'); |
|
92
|
|
|
|
|
93
|
|
|
$page = empty($page) ? (empty($getPage) ? 1 : $getPage) : $page; |
|
94
|
|
|
$numPerPage = empty($numPerPage) ? (empty($getLimit) ? 10 : $getLimit) : $numPerPage; |
|
95
|
|
|
|
|
96
|
|
|
$this->qb->page($page, $numPerPage); |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
// ------------------------------------------------------------------------ |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* FinderTrait::paginate |
|
105
|
|
|
* |
|
106
|
|
|
* @param int $numPerPage |
|
107
|
|
|
* |
|
108
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result |
|
109
|
|
|
*/ |
|
110
|
|
|
public function paginate($numPerPage) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->withPaging(null, $numPerPage)->all($numPerPage); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// ------------------------------------------------------------------------ |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* FinderTrait::find |
|
119
|
|
|
* |
|
120
|
|
|
* @param mixed $criteria |
|
121
|
|
|
* @param string|null $field |
|
122
|
|
|
* @param int|null $limit |
|
123
|
|
|
* |
|
124
|
|
|
* @return bool|mixed|\O2System\Framework\Models\Sql\DataObjects\Result |
|
125
|
|
|
*/ |
|
126
|
|
|
public function find($criteria, $field = null, $limit = null) |
|
127
|
|
|
{ |
|
128
|
|
|
if (is_array($criteria)) { |
|
129
|
|
|
return $this->findIn($criteria, $field); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
|
133
|
|
|
if (strpos($field, '.') === false) { |
|
134
|
|
|
$field = $this->table . '.' . $field; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if ($result = $this->qb |
|
138
|
|
|
->from($this->table) |
|
139
|
|
|
->where($field, $criteria) |
|
140
|
|
|
->get($limit)) { |
|
141
|
|
|
if ($result->count() > 0) { |
|
142
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
if ($this->result->count() == 1) { |
|
145
|
|
|
return $this->result->first(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $this->result; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return false; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// ------------------------------------------------------------------------ |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* FinderTrait::findWhere |
|
159
|
|
|
* |
|
160
|
|
|
* @param array $conditions |
|
161
|
|
|
* @param int|null $limit |
|
162
|
|
|
* |
|
163
|
|
|
* @return bool|mixed|\O2System\Framework\Models\Sql\DataObjects\Result |
|
164
|
|
|
*/ |
|
165
|
|
|
public function findWhere(array $conditions, $limit = null) |
|
166
|
|
|
{ |
|
167
|
|
|
foreach ($conditions as $field => $criteria) { |
|
168
|
|
|
if (strpos($field, '.') === false) { |
|
169
|
|
|
$field = $this->table . '.' . $field; |
|
170
|
|
|
} |
|
171
|
|
|
$this->qb->where($field, $criteria); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
if ($result = $this->qb |
|
175
|
|
|
->from($this->table) |
|
176
|
|
|
->get($limit)) { |
|
177
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
if ($limit == 1) { |
|
180
|
|
|
return $this->result->first(); |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $this->result; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
return false; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
// ------------------------------------------------------------------------ |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* FinderTrait::findIn |
|
193
|
|
|
* |
|
194
|
|
|
* @param array $inCriteria |
|
195
|
|
|
* @param string|null $field |
|
196
|
|
|
* |
|
197
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result |
|
198
|
|
|
*/ |
|
199
|
|
|
public function findIn(array $inCriteria, $field = null) |
|
200
|
|
|
{ |
|
201
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
|
202
|
|
|
$field = $this->table . '.' . $field; |
|
203
|
|
|
|
|
204
|
|
|
if ($result = $this->qb |
|
205
|
|
|
->from($this->table) |
|
206
|
|
|
->whereIn($field, $inCriteria) |
|
207
|
|
|
->get()) { |
|
208
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
return $this->result; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
return false; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
// ------------------------------------------------------------------------ |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* FinderTrait::findNotIn |
|
220
|
|
|
* |
|
221
|
|
|
* @param array $notInCriteria |
|
222
|
|
|
* @param string|null $field |
|
223
|
|
|
* |
|
224
|
|
|
* @return bool|\O2System\Framework\Models\Sql\DataObjects\Result |
|
225
|
|
|
*/ |
|
226
|
|
|
public function findNotIn(array $notInCriteria, $field = null) |
|
227
|
|
|
{ |
|
228
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
|
229
|
|
|
if (strpos($field, '.') === false) { |
|
230
|
|
|
$field = $this->table . '.' . $field; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
if ($result = $this->qb |
|
234
|
|
|
->from($this->table) |
|
235
|
|
|
->whereNotIn($field, $notInCriteria) |
|
236
|
|
|
->get()) { |
|
237
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
|
|
238
|
|
|
|
|
239
|
|
|
return $this->result; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
return false; |
|
243
|
|
|
} |
|
244
|
|
|
} |