Adds field as expression to owner model.
Used in aggregate strategy.

@param string $n        Field name
@param array  $defaults Properties

@throws Exception

@return Field
507206
public function addField($n, $defaults = []) : Field
    {
        if (!isset($defaults['aggregate']) && !isset($defaults['concat']) && !isset($defaults['expr'])) {
            throw new Exception([
                'Aggregate field requires "aggregate", "concat" or "expr" specified to hasMany()->addField()',
                'field'    => $n,
                'defaults' => $defaults,
            ]);
        }

        $defaults['aggregate_relation'] = $this;

        $field_n = isset($defaults['field']) ? $defaults['field'] : $n;
        $field = isset($defaults['field']) ? $defaults['field'] : null;

        if (isset($defaults['concat'])) {
            $defaults['aggregate'] = $this->owner->dsql()->groupConcat($field_n, $defaults['concat']);
            $defaults['read_only'] = false;
            $defaults['never_save'] = true;
        }

        if (isset($defaults['expr'])) {
            $cb = function () use ($defaults, $field) {
                $r = $this->refLink();

                return $r->action('field', [$r->expr(
                    $defaults['expr'],
                    isset($defaults['args']) ? $defaults['args'] : null
                ), 'alias'=>$field]);
            };
            unset($defaults['args']);
        } elseif (is_object($defaults['aggregate'])) {
            $cb = function () use ($defaults, $field) {
                return $this->refLink()->action('field', [$defaults['aggregate'], 'alias'=>$field]);
            };
        } elseif ($defaults['aggregate'] == 'count' && !isset($defaults['field'])) {
            $cb = function () use ($defaults, $field) {
                return $this->refLink()->action('count', ['alias'=>$field]);
            };
        } elseif (in_array($defaults['aggregate'], ['sum', 'avg', 'min', 'max', 'count'])) {
            $cb = function () use ($defaults, $field_n) {
                return $this->refLink()->action('fx0', [$defaults['aggregate'], $field_n]);
            };
        } else {
            $cb = function () use ($defaults, $field_n) {
                return $this->refLink()->action('fx', [$defaults['aggregate'], $field_n]);
            };
        }

        $e = $this->owner->addExpression($n, array_merge([$cb], $defaults));

        return $e;
    }
486638
public function customfield($name = null, $default = null)
    {
        $customfields = $this->getAssignedCustomfields();
        foreach ($customfields as $customfield) {
            if ($customfield->getName() === $name) {
                $value = $customfield->setModel($this)->getValue();
                return is_null($value) ? $default : $value;
            }
        }
        return $default;
    }
1135322
protected function collectValue($value, array $defaults = [])
    {
        if (!in_array($value, $defaults, true)) {
            $defaults[] = $value;
        }

        return $defaults;
    }
693926
public function addFieldRound($field, $decimals = 0, $alias = null)
    {
        $this->addField(new Func('round', $field, new Expression((int)$decimals)), $alias); return $this;
    }
949326
protected function collectValues(array $values, array $defaults = [])
    {
        foreach ($values as $value) {
            $defaults = $this->collectValue($value, $defaults);
        }

        return $defaults;
    }
939114
public function fillWithDefaults(int $num, $default = null): self
    {
        if ($num < 0) {
            throw new \InvalidArgumentException('The $num parameter can only contain non-negative values.');
        }

        $this->generatorToArray();

        $tmpArray = $this->array;

        $count = \count($tmpArray);

        while ($count < $num) {
            $tmpArray[] = $default;
            ++$count;
        }

        return static::create(
            $tmpArray,
            $this->iteratorClass,
            false
        );
    }
1248152
public function defaults($defaults) {
        $this->defaults = $defaults;

        foreach ($defaults as $k => $v) {
            if (property_exists($this, $k)) {
                $this->{$k} = $v;
            }
        }
    }
1177453
protected function _addDefaultFields()
    {
        $select = $this->clause('select');
        $this->_hasFields = true;

        /** @var \Cake\ORM\Table $repository */
        $repository = $this->getRepository();

        if (!count($select) || $this->_autoFields === true) {
            $this->_hasFields = false;
            $this->select($repository->getSchema()->columns());
            $select = $this->clause('select');
        }

        $aliased = $this->aliasFields($select, $repository->getAlias());
        $this->select($aliased, true);
    }
1150586
public function setDefaultValue($defaultValue): FieldInterface
    {
        if (!is_scalar($defaultValue) && !is_null($defaultValue)) {
            throw new \InvalidArgumentException(
                "invalid value provided for 'defaultValue'; ".
                "expecting either a scalar or null value"
            );
        }
        $this->defaultValue = $defaultValue;
        return $this;
    }
21021
public function refLink($defaults = []) : Model
    {
        $m = $this->getModel($defaults);

        $m->addCondition(
            $this->their_field ?: ($m->id_field),
            $this->referenceOurValue($m)
        );

        return $m;
    }
733607
public function add($object, $defaults = [])
    {
        if (!is_array($defaults)) {
            $defaults = ['name' => $defaults];
        }

        $defaults['join'] = $this;

        return $this->owner->add($object, $defaults);
    }
1228571
public function addDbParams($default = "", array $types)
    {
        if(array_key_exists($default, $types))
        {
            $conn = $types[$default];
        }
        else
        {
            throw new Exception('Database type ' . $default . ' is not a valid type. Please check the value in the database.php file.', 1);
        }

        $this->params = $conn;
    }
790468
public function addField($name, $attrs)
    {
        if (is_string($attrs)) {
            $attrs = ['type' => $attrs];
        }
        $attrs = array_intersect_key($attrs, $this->_fieldDefaults);
        $this->_fields[$name] = $attrs + $this->_fieldDefaults;

        return $this;
    }
188297
private function selectString($n)
    {
        if (is_null($this->pluralexpression)) {
            $this->pluralexpression = new ExpressionLanguage();
        }
        try {
            $plural = $this->pluralexpression->evaluate(
                $this->getPluralForms(),
                ['n' => $n]
            );
        } catch (\Exception $e) {
            $plural = 0;
        }

        if ($plural >= $this->pluralcount) {
            $plural = $this->pluralcount - 1;
        }

        return $plural;
    }
908721
public function setDefaults(array $defaults)
    {
        $this->defaults = array_merge($this->_global_defaults, $defaults, $this->defaults);
        return $this;
    }
495281
public function getModel($defaults = []) : Model
    {
        // set table_alias
        if (!isset($defaults['table_alias'])) {
            if (!$this->table_alias) {
                $this->table_alias = $this->link;
                $this->table_alias = preg_replace('/_'.($this->owner->id_field ?: 'id').'/', '', $this->table_alias);
                $this->table_alias = preg_replace('/([a-zA-Z])[a-zA-Z]*[^a-zA-Z]*/', '\1', $this->table_alias);
                if (isset($this->owner->table_alias)) {
                    $this->table_alias = $this->owner->table_alias.'_'.$this->table_alias;
                }
            }
            $defaults['table_alias'] = $this->table_alias;
        }

        // if model is Closure, then call it and return model
        if (is_object($this->model) && $this->model instanceof \Closure) {
            $c = $this->model;
            $c = $c($this->owner, $this, $defaults);

            return $this->addToPersistence($c, $defaults);
        }

        // if model is set, then return clone of this model
        if (is_object($this->model)) {
            $c = clone $this->model;

            return $this->addToPersistence($c, $defaults);
        }

        // last effort - try to add model
        if (is_array($this->model)) {
            $model = $this->model[0];
            $md = $this->model;
            unset($md[0]);

            $defaults = array_merge($md, $defaults);
        } else {
            $model = $this->model;
        }

        if (!$model instanceof Model) {
            $model = $this->factory($model, $defaults);
        }

        return $this->addToPersistence($model, $defaults);
    }
879302
public function aliasFields($fields, $defaultAlias = null)
    {
        $aliased = [];
        foreach ($fields as $alias => $field) {
            if (is_numeric($alias) && is_string($field)) {
                $aliased += $this->aliasField($field, $defaultAlias);
                continue;
            }
            $aliased[$alias] = $field;
        }

        return $aliased;
    }