Build model dictionary keyed by the relation's foreign key.

@param EntityCollection $results

@return array
493764
protected function buildDictionary(EntityCollection $results)
    {
        $dictionary = [];

        $foreign = $this->getPlainForeignKey();

        // First we will create a dictionary of models keyed by the foreign key of the
        // relationship as this will allow us to quickly access all of the related
        // models without having to do nested looping which will be quite slow.
        foreach ($results as $result) {
            $dictionary[$result->{$foreign}][] = $result;
        }

        return $dictionary;
    }
584409
public function match(array $results, $relation)
    {
        $foreign = $this->foreignKey;

        $other = $this->otherKey;

        // Execute the relationship and get related entities as an EntityCollection
        $entities = $this->getEager();

        // First we will get to build a dictionary of the child models by their primary
        // key of the relationship, then we can easily match the children back onto
        // the parents using that dictionary and the primary key of the children.
        $dictionary = [];

        // TODO ; see if otherKey is the primary key of the related entity, we can
        // simply use the EntityCollection key to match entities to results, which
        // will be much more efficient, and use this method as a fallback if the
        // otherKey is not the same as the primary Key.
        foreach ($entities as $entity) {
            $entity = $this->factory->make($entity);
            $dictionary[$entity->getEntityAttribute($other)] = $entity->getObject();
        }

        // Once we have the dictionary constructed, we can loop through all the parents
        // and match back onto their children using these keys of the dictionary and
        // the primary key of the children to map them onto the correct instances.
        return array_map(function ($result) use ($dictionary, $foreign, $relation) {
            if (array_key_exists($foreign, $result) && isset($dictionary[$result[$foreign]])) {
                $result[$relation] = $dictionary[$result[$foreign]];
            } else {
                $result[$relation] = null;
            }

            return $result;
        }, $results);
    }
120962
public function processColumnListing($results)
    {
        return array_values(array_map(function ($result)
        {
            $result = (object) $result;

            return $result->name;

        }, $results));
    }
892650
public function matchRelation($model, $results) {
        $data = [];
        foreach ($results as $item) {
            if (!$this->isMatchRelation($model, $item)) {
                continue;
            }
            if ($this->type == self::TYPE_ONE) {
                return $item;
            }
            $data[] = $item;
        }
        return $data;
    }
117868
def _build_dictionary(self, results):
        
        foreign = self._foreign_key

        dictionary = {}

        for result in results:
            key = getattr(result.pivot, foreign)
            if key not in dictionary:
                dictionary[key] = []

            dictionary[key].append(result)

        return dictionary
1363802
protected function getKeysFromResults(array $results, $key = null)
    {
        if (is_null($key)) {
            $key = $this->parentMap->getKeyName();
        }

        return array_unique(array_values(array_map(function ($value) use ($key) {
            return $value[$key];
        }, $results)));
    }
1139947
public function groupVersions($results)
    {
        $property = $this->versionAssociation()->getProperty();

        return $results->map(function (EntityInterface $row) use ($property) {
            $versionField = $this->_config['versionField'];
            $versions = (array)$row->get($property);
            $grouped = new Collection($versions);

            $result = [];
            foreach ($grouped->combine('field', 'content', 'version_id') as $versionId => $keys) {
                $entityClass = $this->_table->getEntityClass();
                $versionData = [
                    $versionField => $versionId
                ];

                $keys = $this->_convertFieldsToType($keys, 'toPHP');

                /* @var \Cake\Datasource\EntityInterface $versionRow */
                $versionRow = $grouped->match(['version_id' => $versionId])->first();

                foreach ($this->_config['additionalVersionFields'] as $mappedField => $field) {
                    if (!is_string($mappedField)) {
                        $mappedField = 'version_' . $field;
                    }
                    $versionData[$mappedField] = $versionRow->get($field);
                }

                $version = new $entityClass($keys + $versionData, [
                    'markNew' => false,
                    'useSetters' => false,
                    'markClean' => true
                ]);
                $result[$versionId] = $version;
            }

            $options = ['setter' => false, 'guard' => false];
            $row->set('_versions', $result, $options);
            unset($row[$property]);
            $row->clean();

            return $row;
        });
    }
204708
protected function collectionFromResult($result)
    {
        // If we have one result which is not an assoc array, make it the first element of an array for the
        // collectionFromResult function so we always return a collection from filter
        if ((bool) count(array_filter(array_keys($result), 'is_string'))) {
            $result = [$result];
        }

        $class = $this->class;
        $collection = [];

        foreach ($result as $r) {
            $collection[] = new $class($this->connection, $r);
        }

        return $collection;
    }
347185
protected function matchOneOrMany(array $results, $relation, $type)
    {
        $entities = $this->getEager();

        $dictionary = $this->buildDictionary($entities);

        $cache = $this->parentMapper->getEntityCache();

        $host = $this;

        // Once we have the dictionary we can simply spin through the parent models to
        // link them up with their children using the keyed dictionary to make the
        // matching very convenient and easy work. Then we'll just return them.
        return array_map(function ($result) use ($dictionary, $cache, $type, $relation, $host) {
            $key = $result[$host->localKey];

            if (isset($dictionary[$key])) {
                $value = $host->getRelationValue($dictionary, $key, $type);

                $result[$relation] = $value;

                // TODO : Refactor This
                $cache->cacheLoadedRelationResult($key, $relation, $value, $this);
            } else {
                $result[$relation] = $type === 'many' ? $this->relatedMap->newCollection() : null;
            }

            return $result;
        }, $results);
    }
839067
protected function matchOneOrMany(array $models, Collection $results, $relation, $type)
    {
        $dictionary = $this->buildDictionary($results);

        foreach ($models as $model) {
            $key = $model->getAttribute($this->localKey);

            if (isset($dictionary[$key])) {
                $value = $this->getRelationValue($dictionary, $key, $type);

                $model->setRelation($relation, $value);
            }
        }

        return $models;
    }
433630
protected function getAll($result = [])
    {

        if (array_key_exists("hits", $result)) {

            $new = [];

            foreach ($result["hits"]["hits"] as $row) {

                $model = $this->model ? new $this->model($row["_source"], true) : new Model($row["_source"], true);

                $model->setConnection($model->getConnection());
                $model->setIndex($row["_index"]);
                $model->setType($row["_type"]);

                // match earlier version

                $model->_index = $row["_index"];
                $model->_type = $row["_type"];
                $model->_id = $row["_id"];
                $model->_score = $row["_score"];

                $new[] = $model;
            }

            $new = new Collection($new);

            $new->total = $result["hits"]["total"];
            $new->max_score = $result["hits"]["max_score"];
            $new->took = $result["took"];
            $new->timed_out = $result["timed_out"];
            $new->scroll_id = isset($result["_scroll_id"]) ? $result["_scroll_id"] : NULL;
            $new->shards = (object)$result["_shards"];

            return $new;

        } else {

            return new Collection([]);

        }
    }
1297207
protected function getEagerModelKeys(array $results)
    {
        $keys = [];

        // First we need to gather all of the keys from the result set so we know what
        // to query for via the eager loading query. We will add them to an array then
        // execute a "where in" statement to gather up all of those related records.
        foreach ($results as $result) {
            if (array_key_exists($this->foreignKey, $result) && !is_null($value = $result[$this->foreignKey])) {
                $keys[] = $value;
            }
        }

        // If there are no keys that were not null we will just return an array with 0 in
        // it so the query doesn't fail, but will not return any results, which should
        // be what this developer is expecting in a case where this happens to them.
        if (count($keys) == 0) {
            return [0];
        }

        return array_values(array_unique($keys));
    }
36944
public function build(array $results, array $eagerLoads)
    {
        // Make a list of all primary key of the current result set. This will
        // allow us to group all polymorphic operations by type, then put
        // back every object in the intended order.
        $primaryKeyColumn = $this->entityMap->getKeyName();
        $ids = array_map(function ($row) use ($primaryKeyColumn) {
            return $row[$primaryKeyColumn];
        }, $results);

        $results = array_combine($ids, $results);

        // Make a list of types appearing within this result set.
        $discriminatorColumn = $this->entityMap->getDiscriminatorColumn();
        $types = array_unique(array_pluck($results, $discriminatorColumn));

        // We'll split the result set by type that will make it easier to deal
        // with.
        $entities = [];

        foreach ($types as $type) {
            $this->mappers[$type] = $this->getMapperForType($type);

            $resultsByType[$type] = array_filter($results, function (array $row) use ($type, $discriminatorColumn) {
                return $row[$discriminatorColumn] === $type;
            });

            $entities = $entities + $this->buildResultsForType($resultsByType[$type], $type, $eagerLoads);
        }

        return array_map(function ($id) use ($entities) {
            return $entities[$id];
        }, $ids);
    }
1031735
public function matchOne(array $models, Collection $results, $relation)
    {
        return $this->matchOneOrMany($models, $results, $relation, 'one');
    }
1277864
public function match(array $results, $relation)
    {
        foreach (array_keys($this->dictionary) as $type) {
            $results = $this->matchToMorphParents($type, $this->getResultsByType($type), $results);
        }

        return $results;
    }
111564
protected function buildDictionary(array $results)
    {
        // First we will build a dictionary of child models keyed by the foreign key
        // of the relation so that we will easily and quickly match them to their
        // parents without having a possibly slow inner loops for every models.
        $dictionary = [];

        foreach ($results as $result) {
            $dictionary[$result->pivot->{$this->foreignKey}][] = $result;
        }

        return $dictionary;
    }
978764
protected function queryEagerLoadedRelationships(array $results, array $eagerLoads) : array
    {
        $this->eagerLoads = $this->parseRelations($eagerLoads);

        return $this->eagerLoadRelations($results);
    }