2019-11-18 05:58:33 +01:00
---
2014-10-27 04:40:02 +01:00
title: Extending DataObjects
summary: Modify the data model without using subclasses.
2019-11-18 05:58:33 +01:00
---
2014-10-27 04:40:02 +01:00
# Extending DataObjects
2017-07-03 03:22:12 +02:00
You can add properties and methods to existing [DataObject ](api:SilverStripe\ORM\DataObject )s like [Member ](api:SilverStripe\Security\Member ) without hacking core code or sub
classing by using [DataExtension ](api:SilverStripe\ORM\DataExtension ). See the [Extending SilverStripe ](../extending ) guide for more information on
[DataExtension ](api:SilverStripe\ORM\DataExtension ).
2014-10-27 04:40:02 +01:00
2017-07-03 03:22:12 +02:00
The following documentation outlines some common hooks that the [Extension ](api:SilverStripe\Core\Extension ) API provides specifically for managing
2014-10-27 04:40:02 +01:00
data records.
## onBeforeWrite
2016-03-30 02:17:28 +02:00
You can customise saving-behavior for each DataObject, e.g. for adding workflow or data customization. The function is
2014-10-27 04:40:02 +01:00
triggered when calling *write()* to save the object to the database. This includes saving a page in the CMS or altering
a `ModelAdmin` record.
Example: Disallow creation of new players if the currently logged-in player is not a team-manager.
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Security\Security;
use SilverStripe\ORM\DataObject;
class Player extends DataObject
{
private static $has_many = [
2020-09-25 02:09:37 +02:00
'Teams' => 'Team',
2017-10-27 04:38:27 +02:00
];
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
public function onBeforeWrite()
{
2017-08-07 05:11:17 +02:00
// check on first write action, aka "database row creation" (ID-property is not set)
2020-09-25 02:09:37 +02:00
if (!$this->isInDb()) {
2017-10-27 04:38:27 +02:00
$currentPlayer = Security::getCurrentUser();
2020-09-25 02:09:37 +02:00
if (!$currentPlayer->IsTeamManager()) {
throw new \Exception('Player-creation not allowed');
2017-10-27 04:38:27 +02:00
}
2017-08-07 05:11:17 +02:00
}
2017-10-27 04:38:27 +02:00
2017-08-07 05:11:17 +02:00
// check on every write action
2020-09-25 02:09:37 +02:00
if (!$this->record['TeamID']) {
throw new \Exception('Cannot save player without a valid team');
2017-08-07 05:11:17 +02:00
}
2017-10-27 04:38:27 +02:00
2017-08-07 05:11:17 +02:00
// CAUTION: You are required to call the parent-function, otherwise
// SilverStripe will not execute the request.
parent::onBeforeWrite();
}
2017-10-27 04:38:27 +02:00
}
2017-08-03 05:35:09 +02:00
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
## onBeforeDelete
Triggered before executing *delete()* on an existing object.
2015-02-28 01:09:15 +01:00
Example: Checking for a specific [permission ](permissions ) to delete this type of object. It checks if a
2014-10-27 04:40:02 +01:00
member is logged in who belongs to a group containing the permission "PLAYER_DELETE".
2017-08-03 02:51:32 +02:00
```php
2017-10-27 04:38:27 +02:00
use SilverStripe\Security\Permission;
use SilverStripe\Security\Security;
use SilverStripe\ORM\DataObject;
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
class Player extends DataObject
{
private static $has_many = [
2017-08-07 05:11:17 +02:00
"Teams" => "Team"
2017-10-27 04:38:27 +02:00
];
2017-08-07 05:11:17 +02:00
2017-10-27 04:38:27 +02:00
public function onBeforeDelete()
{
2017-08-07 05:11:17 +02:00
if(!Permission::check('PLAYER_DELETE')) {
2017-10-27 04:38:27 +02:00
Security::permissionFailure($this);
exit();
2017-08-07 05:11:17 +02:00
}
2017-10-27 04:38:27 +02:00
2017-08-07 05:11:17 +02:00
parent::onBeforeDelete();
}
2017-10-27 04:38:27 +02:00
}
2017-08-03 02:51:32 +02:00
```
2014-10-27 04:40:02 +01:00
2019-11-18 05:58:33 +01:00
[notice]
2014-10-27 04:40:02 +01:00
Note: There are no separate methods for *onBeforeCreate* and *onBeforeUpdate* . Please check `$this->isInDb()` to toggle
these two modes, as shown in the example above.
2019-11-18 05:58:33 +01:00
[/notice]
2017-11-27 04:39:17 +01:00
## Related Lessons
* [Working with data relationships - $has_many ](https://www.silverstripe.org/learn/lessons/v4/working-with-data-relationships-has-many-1 )