27 private $iteratorCallback;
29 private $loaded =
false;
31 private $parentChanged =
false;
40 if ($this->parent !==
null && $this->parent !== $parent)
42 $this->parentChanged =
true;
45 $this->parent = $parent;
63 $this->setItem($item);
70 if ($item->getParentCollection() !== $this)
75 $item->setParentCollection(
null);
76 $this->unsetItem($item);
82 private function setItem(
BaseEntity $item): self
85 $this->items[$item->
getHash()] = $item;
86 unset($this->removedItems[$item->
getHash()]);
91 private function unsetItem(BaseEntity $item): self
93 unset($this->items[$item->getHash()]);
94 $this->removedItems[$item->getHash()] = $item;
108 unset($this->removedItems[$item->getHash()]);
120 foreach ($this->items as $item)
122 $item->clearChangedFields();
132 return empty($this->items);
137 if ($this->parentChanged)
142 if (!empty($this->removedItems))
147 foreach ($this->items as $entity)
149 if ($entity->isChanged())
162 if ($item->getId() === $id)
173 if ((empty($this->items) || $callback) && !$this->
isLoaded())
182 if ($callback($item))
190 return reset($this->items) ?:
null;
201 foreach ($this->items as $entity)
203 $fields = $entity->getFields();
205 if ($entity->isNew())
207 $result[
'n' . $counter++] = $fields;
211 $result[$entity->getId()] = $fields;
246 return new \ArrayIterator(array_values($this->removedItems));
257 return new \ArrayIterator(array_values($this->items));
262 if ($this->iteratorCallback && !$this->
isLoaded())
267 $this->loaded =
true;
275 return $this->loaded;
280 $iterator = $this->iteratorCallback;
285 foreach ($iterator($params) as $entity)
303 if ($this->iteratorCallback !==
null)
308 $this->iteratorCallback = $iteratorCallback;
323 foreach ($this->items as $item)
325 $res = $item->saveInternal();
327 if (!$res->isSuccess())
329 $result->addErrors($res->getErrors());
335 $res = $item->deleteInternal();
337 if ($res->isSuccess())
343 $result->addErrors($res->getErrors());
360 $items = array_merge($this->items, $this->removedItems);
364 $res = $item->deleteInternal();
366 if (!$res->isSuccess())
368 $result->addErrors($res->getErrors());