23 parent::__construct();
37 return static::getCollectionElementClass()::
getDataClass();
63 $collectionElementClass = static::getCollectionElementClass();
65 if (!($entry instanceof $collectionElementClass))
67 $entryClass = \get_class($entry);
68 throw new ArgumentTypeException(
"Entry is instance of {$entryClass}, but collection support {$collectionElementClass}");
73 $entry->setRegistry($this);
82 $this->newEntityTmpId --;
83 parent::offsetSet($this->newEntityTmpId, $entry);
97 $collectionElementClass = static::getCollectionElementClass();
99 if (!($entry instanceof $collectionElementClass))
101 $entryClass = \get_class($entry);
102 throw new ArgumentTypeException(
"Entry is instance of {$entryClass}, but collection support {$collectionElementClass}");
105 if ($offset ===
null)
107 if ($entry->getPrimaryId())
109 $offset = $entry->getPrimaryId();
113 $this->newEntityTmpId --;
114 $offset = $this->newEntityTmpId;
118 parent::offsetSet($offset, $entry);
124 protected function prepareFields():
Result
128 $this->dataEntityCollection =
null;
129 $dataEntity = $this->getDataEntityCollection();
139 $resultFill =
$entity->prepareFields();
141 if (!$resultFill->isSuccess())
143 $result->addErrors($resultFill->getErrors());
147 if ($resultFill->getData()[
'SKIP_SAVE'] ??
false)
152 $dataEntity->add(
$entity->getDataEntity());
166 foreach ($this as $item)
168 if ($item->getPrimaryId())
170 $ids[] = $item->getPrimaryId();
183 if (is_array($source))
185 if ($this->isArrayOfIds($source))
187 return $this->initByArrayOfPrimary($source);
208 $this->dataEntityCollection = $entityCollection;
218 if ($this->dataEntityCollection ===
null)
223 $entityCollectionClass = $dataClass::getCollectionClass();
224 $this->dataEntityCollection =
new $entityCollectionClass;
227 return $this->dataEntityCollection;
230 public function save(
bool $isGroupSave =
false): Result
232 $result = $this->prepareFields();
238 $resultSave = $this->getDataEntityCollection()->save(
true);
239 if (!$resultSave->isSuccess())
241 $result->addErrors($resultSave->getErrors());
247 foreach ($this as $inx =>
$entity)
253 foreach ($index as $inx)
256 if (
$entity instanceof RegistryEntry)
261 if (!$resultSave->isSuccess())
263 $result->addErrors($resultSave->getErrors());
285 if (method_exists(static::getDataClass(),
'deleteByFilter'))
287 if (empty($idsToDelete))
292 $primaryField = static::getPrimaryFieldName();
293 static::getDataClass()::deleteByFilter(
295 "={$primaryField}" => $idsToDelete
302 foreach ($idsToDelete as $idToDelete)
304 $deleteResult = static::getDataClass()::
delete($idToDelete);
305 if (!$deleteResult->isSuccess())
307 $result->addErrors($deleteResult->getErrors());
317 public function hasUnsaved(): bool
322 if (
$entity->getPrimaryId() ===
null)
341 return $this->
count() === 0;
351 $collectionClass = static::getDataClass()::getCollectionClass();
353 if (!($entitiesCollection instanceof $collectionClass))
355 $entryClass = \get_class($entitiesCollection);
356 throw new ArgumentTypeException(
"Entry is instance of {$entryClass}, but collection support {$collectionClass}");
361 return $this->initForEach($entitiesCollection);
370 return $this->initForEach(
$items);
377 protected function initByArrayOfPrimary(
array $ids):
Result
379 $primaryField = static::getPrimaryFieldName();
387 $entitiesCollection = static::getDataClass()::query()
389 ->whereIn($primaryField, $ids)
393 return $this->initByEntitiesCollection($entitiesCollection);
400 private function initForEach($entitiesCollection): Result
403 $itemClass = static::getCollectionElementClass();
405 foreach ($entitiesCollection as
$entity)
407 $item =
new $itemClass;
408 $loadResult = $item->load(
$entity);
410 if (!$loadResult->isSuccess())
412 $result->addErrors($loadResult->getErrors());
414 elseif ($item instanceof RegistryEntry)
416 $item->setRegistry($this);
423 private function isArrayOfIds(
array $array): bool
425 foreach ($array as
$key => $value)
427 if (!is_int(
$key) || !is_int($value))
440 private static function getPrimaryFieldName(): string
442 $primaryField = static::getDataClass()::getEntity()->getPrimary();
444 if (!is_scalar($primaryField))
446 throw new \Bitrix\Main\SystemException(
'Do not support composite primary keys');
449 return $primaryField;
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)