|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Records\Traits\Searchable; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Database\Query\AbstractQuery as Query; |
|
6
|
|
|
use Nip\Records\Navigator\Pagination\Paginator; |
|
7
|
|
|
use Nip\Records\AbstractModels\Record; |
|
8
|
|
|
use Nip\Records\Collections\Collection as RecordCollection; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Trait SearchableRecordsTrait |
|
12
|
|
|
* @package Nip\Records\Traits\Searchable |
|
13
|
|
|
*/ |
|
14
|
|
|
trait SearchableRecordsTrait |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Returns paginated results |
|
19
|
|
|
* @param Paginator $paginator |
|
20
|
|
|
* @param array $params |
|
21
|
|
|
* @return mixed |
|
22
|
|
|
*/ |
|
23
|
|
|
public function paginate(Paginator $paginator, $params = []) |
|
24
|
|
|
{ |
|
25
|
|
|
$query = $this->paramsToQuery($params); |
|
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
$countQuery = $this->getDB()->newSelect(); |
|
|
|
|
|
|
28
|
|
|
$countQuery->count(['*', 'count']); |
|
29
|
|
|
$countQuery->from([$query, 'tbl']); |
|
30
|
|
|
$results = $countQuery->execute()->fetchResults(); |
|
31
|
|
|
$count = $results[0]['count']; |
|
32
|
|
|
|
|
33
|
|
|
$paginator->setCount($count); |
|
|
|
|
|
|
34
|
|
|
|
|
35
|
|
|
$params['limit'] = $paginator->getLimits(); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
return $this->findByParams($params); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param array $params |
|
42
|
|
|
*/ |
|
43
|
3 |
|
protected function injectParams(&$params = []) |
|
|
|
|
|
|
44
|
|
|
{ |
|
45
|
3 |
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return RecordCollection |
|
49
|
|
|
*/ |
|
50
|
|
|
public function findAll() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->findByParams(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param int $count |
|
57
|
|
|
* @return RecordCollection |
|
58
|
|
|
*/ |
|
59
|
|
|
public function findLast($count = 9) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->findByParams([ |
|
62
|
|
|
'limit' => $count, |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Checks the registry before fetching from the database |
|
68
|
|
|
* @param mixed $primary |
|
69
|
|
|
* @return Record |
|
70
|
|
|
*/ |
|
71
|
|
|
public function findOne($primary) |
|
72
|
|
|
{ |
|
73
|
|
|
$item = $this->getRegistry()->get($primary); |
|
|
|
|
|
|
74
|
|
|
if (!$item) { |
|
75
|
|
|
$all = $this->getRegistry()->get("all"); |
|
76
|
|
|
if ($all) { |
|
77
|
|
|
$item = $all[$primary]; |
|
78
|
|
|
} |
|
79
|
|
|
if (!$item) { |
|
80
|
|
|
$params['where'][] = ["`{$this->getTable()}`.`{$this->getPrimaryKey()}` = ?", $primary]; |
|
|
|
|
|
|
81
|
|
|
$item = $this->findOneByParams($params); |
|
|
|
|
|
|
82
|
|
|
if ($item) { |
|
|
|
|
|
|
83
|
|
|
$this->getRegistry()->set($primary, $item); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $item; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $item; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* When searching by primary key, look for items in current registry before |
|
95
|
|
|
* fetching them from the database |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $pk_list |
|
98
|
|
|
* @return RecordCollection |
|
99
|
|
|
*/ |
|
100
|
|
|
public function findByPrimary($pk_list = []) |
|
101
|
|
|
{ |
|
102
|
|
|
$pk = $this->getPrimaryKey(); |
|
103
|
|
|
$return = $this->newCollection(); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
if ($pk_list) { |
|
|
|
|
|
|
106
|
|
|
$pk_list = array_unique($pk_list); |
|
107
|
|
|
foreach ($pk_list as $key => $value) { |
|
108
|
|
|
$item = $this->getRegistry()->get($value); |
|
109
|
|
|
if ($item) { |
|
110
|
|
|
unset($pk_list[$key]); |
|
111
|
|
|
$return[$item->{$pk}] = $item; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
if ($pk_list) { |
|
|
|
|
|
|
115
|
|
|
$query = $this->paramsToQuery(); |
|
116
|
|
|
$query->where("$pk IN ?", $pk_list); |
|
117
|
|
|
$items = $this->findByQuery($query); |
|
118
|
|
|
|
|
119
|
|
|
if (count($items)) { |
|
120
|
|
|
foreach ($items as $item) { |
|
121
|
|
|
$this->getRegistry()->set($item->{$pk}, $item); |
|
122
|
|
|
$return[$item->{$pk}] = $item; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $return; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Finds one Record using params array |
|
134
|
|
|
* |
|
135
|
|
|
* @param array $params |
|
136
|
|
|
* @return Record|null |
|
137
|
|
|
*/ |
|
138
|
|
|
public function findOneByParams(array $params = []) |
|
139
|
|
|
{ |
|
140
|
|
|
$params['limit'] = 1; |
|
141
|
|
|
$records = $this->findByParams($params); |
|
142
|
|
|
if (count($records) > 0) { |
|
143
|
|
|
return $records->rewind(); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return null; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Finds Records using params array |
|
151
|
|
|
* |
|
152
|
|
|
* @param array $params |
|
153
|
|
|
* @return RecordCollection |
|
154
|
|
|
*/ |
|
155
|
|
|
public function findByParams($params = []) |
|
156
|
|
|
{ |
|
157
|
|
|
$query = $this->paramsToQuery($params); |
|
158
|
|
|
|
|
159
|
|
|
return $this->findByQuery($query, $params); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* @param Query $query |
|
164
|
|
|
* @param array $params |
|
165
|
|
|
* @return bool |
|
166
|
|
|
*/ |
|
167
|
|
|
public function findOneByQuery($query, $params = []) |
|
168
|
|
|
{ |
|
169
|
|
|
$query->limit(1); |
|
170
|
|
|
$return = $this->findByQuery($query, $params); |
|
171
|
|
|
if (count($return) > 0) { |
|
172
|
|
|
return $return->rewind(); |
|
|
|
|
|
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
return null; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
/** |
|
179
|
|
|
* @param $field |
|
180
|
|
|
* @param $value |
|
181
|
|
|
* @return mixed |
|
182
|
|
|
*/ |
|
183
|
|
|
public function findByField($field, $value) |
|
184
|
|
|
{ |
|
185
|
|
|
$params['where'][] = ["$field " . (is_array($value) ? "IN" : "=") . " ?", $value]; |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
|
|
return $this->findByParams($params); |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* @param Query $query |
|
192
|
|
|
* @param array $params |
|
193
|
|
|
* @return RecordCollection |
|
194
|
|
|
*/ |
|
195
|
|
|
public function findByQuery($query, $params = []) |
|
196
|
|
|
{ |
|
197
|
|
|
$return = $this->newCollection(); |
|
198
|
|
|
|
|
199
|
|
|
$results = $this->getDB()->execute($query); |
|
200
|
|
|
if ($results->numRows() > 0) { |
|
201
|
|
|
$pk = $this->getPrimaryKey(); |
|
202
|
|
|
/** @noinspection PhpAssignmentInConditionInspection */ |
|
203
|
|
|
while ($row = $results->fetchResult()) { |
|
204
|
|
|
$item = $this->getNew($row); |
|
|
|
|
|
|
205
|
|
|
if (is_string($pk)) { |
|
206
|
|
|
$this->getRegistry()->set($item->getPrimaryKey(), $item); |
|
207
|
|
|
} |
|
208
|
|
|
if (isset($params['indexKey']) && !empty($params['indexKey'])) { |
|
209
|
|
|
$return->add($item, $params['indexKey']); |
|
210
|
|
|
} else { |
|
211
|
|
|
$return->add($item); |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
return $return; |
|
217
|
|
|
} |
|
218
|
|
|
} |
|
219
|
|
|
|