Sets up the selectRelated data structure so that we only select certain
related models
(as opposed to all models, when $this->selectRelated=true).

@param array $fields

@since  1.1.0

@author Eddilbert Macharia (http://eddmash.com) <edd.cowan@gmail.com>
328515
public function addSelectRelected(array $fields)
    {
        if (is_bool($this->selectRelected)) {
            $relatedFields = [];
        } else {
            $relatedFields = $this->selectRelected;
        }

        // we need the path to follow
        //
        //array(
        //  "order" => array(
        //    "buyer" => array()
        //  )
        //  "product" => array()
        //)
        foreach ($fields as $field) {
            $names = StringHelper::split(BaseLookup::$lookupPattern, $field);
            // we use by reference so that we assigned the values back to the original array
            $d = &$relatedFields;
            foreach ($names as $name) {
                $d = &$d[$name];
                if (empty($d)) {
                    $d = [];
                }
            }
        }
        $this->selectRelected = $relatedFields;
    }
1116260
public static function setRelations($modelOne, $modelTwo, $relations): void
    {
        $name = [$modelOne, $modelTwo];
        sort($name);
        $name = implode(':', $name);
        // create record in static variable
        self::$relations[$name] = $relations;
    }
56567
public function fillKeyField(array &$fields)
    {
        $model   = $this->config->getDataModel();
        $keyName = $model->getKeyName();

        //add the primary key field, which will be uneditable, but part of the data model
        if ($this->config->getType() === 'model' && !isset($fields[$keyName])) {
            $keyField         = $this->make($keyName, array('visible' => false));
            $fields[$keyName] = $keyField->getOptions();
        }
    }
1095919
public function select(array $fields)
    {
        if (empty($fields)) {
            throw new InvalidArgumentException('The field array must not be' .
                ' empty.');
        }

        foreach ($fields as $field) {
            $validationResult = QueryValidator::validateFieldName($field);
            if ($validationResult->isFailed()) {
                throw new InvalidArgumentException('The field array for' .
                    ' building the SELECT clause contains invalid field name.' .
                    ' Validation fail reason: ' .
                    $validationResult->getFailReason());
            }
        }

        // Flipping an array will create a new associative array. The keys of
        // the new associative array make a set of distinct fields.
        $distinctFields = array_flip($fields);
        $this->selectFields = array_keys($distinctFields);
    }
598246
protected function setPostFields (array $fields)
	{
		foreach ($fields as $key => $value) $this->fields[$key] = $value;
	}
391835
public function setRelatedModels($models)
    {
        $models = $this->parseAsArray($models);
        foreach ($models as $i => $model) {
            if (is_string($model)) {
                list($model, $prop) = $this->resolveRelatedModel($model);
                $models[$i]                                 = $model;
                $this->relatedProperties[$model->objType()] = $prop;
            } elseif ($model instanceof ModelInterface) {
                if (!isset($this->relatedProperties[$model->objType()])) {
                    throw new InvalidArgumentException(
                        sprintf(
                            'The related model [%s] requires a target property',
                            get_class($model)
                        )
                    );
                }
                $models[$i] = $model;
            } else {
                throw new InvalidArgumentException(
                    sprintf(
                        'A related model must be defined as "%s"',
                        'ObjType:propertyIdent'
                    )
                );
            }
        }

        $this->relatedModels = $models;

        return $this;
    }
942930
public function setRelated($value)
    {
        $this->loadData(true);

        if (is_null($value)) {
            $value = [];
        }

        if (!is_array($value)) {
            throw new RelationException("HasMany relation can only be set with array of entities");
        }

        //Do not add items twice
        $matched = [];
        foreach ($value as $index => $item) {
            if (is_null($item)) {
                unset($value[$index]);
                continue;
            }

            $this->assertValid($item);
            if (!empty($instance = $this->matchOne($item))) {
                $matched[] = $instance;
                unset($value[$index]);
            }
        }

        $this->deleteInstances = array_diff($this->instances, $matched);
        $this->instances = $matched + $value;
    }
1140326
public function initRelation($model, $field = null)
    {
        if ($field == null) {
            $field = post(self::PARAM_FIELD);
        }

        $this->config = $this->originalConfig;
        $this->model = $model;
        $this->field = $field;

        if ($field == null) {
            return;
        }

        if (!$this->model) {
            throw new ApplicationException(Lang::get(
                'backend::lang.relation.missing_model',
                ['class'=>get_class($this->controller)]
            ));
        }

        if (!$this->model instanceof Model) {
            throw new ApplicationException(Lang::get(
                'backend::lang.model.invalid_class',
                ['model'=>get_class($this->model), 'class'=>get_class($this->controller)]
            ));
        }

        if (!$this->getConfig($field)) {
            throw new ApplicationException(Lang::get('backend::lang.relation.missing_definition', compact('field')));
        }

        if ($extraConfig = post(self::PARAM_EXTRA_CONFIG)) {
            $this->applyExtraConfig($extraConfig);
        }

        $this->alias = camel_case('relation ' . $field);
        $this->config = $this->makeConfig($this->getConfig($field), $this->requiredRelationProperties);
        $this->controller->relationExtendConfig($this->config, $this->field, $this->model);

        /*
         * Relationship details
         */
        $this->relationName = $field;
        $this->relationType = $this->model->getRelationType($field);
        $this->relationObject = $this->model->{$field}();
        $this->relationModel = $this->relationObject->getRelated();

        $this->manageId = post('manage_id');
        $this->foreignId = post('foreign_id');
        $this->readOnly = $this->getConfig('readOnly');
        $this->deferredBinding = $this->getConfig('deferredBinding') || !$this->model->exists;
        $this->viewMode = $this->evalViewMode();
        $this->manageMode = $this->evalManageMode();
        $this->manageTitle = $this->evalManageTitle();
        $this->toolbarButtons = $this->evalToolbarButtons();

        /*
         * Toolbar widget
         */
        if ($this->toolbarWidget = $this->makeToolbarWidget()) {
            $this->toolbarWidget->bindToController();
        }

        /*
         * Search widget
         */
        if ($this->searchWidget = $this->makeSearchWidget()) {
            $this->searchWidget->bindToController();
        }

        /*
         * View widget
         */
        if ($this->viewWidget = $this->makeViewWidget()) {
            $this->controller->relationExtendViewWidget($this->viewWidget, $this->field, $this->model);
            $this->viewWidget->bindToController();
        }

        /*
         * Manage widget
         */
        if ($this->manageWidget = $this->makeManageWidget()) {
            $this->controller->relationExtendManageWidget($this->manageWidget, $this->field, $this->model);
            $this->manageWidget->bindToController();
        }

        /*
         * Pivot widget
         */
        if ($this->manageMode == 'pivot' && $this->pivotWidget = $this->makePivotWidget()) {
            $this->controller->relationExtendPivotWidget($this->pivotWidget, $this->field, $this->model);
            $this->pivotWidget->bindToController();
        }
    }
1015825
public function setFields(array $fields)
    {
        foreach ($fields as $field => $value) {
            if (property_exists($this, $field)) {
                $this->{$field} = $value;
            }
        }

        if (isset($fields['_embedded'])) {
            foreach ($fields['_embedded'] as $embeddedType => $embeddedValue) {
                if (property_exists($this, $embeddedType)) {
                    $propertyObject = $this->factory->createFromPropertyName($embeddedType);
                    if ($propertyObject === null) {
                        continue;
                    }

                    if (is_array($propertyObject)) { //Property is a collection
                        foreach ($embeddedValue as $entityType => $entityFields) {
                            $entityObject = $this->factory->createFromPropertyName($entityType);
                            if ($entityObject === null) {
                                continue;
                            }

                            $entityObject->setFields($entityFields);

                            $propertyObject[] = $entityObject;
                        }
                    } else { //Property is an entity
                        $propertyObject->setFields($embeddedValue);
                    }

                    $this->{$embeddedType} = $propertyObject;
                }
            }
        }
    }
155771
public function select($fields)
    {
        foreach ($this->queries as $query) {
            $query->select($fields);
        }

        return $this;
    }
748998
public function setFields(array $fields)
    {
        $this->fields = $fields;
        $this->queryBuilder->select(implode(', ', $fields));
        return $this;
    }
174803
public function setFields(array $fields)
    {
        $this->fields = [];

        foreach ($fields as $type => $field) {
            //Remove empty and repeated fields
            $this->fields[$type] = array_unique(array_filter(explode(',', $field)));
        }

        return $this;
    }
1366068
public function setSource(array $data, $fields = null)
    {
        $this->setModel(new \atk4\data\Model(new \atk4\data\Persistence_Static($data)), $fields);
    }
1317147
protected function setFields($values)
    {
        foreach ($values[0]->fields as $name => $field) {
            $this->fields[$name] = $field;
        }
    }
1058681
public function setFromArray(array $fields)
    {
        foreach($fields as $key => $value)
        {
                $this->fields[$key] = $value;
                unset($key);
                unset($value);
        }
    }
102915
private function buildRelatedModels() {
        /** @var Base[] $relatedModels */
        $relatedModels = array();
        /** @var ModelBuilder[] $relatedBuilders */
        $relatedBuilders = array();
        $automaticallyUpdatedFields = $this->getModel()->getAutomaticallyUpdatedFields();
        foreach($this->getModel()->getDefaultRelationships() as $alias) {
            $relation = $this->getModel()->getModelsManager()->getRelationByAlias(get_class($this->getModel()), $alias);
            if($relation !== false) {
                if(in_array($relation->getFields(), $automaticallyUpdatedFields)) {
                    $referencedModel = $relation->getReferencedModel();
                    /** @var Base $relatedModel */
                    $relatedModel = new $referencedModel();
                    $relatedModels[$alias] = $relatedModel;
                    $relatedBuilders[$alias] = new ModelBuilder($this->getConfiguration(), $relatedModel->getSource());
                }
            }
        }
        $this->setRelatedModels($relatedModels);
        $this->setRelatedBuilders($relatedBuilders);
    }
430740
protected function setField($values)
    {
        foreach ($values as $name => $field) {
            $this->fields[$name] = $field;
        }
    }