atk4\data\Reference::getModel PHP Method

getModel() public method

Returns destination model that is linked through this reference. Will apply necessary conditions.
public getModel ( array $defaults = [] ) : Model
$defaults array Properties
return Model
    public function getModel($defaults = [])
    {
        // set table_alias
        if (!isset($defaults['table_alias'])) {
            if (!$this->table_alias) {
                $this->table_alias = $this->link;
                $this->table_alias = preg_replace('/_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);
            if (!$c->persistence && $this->owner->persistence) {
                $c = $this->owner->persistence->add($c, $defaults);
            }
            return $c;
        }
        // if model is set, then return clone of this model
        if (is_object($this->model)) {
            $c = clone $this->model;
            if (!$this->model->persistence && $this->owner->persistence) {
                $this->owner->persistence->add($c, $defaults);
            }
            return $c;
        }
        // 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;
        }
        $p = $this->owner->persistence;
        return $p->add($p->normalizeClassName($model, 'Model'), $defaults);
    }