1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the O2System PHP 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\Reactor\Models\Sql\Traits; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Database\DataObjects\Result; |
19
|
|
|
use O2System\Reactor\Models\Sql\DataObjects; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class FinderTrait |
23
|
|
|
* |
24
|
|
|
* @package O2System\Reactor\Models\Sql\Traits |
25
|
|
|
*/ |
26
|
|
|
trait FinderTrait |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* FinderTrait::all |
30
|
|
|
* |
31
|
|
|
* @param null $fields |
|
|
|
|
32
|
|
|
* @param null $limit |
|
|
|
|
33
|
|
|
* |
34
|
|
|
* @return bool|DataObjects\Result |
35
|
|
|
*/ |
36
|
|
|
public function all($fields = null, $limit = null) |
37
|
|
|
{ |
38
|
|
|
if (isset($fields)) { |
39
|
|
|
$this->qb->select($fields); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (isset($limit)) { |
43
|
|
|
$this->qb->limit($limit); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$result = $this->qb->from($this->table)->get(); |
47
|
|
|
|
48
|
|
|
if ($result instanceof Result) { |
49
|
|
|
if ($result->count() > 0) { |
50
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
51
|
|
|
$this->result->setInfo($result->getInfo()); |
52
|
|
|
|
53
|
|
|
return $this->result; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
// ------------------------------------------------------------------------ |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* FinderTrait::page |
63
|
|
|
* |
64
|
|
|
* Find record by page. |
65
|
|
|
* |
66
|
|
|
* @param int $page |
67
|
|
|
*/ |
68
|
|
|
public function page($fields = null, $page = 1, $entries = 5) |
69
|
|
|
{ |
70
|
|
|
$page = empty($page) ? 1 : $page; |
71
|
|
|
$entries = empty($entries) ? 5 : $entries; |
72
|
|
|
|
73
|
|
|
if (isset($fields)) { |
74
|
|
|
if (is_numeric($fields)) { |
75
|
|
|
$page = $fields; |
76
|
|
|
} else { |
77
|
|
|
$this->qb->select($fields); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->qb->page($page, $entries); |
82
|
|
|
|
83
|
|
|
$result = $this->qb->from($this->table)->get(); |
84
|
|
|
|
85
|
|
|
if ($result->count() > 0) { |
86
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
87
|
|
|
$this->result->setInfo($result->getInfo()); |
88
|
|
|
|
89
|
|
|
return $this->result; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return false; |
93
|
|
|
} |
94
|
|
|
// ------------------------------------------------------------------------ |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Find |
98
|
|
|
* |
99
|
|
|
* Find single or many record base on criteria by specific field |
100
|
|
|
* |
101
|
|
|
* @param string $criteria Criteria value |
102
|
|
|
* @param string|null $field Table column field name | set to primary key by default |
103
|
|
|
* |
104
|
|
|
* @return DataObjects\Result|DataObjects\Result\Row|bool Returns FALSE if failed. |
105
|
|
|
*/ |
106
|
|
|
public function find($criteria, $field = null, $limit = null) |
107
|
|
|
{ |
108
|
|
|
if (is_array($criteria)) { |
|
|
|
|
109
|
|
|
return $this->findIn($criteria, $field); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
113
|
|
|
|
114
|
|
|
$result = $this->qb |
115
|
|
|
->from($this->table) |
116
|
|
|
->getWhere([$field => $criteria], $limit); |
117
|
|
|
|
118
|
|
|
if ($result instanceof Result) { |
119
|
|
|
if ($result->count() > 0) { |
120
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
121
|
|
|
$this->result->setInfo($result->getInfo()); |
122
|
|
|
|
123
|
|
|
if ($this->result->count() == 1) { |
124
|
|
|
return $this->result->first(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $this->result; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return false; |
132
|
|
|
} |
133
|
|
|
// ------------------------------------------------------------------------ |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Find In |
137
|
|
|
* |
138
|
|
|
* Find many records within criteria on specific field |
139
|
|
|
* |
140
|
|
|
* @param array $inCriteria List of criteria |
141
|
|
|
* @param string $field Table column field name | set to primary key by default |
142
|
|
|
* |
143
|
|
|
* @return DataObjects\Result|bool Returns FALSE if failed. |
144
|
|
|
*/ |
145
|
|
|
public function findIn(array $inCriteria, $field = null) |
146
|
|
|
{ |
147
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
148
|
|
|
|
149
|
|
|
$result = $this->qb |
150
|
|
|
->from($this->table) |
151
|
|
|
->whereIn($field, $inCriteria) |
152
|
|
|
->get(); |
153
|
|
|
|
154
|
|
|
if ($result->count() > 0) { |
155
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
156
|
|
|
$this->result->setInfo($result->getInfo()); |
157
|
|
|
|
158
|
|
|
return $this->result; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return false; |
162
|
|
|
} |
163
|
|
|
// ------------------------------------------------------------------------ |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Find By |
167
|
|
|
* |
168
|
|
|
* Find single record based on certain conditions |
169
|
|
|
* |
170
|
|
|
* @param array $conditions List of conditions with criteria |
171
|
|
|
* |
172
|
|
|
* @access protected |
173
|
|
|
* @return DataObjects\Result|bool Returns FALSE if failed. |
174
|
|
|
*/ |
175
|
|
|
public function findWhere(array $conditions, $limit = null) |
176
|
|
|
{ |
177
|
|
|
$result = $this->qb |
178
|
|
|
->from($this->table) |
179
|
|
|
->getWhere($conditions, $limit); |
180
|
|
|
|
181
|
|
|
if ($result->count() > 0) { |
182
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
183
|
|
|
$this->result->setInfo($result->getInfo()); |
184
|
|
|
|
185
|
|
|
if ($limit == 1) { |
186
|
|
|
return $this->result->first(); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
return $this->result; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return false; |
193
|
|
|
} |
194
|
|
|
// ------------------------------------------------------------------------ |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Find In |
198
|
|
|
* |
199
|
|
|
* Find many records not within criteria on specific field |
200
|
|
|
* |
201
|
|
|
* @param array $notInCriteria List of criteria |
202
|
|
|
* @param string $field Table column field name | set to primary key by default |
203
|
|
|
* |
204
|
|
|
* @return DataObjects\Result|bool Returns FALSE if failed. |
205
|
|
|
*/ |
206
|
|
|
public function findNotIn(array $notInCriteria, $field = null) |
207
|
|
|
{ |
208
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
209
|
|
|
|
210
|
|
|
$result = $this->qb |
211
|
|
|
->from($this->table) |
212
|
|
|
->whereNotIn($field, $notInCriteria) |
213
|
|
|
->get(); |
214
|
|
|
|
215
|
|
|
if ($result->count() > 0) { |
216
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
217
|
|
|
$this->result->setInfo($result->getInfo()); |
218
|
|
|
|
219
|
|
|
return $this->result; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return false; |
223
|
|
|
} |
224
|
|
|
} |