Setting default for a hasOne Reference (in a Model)


Hi there,

In my UI there is forms to create new Groups. The Group model has a hasOne Relation:

$this->hasOne('group_origin_id', new GroupOrigin());

And I would like to set the default for this field so its pre-selected in the form dropdown.
The atk4\data\Field class has a default property, but I am having a hard time setting it, this does not work:

$this->hasOne('group_origin_id', new GroupOrigin());
$this->group_origin_id->default = 1;

It creates the following PHP warning:

Warning: Creating default object from empty value in /var/www/html/TourManagement/src/Group.php on line 24

It seems as “default” is used in a keyword in PHP, it does not work - or am I wrong?
Any other way to get the wanted behaviour? To set the default in the model would be the most sensible place, as any form will automatically use the default.

Best regards


The format is like this:

$this->getElement('group_origin_id')->default = 1;

or you can use

$this->hasOne('group_origin_id', [new GroupOrigin(), 'default'=>1]);


Works like a charm, thanks!