brasileric
brasileric

Reputation: 1117

Extend a DataObject in SilverStripe

I installed a photogallery module in SilverStripe. This module has a DataObject named PhotoItem.

The PhotoItem class contains some fields, but I want to add extra fields. The easiest way to do that is to edit the PhotoItem file, but then I lose my changes when updating the module.

How can I extend this DataObject with some more fields with a DataObject file under /mysite/code?

Upvotes: 1

Views: 1308

Answers (2)

3dgoo
3dgoo

Reputation: 15794

In Silverstripe 3.1 you can extend a class by creating a DataExtension and applying it to your class.

First you would create a CustomPhotoItem.php in mysite/code or mysite/code/extensions:

CustomPhotoItem.php

class CustomPhotoItem extends DataExtension {

    private static $db = array(
        'ExtraTextField' => 'Text'
    );

    public function updateCMSFields(FieldList $fields) {
        $fields->push(TextField::create('ExtraTextField', 'Extra Text Field'));
    }
}

In order to apply this extension to your class, you need to add the following to your config.yml:

config.yml

PhotoItem:
  extensions:
    - CustomPhotoItem

Your config.yml should be located in mysite/_config/config.yml.

Run dev/build?flush=1 and you should see your new variables added to your original object.

Upvotes: 3

g4b0
g4b0

Reputation: 951

You are searching for DataExtension. Have a look to the documentation, there's evereything you need for adding some more fields to DataObjects. In particular have a look to the section named Adding extra database fields

Upvotes: 1

Related Questions