|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Arrilot\BitrixModels\Queries; |
|
4
|
|
|
|
|
5
|
|
|
use Arrilot\BitrixModels\Adapters\D7Adapter; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
|
|
8
|
|
|
class D7Query extends BaseQuery |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Query select. |
|
12
|
|
|
* |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
public $select = ['*']; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Query group by. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
public $group = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Query runtime. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
public $runtime = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Query limit. |
|
33
|
|
|
* |
|
34
|
|
|
* @var int|null |
|
35
|
|
|
*/ |
|
36
|
|
|
public $limit = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Query offset. |
|
40
|
|
|
* |
|
41
|
|
|
* @var int|null |
|
42
|
|
|
*/ |
|
43
|
|
|
public $offset = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Cache joins? |
|
47
|
|
|
* |
|
48
|
|
|
* @var bool |
|
49
|
|
|
*/ |
|
50
|
|
|
public $cacheJoins = false; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Data doubling? |
|
54
|
|
|
* |
|
55
|
|
|
* @var bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public $dataDoubling = true; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Adapter to interact with Bitrix D7 API. |
|
61
|
|
|
* |
|
62
|
|
|
* @var D7Adapter |
|
63
|
|
|
*/ |
|
64
|
|
|
protected $bxObject; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get count of users that match $filter. |
|
68
|
|
|
* |
|
69
|
|
|
* @return int |
|
70
|
|
|
*/ |
|
71
|
|
|
public function count() |
|
72
|
|
|
{ |
|
73
|
|
|
$className = $this->bxObject->getClassName(); |
|
74
|
|
|
$queryType = 'D7Query::count'; |
|
75
|
|
|
$filter = $this->filter; |
|
76
|
|
|
|
|
77
|
|
|
$callback = function () use ($filter) { |
|
78
|
|
|
return (int) $this->bxObject->getCount($filter); |
|
79
|
|
|
}; |
|
80
|
|
|
|
|
81
|
|
|
return $this->handleCacheIfNeeded(compact('className', 'filter', 'queryType'), $callback); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Get list of items. |
|
86
|
|
|
* |
|
87
|
|
|
* @return Collection |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function loadModels() |
|
90
|
|
|
{ |
|
91
|
|
|
$params = [ |
|
92
|
|
|
'select' => $this->select, |
|
93
|
|
|
'filter' => $this->filter, |
|
94
|
|
|
'group' => $this->group, |
|
95
|
|
|
'order' => $this->sort, |
|
96
|
|
|
'limit' => $this->limit, |
|
97
|
|
|
'offset' => $this->offset, |
|
98
|
|
|
'runtime' => $this->runtime, |
|
99
|
|
|
]; |
|
100
|
|
|
|
|
101
|
|
|
if ($this->cacheTtl && $this->cacheJoins) { |
|
102
|
|
|
$params['cache'] = ['ttl' => $this->cacheTtl, 'cache_joins' => true]; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$className = $this->bxObject->getClassName(); |
|
106
|
|
|
$queryType = 'D7Query::getList'; |
|
107
|
|
|
$keyBy = $this->keyBy; |
|
108
|
|
|
|
|
109
|
|
|
$callback = function () use ($className, $params) { |
|
110
|
|
|
$rows = []; |
|
111
|
|
|
$result = $this->bxObject->getList($params); |
|
112
|
|
|
while ($row = $result->fetch()) { |
|
113
|
|
|
$this->addItemToResultsUsingKeyBy($rows, new $this->modelName($row['ID'], $row)); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
return new Collection($rows); |
|
117
|
|
|
}; |
|
118
|
|
|
|
|
119
|
|
|
return $this->handleCacheIfNeeded(compact('className', 'params', 'queryType', 'keyBy'), $callback); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Setter for limit. |
|
124
|
|
|
* |
|
125
|
|
|
* @param int|null $value |
|
126
|
|
|
* @return $this |
|
127
|
|
|
*/ |
|
128
|
|
|
public function limit($value) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->limit = $value; |
|
131
|
|
|
|
|
132
|
|
|
return $this; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Setter for offset. |
|
137
|
|
|
* |
|
138
|
|
|
* @param int|null $value |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
|
|
public function offset($value) |
|
142
|
|
|
{ |
|
143
|
|
|
$this->offset = $value; |
|
144
|
|
|
|
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Setter for offset. |
|
150
|
|
|
* |
|
151
|
|
|
* @param array|\Bitrix\Main\Entity\ExpressionField $fields |
|
152
|
|
|
* @return $this |
|
153
|
|
|
*/ |
|
154
|
|
|
public function runtime($fields) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->runtime = is_array($fields) ? $fields : [$fields]; |
|
157
|
|
|
|
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Setter for cacheJoins. |
|
163
|
|
|
* |
|
164
|
|
|
* @param bool $value |
|
165
|
|
|
* @return $this |
|
166
|
|
|
*/ |
|
167
|
|
|
public function cacheJoins($value = true) |
|
168
|
|
|
{ |
|
169
|
|
|
$this->cacheJoins = $value; |
|
170
|
|
|
|
|
171
|
|
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
public function enableDataDoubling() |
|
175
|
|
|
{ |
|
176
|
|
|
$this->dataDoubling = true; |
|
177
|
|
|
|
|
178
|
|
|
return $this; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function disableDataDoubling() |
|
182
|
|
|
{ |
|
183
|
|
|
$this->dataDoubling = false; |
|
184
|
|
|
|
|
185
|
|
|
return $this; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* For testing. |
|
190
|
|
|
* |
|
191
|
|
|
* @param $bxObject |
|
192
|
|
|
* @return $this |
|
193
|
|
|
*/ |
|
194
|
|
|
public function setAdapter($bxObject) |
|
195
|
|
|
{ |
|
196
|
|
|
$this->bxObject = $bxObject; |
|
197
|
|
|
|
|
198
|
|
|
return $this; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|