Imports model fields in form by using form controller.

@param Model $model
@param array|string|bool $fields
185066
public function importFields($model, $fields = null)
    {
        /** @type Controller_MVCForm $c */
        $c = $this->add($this->default_controller);
        $c->importFields($model, $fields);
    }
873396
private function addModelToArraySimpleMode($models, $model)
    {
        $apply_to_models = array();
        
        if (count($models) > 0) {
            foreach ($models as $item) {
                $object = $model->find($item);
                if ($object != null) {
                    $apply_to_models[] = $object;
                }
            }
        }
        
        return $apply_to_models;
    }
942732
function setUp(Model $Model, $options = array()) {
  	if ( array_key_exists('mediaUploadFieldName', $options) ) {
  		$this->_form_field_name = $options['mediaUploadFieldName'];
  	}

  	if ( array_key_exists('mediaUploadFk', $options) ) {
  		$this->_form_fk = $options['mediaUploadFk'];
  	}

  	$Model->bindModel(
	        array('belongsTo' => array(
	                'Media' => array(
	                    'className' => 'Risto.Media'
	                )
	            )
	        )
	    );

/*
  	$Model->validate['media_file'] = array(
                'rule'    => 'uploadError',
                'message' => __('Falló al subir el archivo. Es mas grande que %s?', ini_get('upload_max_filesize')),
                'allowEmpty' => true,
                'required' => false,
            );
            */
  	// Inside a model class
	$Model->validator()->add('uploadError', false, array(
	    'rule' => 'uploadError',
	    'message' => __('Falló al subir el archivo. Es mas grande que %s?', ini_get('upload_max_filesize')),
	));


  }
838533
public function setFields()
    {
        foreach ($this->field_associations as $form_field => $model_field) {
            $this->form->set($form_field, $model_field->get());
        }
    }
1272533
public function importFields($model, $fields = null)
    {
        $this->model = $model;
        $this->form = $this->owner;

        if ($fields === false) {
            return;
        }

        if (!$fields) {
            if ($model->only_fields) {
                $fields = $model->only_fields;
            } else {
                $fields = [];
                // get all field-elements
                foreach ($model->elements as $field => $f_object) {
                    if ($f_object instanceof \atk4\data\Field
                        && $f_object->isEditable()
                        && !$f_object->isHidden()
                    ) {
                        $fields[] = $field;
                    }
                }
            }
        }

        if (!is_array($fields)) {
            $fields = [$fields];
        }

        // import fields one by one
        foreach ($fields as $field) {
            $this->importField($field);
        }

        // set update hook
        if (!$this->_hook_set) {
            $this->owner->addHook('update', array($this, 'update'));
            $model->addHook('afterLoad', array($this, 'setFields'));
            $this->_hook_set = true;
        }

        return $this;
    }
700689
public function setBelongsToData(Model $model) {
        $typeAhead = array();

        foreach ($model->belongsTo as $alias => $assoc) {
            $object = Admin::introspectModel($assoc['className']);
            $count = $this->getRecordCount($object);

            // Add to type ahead if too many records
            if (!$count || $count > $object->admin['associationLimit']) {
                $class = $assoc['className'];

                if (mb_strpos($class, '.') === false) {
                    $class = Configure::read('Admin.coreName') . '.' . $class;
                }

                // Get the foreign key from the child -> parent
                $foreignKey = null;

                foreach ($object->belongsTo as $childAssoc) {
                    if ($childAssoc['className'] === $model->name) {
                        $foreignKey = $childAssoc['foreignKey'];
                        break;
                    }
                }

                $typeAhead[$assoc['foreignKey']] = array(
                    'alias' => $alias,
                    'model' => $class,
                    'foreignKey' => $foreignKey
                );

            } else {
                $variable = Inflector::variable(Inflector::pluralize(preg_replace('/(?:_id)$/', '', $assoc['foreignKey'])));

                $this->Controller->set($variable, $this->getRecordList($object));
            }
        }

        $this->Controller->set('typeAhead', $typeAhead);
    }
1275661
public function setup(Model $Model, $config = array()) {
		if (is_string($config)) {
			$config = array($config);
		}

		$_config = array();

		foreach ((array) $config as $field => $options) {
			if (is_int($field) && is_string($options)) {
				$field = $options;
				$options = $this->_options;
			}
			$_config[$field] = array_merge($this->_options, $options);
		}

		$this->settings[$Model->alias] = $_config;
	}
1201351
public function createModel($model)
    {
        $schema = new Schema();

        $tableDef = $schema->createTable($model->getMeta()->getDbTable());
        // this assumes fields set_from_name has been invoked
        $primaryKeyFields = [];
        $unique_fields = [];
        $indexes = [];
        /** @var $field Field */
        /** @var $field ForeignKey */
        foreach ($model->getMeta()->localFields as $fname => $field) {
            $colName = $field->getColumnName();
            $type = $field->dbType($this->connection);
            // if we don't have a type skip
            if (empty($type)) {
                continue;
            }
            if ($field->primaryKey) {
                $primaryKeyFields[] = $model->getMeta()->primaryKey->getColumnName();
            } elseif ($field->isUnique()) {
                $unique_fields[] = $colName;
            } elseif ($field->dbIndex) {
                $indexes[] = $colName;
            }
            $tableDef->addColumn($colName, $type, $this->getDoctrineColumnOptions($field));
            if ($field->isRelation && $field->relation && $field->dbConstraint) {
                $relField = $field->getRelatedField();
                $tableDef->addForeignKeyConstraint(
                    $relField->scopeModel->getMeta()->getDbTable(),
                    [$field->getColumnName()],
                    [$relField->getColumnName()]
                );
            }
        }
        // create the primary key
        $tableDef->setPrimaryKey($primaryKeyFields);
        // add index constraint
        if (!empty($indexes)) {
            $tableDef->addIndex($indexes);
        }
        // add unique constraint
        if (!empty($unique_fields)) {
            $tableDef->addUniqueIndex($unique_fields);
        }
        $this->createTable($tableDef);
        // many to many
        /** @var $relationField ManyToManyField */
        foreach ($model->getMeta()->localManyToMany as $name => $relationField) {
            if ($relationField->manyToMany && $relationField->relation->through->getMeta()->autoCreated) {
                $this->createModel($relationField->relation->through);
            }
        }
    }
664757
protected function prepareModel(Model $model)
    {
        if (is_callable($this->prepareModel)) {
            call_user_func($this->prepareModel, $model);
        } else {
            foreach ($model->attributes() as $attribute) {
                $value = Yii::$app->settings->get($this->getSection($model), $attribute);

                if (!is_null($value)) {
                    $model->{$attribute} = $value;
                }
            }
        }
    }
409573
public function setup(Model $model, $settings = array()) {
        $this->settings[$model->alias] = Hash::merge($this->_defaults, $settings);
    }
164674
protected function body(Model $model)
    {
        $body = '';

        foreach ($model->getTraits() as $trait) {
            $body .= $this->class->mixin($trait);
        }

        $excludedConstants = [];

        if ($model->hasCustomCreatedAtField()) {
            $body .= $this->class->constant('CREATED_AT', $model->getCreatedAtField());
            $excludedConstants[] = $model->getCreatedAtField();
        }

        if ($model->hasCustomUpdatedAtField()) {
            $body .= $this->class->constant('UPDATED_AT', $model->getUpdatedAtField());
            $excludedConstants[] = $model->getUpdatedAtField();
        }

        if ($model->hasCustomDeletedAtField()) {
            $body .= $this->class->constant('DELETED_AT', $model->getDeletedAtField());
            $excludedConstants[] = $model->getDeletedAtField();
        }

        if ($model->usesPropertyConstants()) {
            // Take all properties and exclude already added constants with timestamps.
            $properties = array_keys($model->getProperties());
            $properties = array_diff($properties, $excludedConstants);

            foreach ($properties as $property) {
                $body .= $this->class->constant(strtoupper($property), $property);
            }
        }

        $body = trim($body, "\n");
        // Separate constants from fields only if there are constants.
        if (! empty($body)) {
            $body .= "\n";
        }

        // Append connection name when required
        if ($model->shouldShowConnection()) {
            $body .= $this->class->field('connection', $model->getConnectionName());
        }

        // When table is not plural, append the table name
        if ($model->needsTableName()) {
            $body .= $this->class->field('table', $model->getTableForQuery());
        }

        if ($model->hasCustomPrimaryKey()) {
            $body .= $this->class->field('primaryKey', $model->getPrimaryKey());
        }

        if ($model->doesNotAutoincrement()) {
            $body .= $this->class->field('incrementing', false, ['visibility' => 'public']);
        }

        if ($model->hasCustomPerPage()) {
            $body .= $this->class->field('perPage', $model->getPerPage());
        }

        if (! $model->usesTimestamps()) {
            $body .= $this->class->field('timestamps', false, ['visibility' => 'public']);
        }

        if ($model->hasCustomDateFormat()) {
            $body .= $this->class->field('dateFormat', $model->getDateFormat());
        }

        if ($model->doesNotUseSnakeAttributes()) {
            $body .= $this->class->field('snakeAttributes', false, ['visibility' => 'public static']);
        }

        if ($model->hasCasts()) {
            $body .= $this->class->field('casts', $model->getCasts(), ['before' => "\n"]);
        }

        if ($model->hasDates()) {
            $body .= $this->class->field('dates', $model->getDates(), ['before' => "\n"]);
        }

        if ($model->hasHidden() && $model->doesNotUseBaseFiles()) {
            $body .= $this->class->field('hidden', $model->getHidden(), ['before' => "\n"]);
        }

        if ($model->hasFillable() && $model->doesNotUseBaseFiles()) {
            $body .= $this->class->field('fillable', $model->getFillable(), ['before' => "\n"]);
        }

        if ($model->hasHints() && $model->usesHints()) {
            $body .= $this->class->field('hints', $model->getHints(), ['before' => "\n"]);
        }

        foreach ($model->getMutations() as $mutation) {
            $body .= $this->class->method($mutation->name(), $mutation->body(), ['before' => "\n"]);
        }

        foreach ($model->getRelations() as $constraint) {
            $body .= $this->class->method($constraint->name(), $constraint->body(), ['before' => "\n"]);
        }

        // Make sure there not undesired line breaks
        $body = trim($body, "\n");

        return $body;
    }
764250
public function creating(Model $model)
    {
        $count = method_exists($model, 'getCounter') ? $model->getCounter() : ($model::count() ?? 0);

        $column = method_exists($model, 'getReferenceColumn') ? $model->getReferenceColumn() : 'reference';

        $prefix = method_exists($model, 'referencePrefix') ? $model->referencePrefix() : config('document.prefix');

        if (Schema::hasColumn($model->getTable(), $column) && is_null($model->$column)) {
            $model->$column = generate_reference($prefix, $count);
        }
    }
1368593
public function add(FormField $field)
    {
        $segments = $this->getSegments($field->getName());

        $target = &$this->fields;
        while ($segments) {
            if (!\is_array($target)) {
                $target = [];
            }
            $path = array_shift($segments);
            if ('' === $path) {
                $target = &$target[];
            } else {
                $target = &$target[$path];
            }
        }
        $target = $field;
    }
1132842
protected function _cleanupOldFiles(Model $model, array $fields) {
        $columns = $this->_columns[$model->alias];
        $data = $this->_doFind($model, array($model->alias . '.' . $model->primaryKey => $model->id));

        if (empty($data[$model->alias])) {
            return;
        }

        foreach ($fields as $column => $value) {
            if (empty($data[$model->alias][$column])) {
                continue;
            }

            if (!empty($this->settings[$model->alias][$columns[$column]])) {
                $attachment = $this->_settingsCallback($model, $this->settings[$model->alias][$columns[$column]]);

                if (!$attachment['cleanup']) {
                    continue;
                }
            }

            // Delete if previous value doesn't match new value
            $previous = $data[$model->alias][$column];

            if ($previous !== $value) {
                $this->_deleteFile($model, $columns[$column], $previous, $column);
            }
        }
    }
171822
public function getInstanceValueForFields(Model $modelInstance, $fields)
    {
        $values = [];
        /** @var $field Field */
        foreach ($fields as $field) {
            $val = $modelInstance->{$field->getAttrName()};
            if (!$val) {
                continue;
            }
            $values[] = $val;
        }

        return $values;
    }
1029677
public function fillModel(&$model, $input)
    {
        $model->{$this->getOption('field_name')} = is_null($input) || $input === '' ? null : $this->parseNumber($input);
    }
1313198
public function setup(Model $model, $settings = array()) {
        foreach ($settings as $field => $options) {
            $filters = array();

            foreach ($options as $key => $value) {
                if (!isset($this->_defaults[$key])) {
                    continue;
                }

                if (is_array($value)) {
                    if (($key === 'strip' || $key === 'paranoid') && empty($value['allowed'])) {
                        $value = array('allowed' => $value);
                    }

                    if (empty($value['filter'])) {
                        $value['filter'] = true;
                    }
                } else {
                    $value = array('filter' => (bool) $value);
                }

                $filters[$key] = Hash::merge($this->_defaults[$key], $value);
            }

            $this->settings[$model->alias][$field] = $filters;
        }
    }