Gather the keys from an array of related models.

@param  array  $models
@return array
185673
protected function getEagerModelKeys(array $models)
    {
        $keys = [];

        // First we need to gather all of the keys from the parent models 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 ($models as $model) {
            if (is_array($this->foreignKey)) { //Check for multi-columns relationship
                $keys[] = array_map(function ($k) use ($model) {
                    return $model->{$k};
                }, $this->foreignKey);
            } else {
                if (! is_null($value = $model->{$this->foreignKey})) {
                    $keys[] = $value;
                }
            }
        }

        // If there are no keys that were not null we will just return an array with either
        // null or 0 in (depending on if incrementing keys are in use) so the query wont
        // fail plus returns zero results, which should be what the developer expects.
        if (count($keys) === 0) {
            return [$this->relationHasIncrementingId() ? 0 : null];
        }

        sort($keys);

        return array_values(array_unique($keys, SORT_REGULAR));
    }
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;
	}
563140
public static function fillPropertyGroups(&$models)
    {

        if (count($models) === 0) {
            return [];
        }
        /** @var \yii\db\ActiveRecord|\DevGroup\DataStructure\traits\PropertiesTrait|\DevGroup\TagDependencyHelper\TagDependencyTrait $firstModel */
        $firstModel = reset($models);
        //        if ($firstModel->propertyGroupIds !== null) {
        //            // assume that we have already got them
        //            return;
        //        }

        $tags = [
            PropertyGroup::commonTag(),
        ];
        foreach ($models as $modelForTag) {
            $tags[] = $modelForTag->objectTag();
        }

        $binding_rows = Yii::$app->cache->lazy(
            function () use ($firstModel, $models) {
                $query = new Query();

                $query->select(['model_id', 'property_group_id'])->from(
                    $firstModel::bindedPropertyGroupsTable()
                )->where(PropertiesHelper::getInCondition($models))->orderBy(['sort_order' => SORT_ASC]);

                return $query->all($firstModel::getDb());

            },
            static::generateCacheKey($models, 'property_groups_ids'),
            0,
            $tags
        );

        array_walk(
            $binding_rows,
            function (&$item) {
                $item['model_id'] = (int) $item['model_id'];
                $item['property_group_id'] = (int) $item['property_group_id'];
            }
        );

        foreach ($models as &$model) {
            $id = $model->id;
            $model->propertyGroupIds = array_reduce(
                $binding_rows,
                function ($carry, $item) use ($id) {
                    if ($item['model_id'] === $id) {
                        $carry[] = (int) $item['property_group_id'];
                    }
                    return $carry;
                },
                []
            );
        }

    }
1168775
protected function collectForeignKeys()
    {
        $keys = [];

        foreach ($this->info->relations as $relation) {

            if ( ! in_array(
                $relation->type,
                [ RelationType::BELONGS_TO, RelationType::MORPH_TO, RelationType::BELONGS_TO_THROUGH ]
            )) {
                continue;
            }

            $keys = array_merge($keys, $relation->foreign_keys);
        }

        return $keys;
    }
100957
public function setModels(array $models)
    {
        $this->models = array();
        foreach ($models as $modelName => $model)
        {
            if (!isset($model['hidden']) || $model['hidden'] == FALSE)
            {
                $this->models[$modelName] = $modelName;
            }
        }
    }
1335149
public function buildRelation(array $models, array $results) {
        foreach ($models as &$model) {
            if ($model instanceof Model) {
                $model->setRelation($this->getKey(), $this->matchRelation($model, $results));
                continue;
            }
            $model[$this->getKey()] = $this->matchRelation($model, $results);
        }
        unset($model);
        return $models;
    }
500842
public function getRelationData(Model $model)
    {
        $relations = $this->getRelations($model);
        if (empty($relations)) {
            return [];
        }
        $return = [];

        foreach ($this->properties as $name => $details) {
            try {
                $related = $model->{$name};
                if (!is_object($model->$name())) {
                    continue;
                }
            } catch (Exception $ex) {
                continue;
            }

            if (is_null($related)) {
                $related = $model->$name()->getModel();
                array_set($return, $name, [get_class($related) => $related->getFillable()]);
            }
            if (!is_null($related) && method_exists($related, 'getAttributes')) {
                array_set($return, $name, [get_class($related) => $related->getAttributes()]);
            }
        }
        return $return;
    }
46450
protected function hydrateIntermediateRelations(array $models)
    {
        $intermediateTables = $this->intermediateTables;

        ksort($intermediateTables);

        foreach ($intermediateTables as $accessor => $intermediateTable) {
            $prefix = $this->prefix($accessor);

            if (Str::contains($accessor, '.')) {
                [$path, $key] = preg_split('/\.(?=[^.]*$)/', $accessor);
            } else {
                [$path, $key] = [null, $accessor];
            }

            foreach ($models as $model) {
                $relation = $this->intermediateRelation($model, $intermediateTable, $prefix);

                data_get($model, $path)->setRelation($key, $relation);
            }
        }
    }
417890
protected function makeRelations(array $models)
    {
        foreach ($models as $model) {
            $localTable  = $model->getTable();
            $foreignKeys = $localTable->getTableForeignKeys();

            foreach ($foreignKeys as $foreignKey) {
                if (!isset($models[$foreignKey->getForeignTableName()])) {
                    continue;
                }

                $foreignModel = $models[$foreignKey->getForeignTableName()];
                $foreignTable = $foreignModel->getTable();

                $localColumns   = $foreignKey->getLocalColumns();
                $foreignColumns = $foreignKey->getForeignColumns();

                $localIndex   = $localTable->getTableIndexByColumnsName($localColumns);
                $foreignIndex = $foreignTable->getTableIndexByColumnsName($foreignColumns);

                $localTableClass   = str_singular($model->getFullClass());
                $foreignTableClass = str_singular($foreignModel->getFullClass());

                $localColumn   = reset($localColumns);
                $foreignColumn = reset($foreignColumns);

                // Guess foreign method name
                $localMethodName   = str_replace(['_id'], '', snake_case($localColumn));
                $foreignMethodName = $model->getClass();

                // Try to find out connection type
                if ($localIndex !== false && $foreignIndex !== false && $localIndex->isUnique() && $foreignIndex->isUnique()) {
                    // One To One
                    $localTable->addHasOneRelation($foreignTableClass, $localMethodName, $foreignColumn, $localColumn);
                    $foreignTable->addBelongsToRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                } elseif ($foreignIndex !== false && $foreignIndex->isUnique()) {
                    // Many To One
                    $localTable->addBelongsToRelation($foreignTableClass, $localMethodName, $localColumn, $foreignColumn);
                    $foreignTable->addHasManyRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                } elseif ($localIndex !== false && $localIndex->isUnique()) {
                    // One To Many
                    $localTable->addHasManyRelation($foreignTableClass, $localMethodName, $foreignColumn, $localColumn);
                    $foreignTable->addBelongsToRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                } else {
                    // Many To Many without pivot table
                    $localTable->addHasManyRelation($foreignTableClass, $localMethodName, $foreignColumn, $localColumn);
                    $foreignTable->addHasManyRelation($localTableClass, $foreignMethodName, $localColumn, $foreignColumn);
                }
            }
        }
    }
1019227
protected function loadWiths(array $models) {
        if ($models && $this->_with) {
            foreach ($this->_with as $w) {
                /** @var BatchLoader $loader */
                $loader = $w['loader'];
                $subWith = $w['with'];
                $loader->batchLoadFor($models, $subWith);
            }
            $this->_with = [];
        }
        return $models;
    }
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;
	}
760546
private function createTags(Collection $models): array
    {
        $tags = [];

        /** @var Model $model */
        foreach ($models as $model) {
            $tags[] = static::createTagFromModel($model);
        }

        return $tags;
    }
1119654
public function flatKeys(array $keys): array
    {
        $this->search = $keys;
        return $this->flatCollection($this->collection->toArray());
    }
105772
public function getExtraRelations($models) {
        if (!$this->relationsParser){
            return [];
        }
        $relationsLeft = $this->relationsParser->getRelationsToBeSelectedSeparately();
        foreach ($relationsLeft as $path => $details){
            $this->relationsParser->getChildrenForModels($models, $path, $details, $this->fields);
        }

    }
1229730
public function addEagerConstraints(array $models)
    {
        // We'll grab the primary key name of the related models since it could be set to
        // a non-standard name and not "id". We will then construct the constraint for
        // our eagerly loading query so it returns the proper models from execution.
        $key = $this->related->getTable().'.'.$this->ownerKey;

        $this->query->whereIn($key, $this->getEagerModelKeys($models));
    }
576556
public function addEagerConstraints(array $models)
    {
        $keys = $this->getKeys($models, $this->getFirstForeignKeyName());

        $this->query->whereIn($this->getQualifiedFirstLocalKeyName(), $keys);
    }
874284
protected function getEagerModelKeys(array $models)
    {
        $keys = array();

        foreach ($models as $model) {
            if (! is_null($value = $model->{$this->foreignKey})) {
                $keys[] = $value;
            }
        }

        if (count($keys) == 0) {
            return array(0);
        }

        return array_values(array_unique($keys));
    }