1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
Collection.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2;
4
5use Bitrix\Main\ORM;
6use Bitrix\Main\ORM\Data\DataManager;
7use Bitrix\Main\ArgumentTypeException;
8use Bitrix\Im\V2\Service\Context;
9
14abstract class Collection extends Registry implements ActiveRecordCollection
15{
17
18 // temporary entity id in registry
19 protected int $newEntityTmpId = 0;
20
21 public function __construct($source = null)
22 {
23 parent::__construct();
24
25 if (!empty($source))
26 {
27 $this->load($source);
28 }
29 }
30
35 final public static function getDataClass(): string
36 {
37 return static::getCollectionElementClass()::getDataClass();
38 }
39
44 abstract public static function getCollectionElementClass(): string;
45
53 abstract public static function find(array $filter, array $order, ?int $limit = null, ?Context $context = null): self;
54
61 public function add(ActiveRecord $entry): self
62 {
63 $collectionElementClass = static::getCollectionElementClass();
64
65 if (!($entry instanceof $collectionElementClass))
66 {
67 $entryClass = \get_class($entry);
68 throw new ArgumentTypeException("Entry is instance of {$entryClass}, but collection support {$collectionElementClass}");
69 }
70
71 if ($entry instanceof RegistryEntry)
72 {
73 $entry->setRegistry($this);
74 }
75
76 if ($entry->getPrimaryId())
77 {
78 parent::offsetSet($entry->getPrimaryId(), $entry);
79 }
80 else
81 {
82 $this->newEntityTmpId --;
83 parent::offsetSet($this->newEntityTmpId, $entry);
84 }
85
86 return $this;
87 }
88
95 public function offsetSet($offset, $entry): void
96 {
97 $collectionElementClass = static::getCollectionElementClass();
98
99 if (!($entry instanceof $collectionElementClass))
100 {
101 $entryClass = \get_class($entry);
102 throw new ArgumentTypeException("Entry is instance of {$entryClass}, but collection support {$collectionElementClass}");
103 }
104
105 if ($offset === null)
106 {
107 if ($entry->getPrimaryId())
108 {
109 $offset = $entry->getPrimaryId();
110 }
111 else
112 {
113 $this->newEntityTmpId --;
114 $offset = $this->newEntityTmpId;
115 }
116 }
117
118 parent::offsetSet($offset, $entry);
119 }
120
124 protected function prepareFields(): Result
125 {
126 $result = new Result;
127
128 $this->dataEntityCollection = null; //Resetting the collection of entities before filling it
129 $dataEntity = $this->getDataEntityCollection();
130
132 foreach ($this as $entity)
133 {
134 if ($entity->isDeleted())
135 {
136 continue;
137 }
138
139 $resultFill = $entity->prepareFields();
140
141 if (!$resultFill->isSuccess())
142 {
143 $result->addErrors($resultFill->getErrors());
144 continue;
145 }
146
147 if ($resultFill->getData()['SKIP_SAVE'] ?? false)
148 {
149 continue;
150 }
151
152 $dataEntity->add($entity->getDataEntity());
153 }
154
155 return $result;
156 }
157
161 public function getPrimaryIds(): array
162 {
163 $ids = [];
164
166 foreach ($this as $item)
167 {
168 if ($item->getPrimaryId())
169 {
170 $ids[] = $item->getPrimaryId();
171 }
172 }
173
174 return $ids;
175 }
176
181 public function load($source): Result
182 {
183 if (is_array($source))
184 {
185 if ($this->isArrayOfIds($source))
186 {
187 return $this->initByArrayOfPrimary($source);
188 }
189
190 return $this->initByArray($source);
191 }
192
193 if ($source instanceof ORM\Objectify\Collection)
194 {
195 return $this->initByEntitiesCollection($source);
196 }
197
198 return (new Result())->addError(new Error(Error::NOT_FOUND));
199 }
200
201
206 protected function setDataEntityCollection(ORM\Objectify\Collection $entityCollection): self
207 {
208 $this->dataEntityCollection = $entityCollection;
209 return $this;
210 }
211
216 public function getDataEntityCollection(): ORM\Objectify\Collection
217 {
218 if ($this->dataEntityCollection === null)
219 {
221 $dataClass = static::getDataClass();
223 $entityCollectionClass = $dataClass::getCollectionClass();
224 $this->dataEntityCollection = new $entityCollectionClass;
225 }
226
227 return $this->dataEntityCollection;
228 }
229
230 public function save(bool $isGroupSave = false): Result
231 {
232 $result = $this->prepareFields();
233
234 if ($result->isSuccess())
235 {
236 if ($isGroupSave)
237 {
238 $resultSave = $this->getDataEntityCollection()->save(true);
239 if (!$resultSave->isSuccess())
240 {
241 $result->addErrors($resultSave->getErrors());
242 }
243 }
244 else
245 {
246 $index = [];
247 foreach ($this as $inx => $entity)
248 {
249 $index[] = $inx;
250 }
251
253 foreach ($index as $inx)
254 {
255 $entity = $this[$inx];
256 if ($entity instanceof RegistryEntry)
257 {
258 $entity->setRegistry($this);
259 }
260 $resultSave = $entity->save();
261 if (!$resultSave->isSuccess())
262 {
263 $result->addErrors($resultSave->getErrors());
264 }
265 elseif ($inx < 0 && isset($this[$entity->getPrimaryId()]))
266 {
267 unset($this[$inx]);// remove temporary object link from registry
268 }
269 }
270 }
271 }
272
273 return $result;
274 }
275
280 public function delete(): Result
281 {
282 $result = new Result();
283
284 $idsToDelete = $this->getPrimaryIds();
285 if (method_exists(static::getDataClass(), 'deleteByFilter'))
286 {
287 if (empty($idsToDelete))
288 {
289 return $result;
290 }
291
292 $primaryField = static::getPrimaryFieldName();
293 static::getDataClass()::deleteByFilter(
294 [
295 "={$primaryField}" => $idsToDelete
296 ]
297 );
298
299 return $result;
300 }
301
302 foreach ($idsToDelete as $idToDelete)
303 {
304 $deleteResult = static::getDataClass()::delete($idToDelete);
305 if (!$deleteResult->isSuccess())
306 {
307 $result->addErrors($deleteResult->getErrors());
308 }
309 }
310
311 return $result;
312 }
313
317 public function hasUnsaved(): bool
318 {
320 foreach ($this as $entity)
321 {
322 if ($entity->getPrimaryId() === null)
323 {
324 return true;
325 }
326 if ($entity->isChanged())
327 {
328 return true;
329 }
330 if ($entity->isDeleted())
331 {
332 return true;
333 }
334 }
335
336 return false;
337 }
338
339 public function isEmpty(): bool
340 {
341 return $this->count() === 0;
342 }
343
349 protected function initByEntitiesCollection(ORM\Objectify\Collection $entitiesCollection): Result
350 {
351 $collectionClass = static::getDataClass()::getCollectionClass();
352
353 if (!($entitiesCollection instanceof $collectionClass))
354 {
355 $entryClass = \get_class($entitiesCollection);
356 throw new ArgumentTypeException("Entry is instance of {$entryClass}, but collection support {$collectionClass}");
357 }
358
359 $this->setDataEntityCollection($entitiesCollection);
360
361 return $this->initForEach($entitiesCollection);
362 }
363
368 protected function initByArray(array $items): Result
369 {
370 return $this->initForEach($items);
371 }
372
377 protected function initByArrayOfPrimary(array $ids): Result
378 {
379 $primaryField = static::getPrimaryFieldName();
380
381 if (empty($ids))
382 {
383 return new Result();
384 }
385
387 $entitiesCollection = static::getDataClass()::query()
388 ->setSelect(['*'])
389 ->whereIn($primaryField, $ids)
390 ->fetchCollection()
391 ;
392
393 return $this->initByEntitiesCollection($entitiesCollection);
394 }
395
400 private function initForEach($entitiesCollection): Result
401 {
402 $result = new Result();
403 $itemClass = static::getCollectionElementClass();
404
405 foreach ($entitiesCollection as $entity)
406 {
407 $item = new $itemClass;
408 $loadResult = $item->load($entity);
409
410 if (!$loadResult->isSuccess())
411 {
412 $result->addErrors($loadResult->getErrors());
413 }
414 elseif ($item instanceof RegistryEntry)
415 {
416 $item->setRegistry($this);
417 }
418 }
419
420 return $result;
421 }
422
423 private function isArrayOfIds(array $array): bool
424 {
425 foreach ($array as $key => $value)
426 {
427 if (!is_int($key) || !is_int($value))
428 {
429 return false;
430 }
431 }
432
433 return true;
434 }
435
440 private static function getPrimaryFieldName(): string
441 {
442 $primaryField = static::getDataClass()::getEntity()->getPrimary();
443
444 if (!is_scalar($primaryField))
445 {
446 throw new \Bitrix\Main\SystemException('Do not support composite primary keys');
447 }
448
449 return $primaryField;
450 }
451}
setDataEntityCollection(ORM\Objectify\Collection $entityCollection)
Определения Collection.php:206
offsetSet($offset, $entry)
Определения Collection.php:95
static find(array $filter, array $order, ?int $limit=null, ?Context $context=null)
static getCollectionElementClass()
__construct($source=null)
Определения Collection.php:21
ORM Objectify Collection $dataEntityCollection
Определения Collection.php:16
load($source)
Определения Collection.php:181
int $newEntityTmpId
Определения Collection.php:19
add(ActiveRecord $entry)
Определения Collection.php:61
static getDataClass()
Определения Collection.php:35
initByEntitiesCollection(ORM\Objectify\Collection $entitiesCollection)
Определения Collection.php:349
initByArray(array $items)
Определения Collection.php:368
const NOT_FOUND
Определения Error.php:9
Определения result.php:20
Определения error.php:15
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$entity
$filter
Определения iblock_catalog_list.php:54
Определения RegistryEntry.php:6
$context
Определения csv_new_setup.php:223
Определения culture.php:9
$order
Определения payment.php:8
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)
Определения prolog_main_admin.php:393
if(empty($signedUserToken)) $key
Определения quickway.php:257
$items
Определения template.php:224