@param Model $model
@param Field $field

@since  1.1.0

@author Eddilbert Macharia (http://eddmash.com) <edd.cowan@gmail.com>
421989
public function handleM2MField(Model $model, Field $field)
    {
        $vals = [];
        /** @var $field ManyToManyField */
        if ($field->relation->through->getMeta()->autoCreated) {
            //todo handle natuaral keys
            $m2mValues = function (Model $model) {
                return $model->getPkValue();
            };

            foreach ($model->{$field->getName()}->all() as $item) {
                $vals[] = $m2mValues($item);
            }
        }
        $this->_fields[$field->getName()] = $vals;
    }
700321
protected function handle(EloquentModel $model, $field, $value) : EloquentModel
  {
    return $model::where($field, 'like', '%' . $value . '%')->first() ?? $model;
  }
665549
protected function assertField($field)
    {
        if (!$this->hasField($field)) {
            throw new ModelException(sprintf('Unknown field, field "%s" not found in model "%s"', $field, $this->entity));
        }
    }
1312742
protected function updateField(Entity $entity, $field) {
		if ($entity->dirty($field)) {
			return;
		}

		$entity->set($field, $this->getUserId());
	}
624703
public function addField(Field $field)
    {
        $this->fields->set($field->getColumn(), $field);
        
        return $this;
    }
489071
public function contributeToClass($fieldName, $modelObject)
    {
        if (!StringHelper::isValidVariableName($fieldName)) {
            throw new FieldError(
                sprintf(
                    ' "%s" is not a valid field name on the model "%s" .',
                    $fieldName,
                    $modelObject->getFullClassName()
                )
            );
        }

        $this->scopeModel = $modelObject;
        $this->setFromName($fieldName);
        $this->scopeModel->getMeta()->addField($this);
        $this->scopeModel->_valueCache[$this->getAttrName()] = $this->getDefault();
    }
171581
protected function rankFieldWeightLabel(Model $model, $field)
    {
        $label = $this->option($model, "rank.fields.$field");

        return collect(['A', 'B', 'C', 'D'])
            ->contains($label) ? $label : '';
    }
1219557
protected function prepareImageRules(Model $model, $property, $fieldName, $field)
    {
        $getter = 'get'.studly_case($property);
        $setter = 'set'.studly_case($property);

        if (! method_exists($model, $getter) || ! method_exists($model, $setter)) {
            throw new \Exception('Unexpected missing method on model '.get_class($model));
        }

        // Calculate minimum allowed image size.
        list($minWidth, $minHeight) = $model->getMinAllowedImageSize($field);
        // Set dimensions on the model.
        $model->setMinDimensions($fieldName, ['width' => $minWidth, 'height' => $minHeight]);

        $modelRules = $model->$getter();

        $restrictions = [];
        if ($minWidth) {
            $restrictions[] = 'min_width='.$minWidth;
        }
        if ($minHeight) {
            $restrictions[] = 'min_height='.$minHeight;
        }

        // Find actual field name
        // Try to get the field config for the image.
        foreach ($model->getFormFields() as $formFieldName => $config) {
            if ($config['type'] == 'gallery' && isset($config['image_field']) && $config['image_field'] == $fieldName) {
                $fieldName = $formFieldName;
                break;
            }
        }

        // Prepare model rules.
        if (isset($modelRules[$fieldName])) {
            // We need to get widget config
            $rules = explode('|', $modelRules[$fieldName]);

            $fieldConfig = $model->getFormField($fieldName);
            if ($fieldConfig['type'] == 'gallery') {
                // we need to remove the required attribute as we validate elsewhere
                if (($requiredKey = array_search('required', $rules)) !== false) {
                    $rules[$requiredKey] = 'gallery_required:'.get_class($model);
                    $this->requiredImages[$fieldName] = $fieldName;
                }
            }

            if (strstr($modelRules[$fieldName], 'max:') === false) {
                if ($maxImageSize = config('ignicms.images.max_upload_size')) {
                    $rules[] = 'max:'.$maxImageSize;
                    $modelRules[$fieldName] = 'dimensions:'.implode(',', $restrictions).
                        '|max:'.$maxImageSize;
                }
            }
            // Check to see for dimensions rule and remove it.
            if (strstr($modelRules[$fieldName], 'dimensions:') !== false) {
                foreach ($rules as $key => $rule) {
                    if (strstr($rule, 'dimensions:') !== false) {
                        unset($rules[$key]);
                    }
                }
            }
            $rules[] = 'dimensions:'.implode(',', $restrictions);
            $modelRules[$fieldName] = implode('|', $rules);
        } else {
            $modelRules[$fieldName] = '';
            if ($maxImageSize = config('ignicms.images.max_upload_size')) {
                $modelRules[$fieldName] .= 'max:'.$maxImageSize.'|';
            }
            $modelRules[$fieldName] .= 'dimensions:'.implode(',', $restrictions);
        }

        $model->$setter($modelRules);
    }
350916
public function apply(Builder $builder, Model $model) {
    $subclassTypes = array_keys($model->getSingleTableTypeMap());

    if (!empty($subclassTypes) ) {
      $builder->whereIn($model->getQualifiedSingleTableTypeColumn(), $subclassTypes);
    }
  }
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;
                }
            }
        }
    }
341081
private function setByTypeFileField(CsvField $field) : void
    {
        if (! in_array($field->getType(), ['files', 'images'])) {
            return;
        }

        $this->setAssociation(
            'hasMany',
            static::generateAssociationName(FileUpload::FILE_STORAGE_TABLE_NAME, $field->getName()),
            [
                'className' => FileUpload::FILE_STORAGE_TABLE_NAME,
                'foreignKey' => 'foreign_key',
                'conditions' => ['model' => $this->getTable(), 'model_field' => $field->getName()]
            ]
        );
    }
360912
public function setup(Model $model, $settings = array()) {
        if ($settings) {
            foreach ($settings as $field => $options) {
                $this->settings[$model->alias][$field] = (array) $options + array('required' => true);
            }
        }
    }
525972
public function validateModelObject(ModelObject $model)
    {
        $fields = $model->getPersistentFields();

        foreach ( $fields as $fieldName ) {

            $value         = $model->$fieldName;
            $fieldResolved = get_class($model).'.'.$fieldName;

            $this->rejectIfInvalid($fieldResolved,
                                   'field',
                                   $model->getFieldTitle($fieldName),
                                   $value,
                                   new ValidationExpression($model->getValidation($fieldName)));
        }
    }
579147
public function deleting(Model $model)
    {
        $model->deleted_by = $this->guard->check() ? $this->guard->user()->getId() : null;
    }
185066
public function importFields($model, $fields = null)
    {
        /** @type Controller_MVCForm $c */
        $c = $this->add($this->default_controller);
        $c->importFields($model, $fields);
    }
43885
public function removeField(Field $field)
    {
        if ($this->hasField($field)) {
            unset($this->fields[$field->getName()]);

            return true;
        }

        return false;
    }
1146008
public function setExtraModelValues(array $fields, &$model)
    {
        //make sure the relationships are loaded
        foreach ($fields as $name => $field) {
            if ($field->getOption('relationship')) {
                $this->setModelRelationship($model, $field);
            }

            //if this is a setter field, unset it
            if ($field->getOption('setter')) {
                $model->__unset($name);
            }
        }
    }