DOC Correct the example for getters/setters

This commit is contained in:
Maxime Rainville 2020-08-06 10:15:14 +12:00
parent 65c3295917
commit 9f94c739c1

View File

@ -216,26 +216,37 @@ See the [Template casting](/developer_guides/templates/casting) section for cont
## Overloading ## 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`. 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>`". 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 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 ```php
use SilverStripe\ORM\DataObject; use SilverStripe\ORM\DataObject;
class Player extends DataObject /**
* @property Float $Cost
*/
class Product extends DataObject
{ {
private static $db = [ 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);
}
} }
``` ```