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;
}