atk4\data\Reference_SQL_One::addTitle PHP Method

addTitle() public method

$order->hasOne('user_id', 'User')->addTitle(); This will add expression 'user' equal to ref('user_id')['name']; This method returns newly created expression field.
public addTitle ( array $defaults = [] ) : Field_SQL_Expression
$defaults array Properties
return Field_SQL_Expression
    public function addTitle($defaults = [])
    {
        if (!is_array($defaults)) {
            throw new Exception(['Argument to addTitle should be an array', 'arg' => $defaults]);
        }
        $field = str_replace('_id', '', $this->link);
        $ex = $this->owner->addExpression($field, array_merge_recursive([function ($m) {
            $mm = $m->refLink($this->link);
            return $mm->action('field', [$mm->title_field]);
        }, 'type' => null, 'ui' => ['editable' => false, 'visible' => true]], $defaults, ['read_only' => false, 'never_save' => true]));
        // Will try to execute last
        $this->owner->addHook('beforeSave', function ($m) use($field) {
            if ($m->isDirty($field) && !$m->isDirty($this->link)) {
                $mm = $m->getRef($this->link)->getModel();
                $mm->addCondition($mm->title_field, $m[$field]);
                $m[$this->link] = $mm->action('field', [$mm->id_field]);
            }
        }, null, 20);
        return $ex;
    }