Fill collection of records with all of their relations.

@param CollectionInterface $collection

@return CollectionInterface
496208
public function fillCollectionWithRelations(CollectionInterface $collection)
    {
        if (!$collection->count()) {
            return $collection;
        }

        foreach ($this->getWith() as $relation) {
            $relation->fillCollection($collection);
        }

        return $collection;
    }
1265673
private function loadReferenceManyCollectionOwningSide(PersistentCollectionInterface $collection)
    {
        $mapping = $collection->getMapping();

        /** @var Identifier[] $identifiers */
        $identifiers = array_map(
            function ($referenceData) use ($mapping) {
                return $this->ap->loadReference($mapping, $referenceData);
            },
            $collection->getRawData()
        );

        //reverse + limit must be applied here rather than on the query,
        //since we are potentially performing multiple queries
        if ($mapping->reverse) {
            $identifiers = array_reverse($identifiers);
        }
        if ($mapping->limit !== null) {
            $identifiers = array_slice($identifiers, 0, $mapping->limit);
        }

        //build document proxies and group by class
        $groupedIds = [];
        foreach ($identifiers as $key => $identifier) {
            //create a reference to the class+id and add it to the collection
            $reference = $identifier->getDocument($this->dm);

            if ($mapping->type === Type::MAP) {
                $collection->set($key, $reference);
            } else {
                $collection->add($reference);
            }

            //only query for the referenced object if it is not already initialized
            if ($reference instanceof Proxy && !$reference->__isInitialized__) {
                $className                = $identifier->class->name;
                $groupedIds[$className][] = $identifier;
            }
        }

        //load documents
        foreach ($groupedIds as $className => $batch) {
            $class   = $this->dm->getClassMetadata($className);
            $filters = array_merge(
                [(array)$mapping->criteria],
                $this->dm->getFilterCollection()->getFilterCriteria($class)
            );

            $qb = $this->dm->createQueryBuilder($className);

            if (!empty($filters)) {
                //filters require a full table scan
                //TODO: needs documentation

                //add identifiers to the query as OR conditions
                /** @var Identifier $identifier */
                foreach ($batch as $identifier) {
                    $expr = $qb->expr();
                    $identifier->addToQueryCondition($expr);
                    $qb->addOr($expr);
                }

                //add any other filter criteria
                foreach ($filters as $filter) {
                    $qb->addCriteria($filter);
                }

                $query = $qb->getQuery();
            } else {
                //load using batchgetitem
                foreach ($batch as $identifier) {
                    $qb->get()->setIdentifier($identifier)->batch();
                }

                $query = $qb->getBatch();
            }

            foreach ($query->getMarshalledResult() as $documentData) {
                $identifier = $class->createIdentifier($documentData);
                $document   = $this->uow->getByIdentifier($identifier);

                if ($document instanceof Proxy && !$document->__isInitialized()) {
                    $data = $this->hydratorFactory->hydrate($document, $documentData);
                    $this->uow->setOriginalDocumentData($document, $data);
                    $document->__isInitialized__ = true;
                }
            }
        }
    }
148649
public function addCollection(Collection $collection)
    {
        $metadata = $collection->getDocumentMetadata();
        $this->collectionMapping[$metadata->getName()] = $collection;
    }
744416
public function make($collection)
    {
        $collection = $this->getCollectionNameFromInstance($collection);

        $this->dirty = true;
        
        return $this->get($collection) ?: $this->entries[$collection] = new Entry;
    }
963679
private function resolveObject($collection, $id)
    {
        return $this->connection->selectCollection($this->databaseName, $collection)->findOne(['_id' => $id]);
    }
841032
public function renumberCollection(Collection $collection)
    {
        $i = 0;

        foreach ( $collection as $item ) {
            if ( $item instanceof NumericallySortableContract ) {
                $item->updateSortingOrder(++$i);
            }
        }

        return $this;
    }
1106069
public function merge(ObjectCollectionInterface $collection)
    {
        $this->items = array_merge(
            $this->getItems(),
            $collection->getItems()
        );

        return $this;
    }
555720
public function setCollection($collection)
    {
        if (!$this->loaded) {
            $this->load();
        }

        $this->collectionInfo->setCollection($collection);
        return $this;
    }
358411
public function createCollection(Collection $collection): Collection
    {
        $query = $this->connection->createQueryBuilder()
            ->insert('ngbm_collection')
            ->values(
                [
                    'id' => ':id',
                    'status' => ':status',
                    'start' => ':start',
                    'length' => ':length',
                    'translatable' => ':translatable',
                    'main_locale' => ':main_locale',
                    'always_available' => ':always_available',
                ]
            )
            ->setValue(
                'id',
                $collection->id !== null ?
                    (int) $collection->id :
                    $this->connectionHelper->getAutoIncrementValue('ngbm_collection')
            )
            ->setParameter('status', $collection->status, 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);

        $query->execute();

        $collection->id = $collection->id ?? (int) $this->connectionHelper->lastInsertId('ngbm_collection');

        return $collection;
    }
944128
public function apply(Collection $collection)
    {
        $this->collection = $collection;

        foreach ($this->filters() as $name => $value) {
            if (! method_exists($this, $name)) {
                continue;
            }

            if (is_array($value) or trim($value)) {
                $this->collection = $this->$name($value);
            } else {
                $this->collection = $this->$name();
            }
        }

        return $this->collection;
    }
355947
public function factory($collection)
    {
        if ($collection instanceof Collection) {
            $collectionName = $collection->getName();
        } else {
            $collectionName = $collection;
        }

        if (!isset($this->collections[$collectionName])) {
            if (!($collection instanceof Collection)) {
                $collection = Norm::createCollection(array(
                    'name' => $collection,
                    'connection' => $this,
                ));

                $this->applyHook('norm.after.factory', $collection);
            }

            $this->collections[$collectionName] = $collection;
        }

        return $this->collections[$collectionName];
    }
663491
public function combine($values): CollectionInterface
    {
        if ($values instanceof IteratorAggregate) {
            $values = $values->getIterator();
        } elseif (is_array($values)) {
            $values = new ArrayIterator($values);
        }

        return $this->wrap(function (Iterator $keys) use ($values) {
            foreach ($keys as $key) {
                yield $key => $values->current();
                $values->next();
            }
        });
    }
1075635
public function addModel(ModelInterface $model): ModelCollectionInterface
    {
        $this->propertyReflection->setValue($model, $this->foreignId);

        $this->models[$model->getId()] = $model;

        return $this;
    }
1019909
public function add($row = []): CollectionInterface
    {
        //if it's an array
        if (is_array($row)) {
            //make it a model
            $row = $this->getModel($row);
        }

        //add it now
        $this->data[] = $row;

        return $this;
    }
1194822
public function insert(array $collection)
    {
        foreach ($collection as $key => $inputs) {
            $collection[$key] = $this->purifyInputs($inputs);
        }

        return $this->model->insert($collection);
    }
1135369
public function collection(Collection $collection)
    {
        $entries = new Entries();

        foreach ($collection as $group) {
            $entry = new Entry($group);

            $entry
                ->conditionalField('id', function () {
                    return Subbly::api('subbly.user')->hasAccess('subbly.backend.auth');
                })

                ->field('name')
                ->field('permissions')

                ->dateField('created_at')
                ->dateField('updated_at')
            ;

            $entries->addEntry($entry);
        }

        return $entries->toArray();
    }
1221417
public function merge(CollectionInterface ...$collections): CollectionInterface
    {
        $temp = [$this->data];

        foreach ($collections as $index => $collection) {
            if (!$collection instanceof static) {
                throw new CollectionMismatchException(
                    \sprintf('Collection with index %d must be of type %s', $index, static::class)
                );
            }

            $temp[] = $collection->toArray();
        }

        return new static(\array_merge(...$temp));
    }