This will take an ArrayCollection of DTO objects and replace them with the Entities

@param Collection $collection

@throws MultipleValidationException
@throws ValidationException
774200
private function convertCollectionOfDtosToEntities(Collection $collection)
    {
        if (0 === $collection->count()) {
            return;
        }
        list($dtoFqn, $collectionEntityFqn) = $this->deriveDtoAndEntityFqnFromCollection($collection);

        foreach ($collection as $key => $dto) {
            if ($dto instanceof $collectionEntityFqn) {
                continue;
            }
            if (false === \is_object($dto)) {
                throw new \InvalidArgumentException('Unexpected DTO value ' .
                                                    \print_r($dto, true) .
                                                    ', expected an instance of' .
                                                    $dtoFqn);
            }
            if (false === ($dto instanceof DataTransferObjectInterface)) {
                throw new \InvalidArgumentException('Found none DTO item in collection, was instance of ' .
                                                    \get_class($dto));
            }
            if (false === ($dto instanceof $dtoFqn)) {
                throw new \InvalidArgumentException('Unexpected DTO ' . \get_class($dto) . ', expected ' . $dtoFqn);
            }
            $collection->set($key, $this->createEntity($collectionEntityFqn, $dto, false));
        }
    }
773542
public function formatCollection($collection, $format = 'json')
    {
        $items = array();

        foreach ($collection as $item) {
            $items[] = $item;
        }

        $context = new SerializationContext();
        $context->setSerializeNull(true);
        $context->enableMaxDepthChecks(true);

        return $this->getSerializer()->serialize($items, 'json', $context);
    }
74071
public function addCollection(RequirementCollection $collection)
    {
        $this->requirements = array_merge($this->requirements, $collection->all());
    }
721569
public function validateCollectionUpdateStruct(Collection $collection, CollectionUpdateStruct $collectionUpdateStruct): void
    {
        if ($collectionUpdateStruct->offset !== null) {
            $offsetConstraints = [
                new Constraints\NotBlank(),
                new Constraints\Type(['type' => 'int']),
            ];

            $offsetConstraints[] = $collection->hasQuery() ?
                new Constraints\GreaterThanOrEqual(['value' => 0]) :
                new Constraints\EqualTo(['value' => 0]);

            $this->validate(
                $collectionUpdateStruct->offset,
                $offsetConstraints,
                'offset'
            );
        }

        if ($collectionUpdateStruct->limit !== null) {
            $this->validate(
                $collectionUpdateStruct->limit,
                [
                    new Constraints\NotBlank(),
                    new Constraints\Type(['type' => 'int']),
                    new Constraints\GreaterThanOrEqual(['value' => 0]),
                ],
                'limit'
            );
        }
    }
102572
public static function fromCollection($collection) {
		if (!(is_array($collection) || $collection instanceof Iterator)) {
			throw new InvalidArgumentException('$collection is neither an array nor an iterator');
		}

		return self::toCollection($collection);
	}
1282259
public function merge(ArrayCollection $collection)
    {
        if (!$collection instanceof GeometryCollection || $collection->getGeometryType() !== $this->getGeometryType()) {
            throw new InvalidArgumentException("Collections types don't match, you can't merge.");
        }

        return parent::merge($collection);
    }
1297093
public function createCollectionTranslation(Collection $collection, string $locale): void
    {
        $query = $this->connection->createQueryBuilder()
            ->insert('ngbm_collection_translation')
            ->values(
                [
                    'collection_id' => ':collection_id',
                    'status' => ':status',
                    'locale' => ':locale',
                ]
            )
            ->setParameter('collection_id', $collection->id, Type::INTEGER)
            ->setParameter('status', $collection->status, Type::INTEGER)
            ->setParameter('locale', $locale, Type::STRING);

        $query->execute();
    }
396985
public function setCollection($collection)
    {
        $this->collection = (array) $collection;
        $this->setCount(count($this->collection));

        return $this;
    }
166227
public function loadCollection(PersistentCollection $collection)
    {
        $association = $collection->getMapping();
        $persister   = $this->getEntityPersister($association->getTargetEntity());

        if ($association instanceof OneToManyAssociationMetadata) {
            $persister->loadOneToManyCollection($association, $collection->getOwner(), $collection);
        } else {
            $persister->loadManyToManyCollection($association, $collection->getOwner(), $collection);
        }

        $collection->setInitialized(true);
    }
438916
public function updateCollection(Collection $collection): void
    {
        $query = $this->connection->createQueryBuilder();
        $query
            ->update('ngbm_collection')
            ->set('start', ':start')
            ->set('length', ':length')
            ->set('translatable', ':translatable')
            ->set('main_locale', ':main_locale')
            ->set('always_available', ':always_available')
            ->where(
                $query->expr()->eq('id', ':id')
            )
            ->setParameter('id', $collection->id, Type::INTEGER)
            ->setParameter('start', $collection->offset, Type::INTEGER)
            ->setParameter('length', $collection->limit, Type::INTEGER)
            ->setParameter('translatable', $collection->isTranslatable, Type::BOOLEAN)
            ->setParameter('main_locale', $collection->mainLocale, Type::STRING)
            ->setParameter('always_available', $collection->alwaysAvailable, Type::BOOLEAN);

        $this->applyStatusCondition($query, $collection->status);

        $query->execute();
    }
148649
public function addCollection(Collection $collection)
    {
        $metadata = $collection->getDocumentMetadata();
        $this->collectionMapping[$metadata->getName()] = $collection;
    }
1052666
function giveCollection(MongoDB\Collection $collection)
    {
        if ($this->collection)
            throw new exImmutable;


        $this->collection = $collection;
        return $this;
    }
1133092
protected function collection_to_array($collection)
    {
        if ($collection instanceof self) {
            return $collection->toArray();

        } else if ($collection instanceof \Traversable) {
            return iterator_to_array($collection);

        } else if ($collection instanceof Arrayable) {
            return $collection->toArray();

        } else if ($collection instanceof Jsonable) {
            return json_decode($collection->toJson(), true);

        } else if (is_object($collection) || is_array($collection)) {
            return (array)$collection;

        } else {
            throw new \InvalidArgumentException(sprintf(
                '$collection has type %s, which is not collection-like',
                gettype($collection)
            ));
        }
    }
845420
protected function configureRoutes(RouteCollection $collection)
    {
        parent::configureRoutes($collection);
        $collection->add('duplicate', $this->getRouterIdParameter() . '/duplicate');
        $collection->add('generateEntityCode');

        /* Needed or not needed ...
         * in sonata-project/admin-bundle/Controller/CRUDController.php
         * the batchAction method
         * throw exception if the http method is not POST
        */
        if ($collection->get('batch')) {
            $collection->get('batch')->setMethods(['POST']);
        }
    }
339133
protected function removeCollection(Collection $collection) {
		$transaction = $this->getTransactionsManager()->get(true);
			
		foreach ($collection as $model) {
			$phalconModel = $this->createTableModel($model);
			$phalconModel->setTransaction($transaction);

			if (! $phalconModel->delete()) {
				$transaction->rollback();
			}
		}

		$transaction->commit();
		return true;
	}
1012083
def validate_collections(self, model, context=None):
        
        result = Result()
        for property_name in self.collections:
            prop = self.collections[property_name]
            collection = self.get(model, property_name)

            errors = prop.validate(
                value=collection,
                model=model,
                context=context
            )
            if len(errors):
                result.add_collection_errors(
                    property_name=property_name,
                    direct_errors=errors
                )

            collection_errors = prop.validate_with_schema(
                collection=collection,
                context=context
            )

            result.add_collection_errors(
                property_name=property_name,
                collection_errors=collection_errors
            )

        return result
1118904
public function update(RepeatingFieldCollection $collection)
    {
        $updated = [];
        // First give priority to already existing entities
        foreach ($collection->flatten() as $entity) {
            $master = $this->getOriginal($entity);
            $master->setValue($entity->getValue());
            $master->setFieldtype($entity->getFieldtype());
            $master->handleStorage($this->getFieldType($entity->getFieldname(), $entity->getBlock()));

            $updated[] = $master;
        }

        $deleted = [];
        foreach ($this->flatten() as $old) {
            if (!in_array($old, $updated)) {
                $deleted[] = $old;
            }
        }

        // Clear the collection so that we re-add only the updated elements
        $this->clear();
        foreach ($updated as $new) {
            $this->add($new);
        }

        return $deleted;
    }