silverstripe-framework/docs/en/02_Developer_Guides/00_Model/How_Tos/Dynamic_Default_Fields.md
David Alexander 903379bde2 DOCS 3.2 : fixing api: links now that api: tag parser working
fixed a couple of external links

fixed a docs link
2016-02-17 18:02:38 -07:00

37 lines
1.1 KiB
Markdown

# Dynamic Default Values
The [api:DataObject::$defaults] array allows you to specify simple static values to be the default values when a
record is created, but in many situations default values need to be dynamically calculated. In order to do this, the
[api:DataObject::populateDefaults()] method will need to be overloaded.
This method is called whenever a new record is instantiated, and you must be sure to call the method on the parent
object!
A simple example is to set a field to the current date and time:
:::php
/**
* Sets the Date field to the current date.
*/
public function populateDefaults() {
$this->Date = date('Y-m-d');
parent::populateDefaults();
}
It's also possible to get the data from any other source, or another object, just by using the usual data retrieval
methods. For example:
:::php
/**
* This method combines the Title of the parent object with the Title of this
* object in the FullTitle field.
*/
public function populateDefaults() {
if($parent = $this->Parent()) {
$this->FullTitle = $parent->Title . ': ' . $this->Title;
} else {
$this->FullTitle = $this->Title;
}
parent::populateDefaults();
}