Field::setDefault($value)

Version 4

Set value if field has no value

Sets given value to field if it has no value. Return TRUE if value was changed.

Return value

Bool

Arguments

$value
Value to set

Full documentation

Set field 'Status' to value 'New' if it has no value:

$Order->status->setDefault('New');

The example above is a shorthand for:

if ( $Order->status->isEmpty() ){
    $Order->status->setValue('New');
}

Show message if default value was set (ie. field was empty).

if ( $Order->status->setDefault('New') ){
    $this->get('feedback','Status was changed to default value: New');
}