22 parent::__construct();
36 return static::getCollectionElementClass()::
getDataClass();
52 abstract public static function find(array $filter, array $order, ?
int $limit =
null, ?
Context $context =
null): self;
62 $collectionElementClass = static::getCollectionElementClass();
64 if (!($entry instanceof $collectionElementClass))
66 $entryClass = \get_class($entry);
67 throw new ArgumentTypeException(
"Entry is instance of {$entryClass}, but collection support {$collectionElementClass}");
72 $entry->setRegistry($this);
81 $this->newEntityTmpId --;
82 parent::offsetSet($this->newEntityTmpId, $entry);
96 $collectionElementClass = static::getCollectionElementClass();
98 if (!($entry instanceof $collectionElementClass))
100 $entryClass = \get_class($entry);
101 throw new ArgumentTypeException(
"Entry is instance of {$entryClass}, but collection support {$collectionElementClass}");
104 if ($offset ===
null)
106 if ($entry->getPrimaryId())
108 $offset = $entry->getPrimaryId();
112 $this->newEntityTmpId --;
117 parent::offsetSet($offset, $entry);
123 protected function prepareFields():
Result
127 $this->dataEntityCollection =
null;
131 foreach ($this as $entity)
133 if ($entity->isDeleted())
138 $resultFill = $entity->prepareFields();
140 if (!$resultFill->isSuccess())
142 $result->addErrors($resultFill->getErrors());
146 $dataEntity->add($entity->getDataEntity());
160 foreach ($this as $item)
162 if ($item->getPrimaryId())
164 $ids[] = $item->getPrimaryId();
177 if (is_array($source))
179 if ($this->isArrayOfIds($source))
181 return $this->initByArrayOfPrimary($source);
187 if ($source instanceof ORM\Objectify\
Collection)
202 $this->dataEntityCollection = $entityCollection;
212 if ($this->dataEntityCollection ===
null)
215 $dataClass = static::getDataClass();
217 $entityCollectionClass = $dataClass::getCollectionClass();
218 $this->dataEntityCollection =
new $entityCollectionClass;
224 public function save(
bool $isGroupSave =
false): Result
226 $result = $this->prepareFields();
228 if ($result->isSuccess())
233 if (!$resultSave->isSuccess())
235 $result->addErrors($resultSave->getErrors());
241 foreach ($this as $inx => $entity)
247 foreach ($index as $inx)
249 $entity = $this[$inx];
250 if ($entity instanceof RegistryEntry)
252 $entity->setRegistry($this);
254 $resultSave = $entity->save();
255 if (!$resultSave->isSuccess())
257 $result->addErrors($resultSave->getErrors());
259 elseif ($inx < 0 && isset($this[$entity->getPrimaryId()]))
279 if (method_exists(static::getDataClass(),
'deleteByFilter'))
281 if (empty($idsToDelete))
286 $primaryField = static::getPrimaryFieldName();
287 static::getDataClass()::deleteByFilter(
289 "={$primaryField}" => $idsToDelete
296 foreach ($idsToDelete as $idToDelete)
298 $deleteResult = static::getDataClass()::
delete($idToDelete);
299 if (!$deleteResult->isSuccess())
301 $result->addErrors($deleteResult->getErrors());
311 public function hasUnsaved(): bool
314 foreach ($this as $entity)
316 if ($entity->getPrimaryId() ===
null)
320 if ($entity->isChanged())
324 if ($entity->isDeleted())
340 $collectionClass = static::getDataClass()::getCollectionClass();
342 if (!($entitiesCollection instanceof $collectionClass))
344 $entryClass = \get_class($entitiesCollection);
345 throw new ArgumentTypeException(
"Entry is instance of {$entryClass}, but collection support {$collectionClass}");
350 return $this->initForEach($entitiesCollection);
359 return $this->initForEach($items);
366 protected function initByArrayOfPrimary(array $ids):
Result
368 $primaryField = static::getPrimaryFieldName();
376 $entitiesCollection = static::getDataClass()::query()
378 ->whereIn($primaryField, $ids)
389 private function initForEach($entitiesCollection): Result
391 $result =
new Result();
392 $itemClass = static::getCollectionElementClass();
394 foreach ($entitiesCollection as $entity)
396 $item =
new $itemClass;
397 $loadResult = $item->load($entity);
399 if (!$loadResult->isSuccess())
401 $result->addErrors($loadResult->getErrors());
403 elseif ($item instanceof RegistryEntry)
405 $item->setRegistry($this);
412 private function isArrayOfIds(array $array): bool
414 foreach ($array as $key => $value)
416 if (!is_int($key) || !is_int($value))
429 private static function getPrimaryFieldName(): string
431 $primaryField = static::getDataClass()::getEntity()->getPrimary();
433 if (!is_scalar($primaryField))
435 throw new \Bitrix\Main\SystemException(
'Do not support composite primary keys');
438 return $primaryField;