Hydrate the pivot table relationship on the models.

@param array $entities

@return array
778708
protected function hydratePivotRelation(array $entities)
    {
        // TODO (note) We should definitely get rid of the pivot in a next
        // release, as this is not quite relevant in a datamapper context.
        return array_map(function ($entity) {
            $entityWrapper = $this->factory->make($entity);

            $pivotAttributes = $this->cleanPivotAttributes($entityWrapper);
            $pivot = $this->newExistingPivot($pivotAttributes);
            $entityWrapper->setEntityAttribute('pivot', $pivot);

            $object = $entityWrapper->unwrap();

            return $object;
        }, $entities);
    }
112251
protected function detachExcept(array $entities = [])
    {
        $query = $this->newPivotQuery();

        if (count($entities) > 0) {
            $keys = $this->getKeys($entities);

            $query->whereNotIn($this->otherKey, $keys);
        }
        $parentKey = $this->parentMap->getKeyName();

        $query->where($this->foreignKey, '=', $this->parent->getEntityAttribute($parentKey));

        $query->delete();
    }
1360273
private function formatEntities(array $entities) : string
    {
        $formattedEntities = array_map(function (DistanceMatrixLocation $entry) {
            return $entry->getFormattedLocation();
        }, $entities);
        $formattedEntities = implode('|', $formattedEntities);

        return $formattedEntities;
    }
578829
public function resetStats(array $entities)
    {
        foreach ($entities as $entity) {
            $ids = $this->getService('repos:base.ownership')
                        ->getQuery()
                        ->ownable($entity)
                        ->disableChain()
                        ->fetchValues('owner.id');
            $entity->set('sharedOwnerCount', count($ids));
            $entity->set('sharedOwnerIds', AnDomainAttribute::getInstance('set')->setData($ids));
        }
    }
431099
protected function hydratePivotRelation(array $models)
    {
        // To hydrate the pivot relationship, we will just gather the pivot attributes
        // and create a new Pivot model, which is basically a dynamic model that we
        // will set the attributes, table, and connections on it so it will work.
        foreach ($models as $model) {
            $model->setRelation('pivot', $this->newExistingPivot(
                $this->migratePivotAttributes($model)
            ));
        }
    }
1243006
protected function hydrateProperties($entity, $data)
    {
        if (isset($data['attributes']) && is_array($data['attributes'])) {
            $entity = parent::hydrateProperties($entity, $data['attributes']);
        }

        return $entity;
    }
976548
public static function hydrateCollection(array $controllers)
    {
        $hydrated = [];

        foreach ($controllers as $controller) {
            $hydrated[] = self::hydrate($controller);
        }

        return $hydrated;
    }
1078933
protected function parseWithRelations( array $relations )
	{
		$results = [];

		foreach ( $relations as $name => $constraints )
		{
			// If the "relation" value is actually a numeric key, we can assume that no
			// constraints have been specified for the eager load and we'll just put
			// an empty Closure with the loader so that we can treat all the same.
			if ( is_numeric( $name ) )
			{
				$f = function ()
				{
					//
				};

				list( $name, $constraints ) = [$constraints, $f];
			}

			// We need to separate out any nested includes. Which allows the developers
			// to load deep relationships using "dots" without stating each level of
			// the relationship with its own key in the array of eager load names.
			$results = $this->parseNestedWith( $name, $results );

			$results[$name] = $constraints;
		}

		return $results;
	}
1059900
public function eagerLoadRelations(array $models)
	{
		foreach ($this->eagerLoad as $name => $constraints)
		{
			// For nested eager loads we'll skip loading them here and they will be set as an
			// eager load on the query to retrieve the relation so that they will be eager
			// loaded on that query, because that is where they get hydrated as models.
			if (strpos($name, '.') === false)
			{
				$models = $this->loadRelation($models, $name, $constraints);
			}
		}

		return $models;
	}
1336911
public function match(array $models, array $results, $relation)
    {
        $dictionary = $this->buildDictionary($results);

        // Once we have an array dictionary of child objects we can easily match the
        // children back to their parent using the dictionary and the keys on the
        // the parent models. Then we will return the hydrated models back out.
        foreach ($models as $model) {
            if (isset($dictionary[$key = $model->getKey()])) {
                $model->setRelation(
                    $relation, $dictionary[$key]
                );
            } else {
                $model->setRelation($relation, []);
            }
        }

        return $models;
    }
641846
public static function hydrateCollection(array $sources)
    {
        $hydrated = [];

        foreach ($sources as $source) {
            $hydrated[] = self::hydrate($source);
        }

        return $hydrated;
    }
746388
public function get($columns = ['*']) : Collection
    {
        // First we'll add the proper select columns onto the query so it is run with
        // the proper columns. Then, we will get the results and hydrate out pivot
        // models with the result of those columns as a separate model relation.
        $columns = $this->query->getQuery()->columns ? [] : $columns;

        $select = $this->getSelectColumns($columns);

        $entities = $this->query->addSelect($select)->disableCache()->get()->all();

        $entities = $this->hydratePivotRelation($entities);

        return $this->relatedMap->newCollection($entities);
    }
299792
public static function hydrateCollection(array $clients)
    {
        $hydrated = [];

        foreach ($clients as $client) {
            $hydrated[] = self::hydrate($client);
        }

        return $hydrated;
    }
1032827
public function allocateIdsToEntities(array $entities)
    {
        $this->validateBatch($entities, EntityInterface::class);

        $incompleteKeys = [];
        foreach ($entities as $entity) {
            if ($entity->key()->state() === Key::STATE_INCOMPLETE) {
                $incompleteKeys[] = $entity->key();
            }
        }

        if (!empty($incompleteKeys)) {
            $keys = $this->allocateIds($incompleteKeys);
        }

        return $entities;
    }
1141256
protected function hydrateAssociations($entity, $data)
    {
        if (isset($data['relationships']) && is_array($data['relationships'])) {
            $metadata = $this->entityManager->getClassMetadata(get_class($entity));

            foreach ($data['relationships'] as $name => $data) {
                if (!isset($metadata->associationMappings[$name])) {
                    throw new \Exception(sprintf('Relation `%s` association not found', $name));
                }

                $mapping = $metadata->associationMappings[$name];

                if (is_array($data['data'])) {
                    if ($resourceId = $this->getResourceId($data['data'])) {
                        $this->hydrateToOneAssociation($entity, $name, $mapping, $resourceId);
                    } else {
                        $this->hydrateToManyAssociation($entity, $name, $mapping,
                            $this->mapRelationshipsArray($data['data'])
                        );
                    }
                }
            }
        }

        return $entity;
    }
906917
public static function buildMultiply($entities)
    {
        $templates = [];

        foreach ($entities as $entity) {
            $templates[$entity->language] = static::buildFromEntity($entity);
        }

        return $templates;
    }
1221424
protected function dehydrate($entity): array
    {
        $properties = $this->hydrator->extract($entity);

        $attributesName = $this->entityMap->getAttributesArrayName();

        if (isset($properties[$attributesName])) {
            $properties[$attributesName] = $this->entityMap->getColumnNamesFromAttributes($properties[$attributesName]);
        } else {
            $properties[$attributesName] = $this->entityMap->getColumnNamesFromAttributes($properties);
        }

        $this->properties = $properties;

        $this->unmanagedProperties = array_except($properties, $this->getManagedProperties());

        return $this->attributesFromProperties($properties);
    }