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
|
|
|
* Checks the registry before fetching from the database |
49
|
|
|
* @param mixed $primary |
50
|
|
|
* @return Record |
51
|
|
|
*/ |
52
|
|
|
public function findOne($primary) |
53
|
|
|
{ |
54
|
|
|
$item = $this->getRegistry()->get($primary); |
|
|
|
|
55
|
|
|
if (!$item) { |
56
|
|
|
$all = $this->getRegistry()->get("all"); |
57
|
|
|
if ($all) { |
58
|
|
|
$item = $all[$primary]; |
59
|
|
|
} |
60
|
|
|
if (!$item) { |
61
|
|
|
$params['where'][] = ["`{$this->getTable()}`.`{$this->getPrimaryKey()}` = ?", $primary]; |
|
|
|
|
62
|
|
|
$item = $this->findOneByParams($params); |
|
|
|
|
63
|
|
|
if ($item) { |
64
|
|
|
$this->getRegistry()->set($primary, $item); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $item; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $item; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Query $query |
76
|
|
|
* @param array $params |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
public function findOneByQuery($query, $params = []) |
80
|
|
|
{ |
81
|
|
|
$query->limit(1); |
82
|
|
|
$return = $this->findByQuery($query, $params); |
83
|
|
|
if (count($return) > 0) { |
84
|
|
|
return $return->rewind(); |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return null; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param $field |
92
|
|
|
* @param $value |
93
|
|
|
* @return mixed |
94
|
|
|
*/ |
95
|
|
|
public function findByField($field, $value) |
96
|
|
|
{ |
97
|
|
|
$params['where'][] = ["$field " . (is_array($value) ? "IN" : "=") . " ?", $value]; |
|
|
|
|
98
|
|
|
|
99
|
|
|
return $this->findByParams($params); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param Query $query |
104
|
|
|
* @param array $params |
105
|
|
|
* @return RecordCollection |
106
|
|
|
*/ |
107
|
|
|
public function findByQuery($query, $params = []) |
108
|
|
|
{ |
109
|
|
|
$return = $this->newCollection(); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$results = $this->getDB()->execute($query); |
112
|
|
|
if ($results->numRows() > 0) { |
113
|
|
|
$pk = $this->getPrimaryKey(); |
114
|
|
|
/** @noinspection PhpAssignmentInConditionInspection */ |
115
|
|
|
while ($row = $results->fetchResult()) { |
116
|
|
|
$item = $this->getNew($row); |
|
|
|
|
117
|
|
|
if (is_string($pk)) { |
118
|
|
|
$this->getRegistry()->set($item->getPrimaryKey(), $item); |
119
|
|
|
} |
120
|
|
|
if (isset($params['indexKey']) && !empty($params['indexKey'])) { |
121
|
|
|
$return->add($item, $params['indexKey']); |
122
|
|
|
} else { |
123
|
|
|
$return->add($item); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $return; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|