public function addTitle($defaults = []) : Field_SQL_Expression
    {
        if (!is_array($defaults)) {
            throw new Exception([
                'Argument to addTitle should be an array',
                'arg' => $defaults,
            ]);
        }

        $field = isset($defaults['field'])
                    ? $defaults['field']
                    : preg_replace('/_'.($this->owner->id_field ?: 'id').'$/i', '', $this->link);

        if ($this->owner->hasField($field)) {
            throw new Exception([
                'Field with this name already exists. Please set title field name manually addTitle([\'field\'=>\'field_name\'])',
                'field' => $field,
            ]);
        }

        $ex = $this->owner->addExpression($field, array_merge_recursive(
            [
                function ($m) {
                    $mm = $m->refLink($this->link);

                    return $mm->action('field', [$mm->title_field])->reset('order');
                },
                'type' => null,
                'ui'   => ['editable' => false, 'visible' => true],
            ],
            $defaults,
            [
                // to be able to change title field, but not save it
                // afterSave hook will take care of the rest
                'read_only'  => false,
                'never_save' => true,
            ]
        ));

        // Will try to execute last
        $this->owner->addHook('beforeSave', function ($m) use ($field) {
            // if title field is changed, but reference ID field (our_field)
            // is not changed, then update reference ID field value
            if ($m->isDirty($field) && !$m->isDirty($this->our_field)) {
                $mm = $this->getModel();

                $mm->addCondition($mm->title_field, $m[$field]);
                $m[$this->our_field] = $mm->action('field', [$mm->id_field]);
            }
        }, null, 20);

        // Set ID field as not visible in grid by default
        if (!array_key_exists('visible', $this->owner->getField($this->our_field)->ui)) {
            $this->owner->getField($this->our_field)->ui['visible'] = false;
        }

        return $ex;
    }
public function addTitle($defaults = []) : Field_SQL_Expression
    {
        if (!is_array($defaults)) {
            throw new Exception([
                'Argument to addTitle should be an array',
                'arg' => $defaults,
            ]);
        }

        $field = isset($defaults['field'])
                    ? $defaults['field']
                    : preg_replace('/_'.($this->owner->id_field ?: 'id').'$/i', '', $this->link);

        if ($this->owner->hasField($field)) {
            throw new Exception([
                'Field with this name already exists. Please set title field name manually addTitle([\'field\'=>\'field_name\'])',
                'field' => $field,
            ]);
        }

        $ex = $this->owner->addExpression($field, array_merge_recursive(
            [
                function ($m) {
                    $mm = $m->refLink($this->link);

                    return $mm->action('field', [$mm->title_field])->reset('order');
                },
                'type' => null,
                'ui'   => ['editable' => false, 'visible' => true],
            ],
            $defaults,
            [
                // to be able to change title field, but not save it
                // afterSave hook will take care of the rest
                'read_only'  => false,
                'never_save' => true,
            ]
        ));

        // Will try to execute last
        $this->owner->addHook('beforeSave', function ($m) use ($field) {
            // if title field is changed, but reference ID field (our_field)
            // is not changed, then update reference ID field value
            if ($m->isDirty($field) && !$m->isDirty($this->our_field)) {
                $mm = $this->getModel();

                $mm->addCondition($mm->title_field, $m[$field]);
                $m[$this->our_field] = $mm->action('field', [$mm->id_field]);
            }
        }, null, 20);

        // Set ID field as not visible in grid by default
        if (!array_key_exists('visible', $this->owner->getField($this->our_field)->ui)) {
            $this->owner->getField($this->our_field)->ui['visible'] = false;
        }

        return $ex;
    }
public function addTitle ( $ defaults = [ ] ) : Field_SQL_Expression { if ( ! is_array ( $ defaults ) ) { throw new Exception ( [ 'Argument to addTitle should be an array' , 'arg' => $ defaults , ] ) ; } $ field = isset ( $ defaults [ 'field' ] ) ? $ defaults [ 'field' ] : preg_replace ( '/_' . ( $ this -> owner -> id_field ? : 'id' ) . '$/i' , '' , $ this -> link ) ; if ( $ this -> owner -> hasField ( $ field ) ) { throw new Exception ( [ 'Field with this name already exists. Please set title field name manually addTitle([\'field\'=>\'field_name\'])' , 'field' => $ field , ] ) ; } $ ex = $ this -> owner -> addExpression ( $ field , array_merge_recursive ( [ function ( $ m ) { $ mm = $ m -> refLink ( $ this -> link ) ; return $ mm -> action ( 'field' , [ $ mm -> title_field ] ) -> reset ( 'order' ) ; } , 'type' => null , 'ui' => [ 'editable' => false , 'visible' => true ] , ] , $ defaults , [ // to be able to change title field, but not save it // afterSave hook will take care of the rest 'read_only' => false , 'never_save' => true , ] ) ) ; // Will try to execute last $ this -> owner -> addHook ( 'beforeSave' , function ( $ m ) use ( $ field ) { // if title field is changed, but reference ID field (our_field) // is not changed, then update reference ID field value if ( $ m -> isDirty ( $ field ) && ! $ m -> isDirty ( $ this -> our_field ) ) { $ mm = $ this -> getModel ( ) ; $ mm -> addCondition ( $ mm -> title_field , $ m [ $ field ] ) ; $ m [ $ this -> our_field ] = $ mm -> action ( 'field' , [ $ mm -> id_field ] ) ; } } , null , 20 ) ; // Set ID field as not visible in grid by default if ( ! array_key_exists ( 'visible' , $ this -> owner -> getField ( $ this -> our_field ) -> ui ) ) { $ this -> owner -> getField ( $ this -> our_field ) -> ui [ 'visible' ] = false ; } return $ ex ; }
Add a title of related entity as expression to our field.

$order->hasOne('user_id', 'User')->addTitle();

This will add expression 'user' equal to ref('user_id')['name'];

This method returns newly created expression field.

@param array $defaults Properties

@return Field_SQL_Expression
Add a title of related entity as expression to our field .