Merge pull request #9632 from open-sausages/pulls/4/tweak-getter-setter-doc

DOC Correct the example for getters/setters
This commit is contained in:
Robbie Averill 2020-08-05 15:26:57 -07:00 committed by GitHub
commit a166ba0ebd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 7 deletions

View File

@ -216,26 +216,37 @@ See the [Template casting](/developer_guides/templates/casting) section for cont
## Overloading
"Getters" and "Setters" are functions that help us save fields to our [DataObject](api:SilverStripe\ORM\DataObject) instances. By default, the
"Getters" and "Setters" are functions that help save fields to our [DataObject](api:SilverStripe\ORM\DataObject) instances. By default, the
methods `getField()` and `setField()` are used to set column data. They save to the protected array, `$obj->record`.
We can overload the default behavior by making a function called "get`<fieldname>`" or "set`<fieldname>`".
The following example will use the result of `getStatus` instead of the 'Status' database column. We can refer to the
database column using `dbObject`.
database column using `getField()`.
```php
use SilverStripe\ORM\DataObject;
class Player extends DataObject
/**
* @property Float $Cost
*/
class Product extends DataObject
{
private static $db = [
"Status" => "Enum(['Active', 'Injured', 'Retired'])"
'Title' => 'Varchar(255)',
'Cost' => 'Int', //cost in pennies/cents
];
public function getStatus()
public function getCost()
{
return (!$this->obj("Birthday")->InPast()) ? "Unborn" : $this->dbObject('Status')->Value();
return $this->getField('Cost') / 100;
}
public function setCost($value)
{
return $this->setField('Cost', $value * 100);
}
}
```