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\NoSql\Traits; |
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------ |
17
|
|
|
|
18
|
|
|
use O2System\Database\DataObjects; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class FinderTrait |
22
|
|
|
* |
23
|
|
|
* @package O2System\Reactor\Models\NoSql\Traits |
24
|
|
|
*/ |
25
|
|
|
trait FinderTrait |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Find |
29
|
|
|
* |
30
|
|
|
* Find single or many record base on criteria by specific field |
31
|
|
|
* |
32
|
|
|
* @param string $criteria Criteria value |
33
|
|
|
* @param string|null $field Table column field name | set to primary key by default |
34
|
|
|
* |
35
|
|
|
* @access protected |
36
|
|
|
* @return DataObjects\Result|bool Returns FALSE if failed. |
37
|
|
|
*/ |
38
|
|
|
public function all($fields = null) |
39
|
|
|
{ |
40
|
|
|
if (isset($fields)) { |
41
|
|
|
$this->db->select($fields); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$result = $this->db->from($this->collection)->get(); |
45
|
|
|
|
46
|
|
|
if ($result->count() > 0) { |
47
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
48
|
|
|
|
49
|
|
|
return $this->result; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
// ------------------------------------------------------------------------ |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Find |
58
|
|
|
* |
59
|
|
|
* Find single or many record base on criteria by specific field |
60
|
|
|
* |
61
|
|
|
* @param string $criteria Criteria value |
62
|
|
|
* @param string|null $field Table column field name | set to primary key by default |
63
|
|
|
* |
64
|
|
|
* @return DataObjects\Result|bool Returns FALSE if failed. |
65
|
|
|
*/ |
66
|
|
|
public function find($criteria, $field = null, $limit = null) |
67
|
|
|
{ |
68
|
|
|
if (is_array($criteria)) { |
|
|
|
|
69
|
|
|
return $this->findIn($criteria, $field); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
73
|
|
|
|
74
|
|
|
$result = $this->db |
75
|
|
|
->from($this->collection) |
76
|
|
|
->getWhere([$field => $criteria], $limit); |
77
|
|
|
|
78
|
|
|
if ($result->count() > 0) { |
79
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
80
|
|
|
|
81
|
|
|
if ($result->count() == 1) { |
82
|
|
|
return $this->result->first(); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $this->result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
// ------------------------------------------------------------------------ |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Find In |
94
|
|
|
* |
95
|
|
|
* Find many records within criteria on specific field |
96
|
|
|
* |
97
|
|
|
* @param array $inCriteria List of criteria |
98
|
|
|
* @param string $field Table column field name | set to primary key by default |
99
|
|
|
* |
100
|
|
|
* @return DataObjects\Result|bool Returns FALSE if failed. |
101
|
|
|
*/ |
102
|
|
|
public function findIn(array $inCriteria, $field = null) |
103
|
|
|
{ |
104
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
105
|
|
|
|
106
|
|
|
$result = $this->db |
107
|
|
|
->from($this->collection) |
108
|
|
|
->whereIn($field, $inCriteria) |
109
|
|
|
->get(); |
110
|
|
|
|
111
|
|
|
if ($result->count() > 0) { |
112
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
113
|
|
|
|
114
|
|
|
return $this->result; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
// ------------------------------------------------------------------------ |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Find By |
123
|
|
|
* |
124
|
|
|
* Find single record based on certain conditions |
125
|
|
|
* |
126
|
|
|
* @param array $conditions List of conditions with criteria |
127
|
|
|
* |
128
|
|
|
* @access protected |
129
|
|
|
* @return DataObjects\Result|bool Returns FALSE if failed. |
130
|
|
|
*/ |
131
|
|
|
public function findWhere(array $conditions, $limit = null) |
132
|
|
|
{ |
133
|
|
|
$result = $this->db |
134
|
|
|
->from($this->collection) |
135
|
|
|
->getWhere($conditions, $limit); |
136
|
|
|
|
137
|
|
|
if ($result->count() > 0) { |
138
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
139
|
|
|
|
140
|
|
|
if ($limit == 1) { |
141
|
|
|
return $this->result->first(); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->result; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
return false; |
148
|
|
|
} |
149
|
|
|
// ------------------------------------------------------------------------ |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Find In |
153
|
|
|
* |
154
|
|
|
* Find many records not within criteria on specific field |
155
|
|
|
* |
156
|
|
|
* @param array $notInCriteria List of criteria |
157
|
|
|
* @param string $field Table column field name | set to primary key by default |
158
|
|
|
* |
159
|
|
|
* @return DataObjects\Result|bool Returns FALSE if failed. |
160
|
|
|
*/ |
161
|
|
|
public function findNotIn(array $notInCriteria, $field = null) |
162
|
|
|
{ |
163
|
|
|
$field = isset($field) ? $field : $this->primaryKey; |
164
|
|
|
|
165
|
|
|
$result = $this->db |
166
|
|
|
->from($this->collection) |
167
|
|
|
->whereNotIn($field, $notInCriteria) |
168
|
|
|
->get(); |
169
|
|
|
|
170
|
|
|
if ($result->count() > 0) { |
171
|
|
|
$this->result = new DataObjects\Result($result, $this); |
|
|
|
|
172
|
|
|
|
173
|
|
|
return $this->result; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return false; |
177
|
|
|
} |
178
|
|
|
} |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.