protected function getCollection($forceNew = false)
{
if ($this->collection !== null && !$forceNew) {
return $this->collection;
}
if ($this->parent->isPersisted()) {
$collection = $this->createCollection();
} else {
$collection = new EmptyCollection();
}
if ($this->toAdd || $this->toRemove || $this->added || $this->removed) {
$all = [];
foreach ($collection as $entity) {
$all[spl_object_hash($entity)] = $entity;
}
foreach ($this->added as $hash => $entity) {
$all[$hash] = $entity;
}
foreach ($this->removed as $hash => $entity) {
unset($all[$hash]);
}
foreach ($this->toAdd as $hash => $entity) {
$all[$hash] = $entity;
}
foreach ($this->toRemove as $hash => $entity) {
unset($all[$hash]);
}
$collection = new ArrayCollection(array_values($all), $this->getTargetRepository());
$collection = $this->applyDefaultOrder($collection);
}
return $this->collection = $collection;
}