Michael Holland
Michael Holland

Reputation: 471

How to extend Silverstripe Blog module?

I'm looking a using Siverstripe's blog module for a project. The blog has most of the features I want, but as the site is mostly book focused I's like to add some fields to the blogpost table to hold book data (title, author, rating, etc. It seems like this should be relatively simple but I can't seem to get it to work. I've created the following extension PHP file:

namespace SilverStripe\Blog\Model;

use SilverStripe\Blog\Model\BlogPost;
use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\TextField;
use SilverStripe\Forms\TextareaField;

class BookDataExtension extends BlogPost
{

    private static $db = [
        'bookTitle' => 'Varchar',
        'bookAuthor' => 'Varchar',
        'bookSeries' => 'Varchar',
        'bookISBN' => 'Varchar',
        'bookSeriesNum' => 'Int',
        'bookRating' => 'Decimal',
        'bookCover' => 'Varchar'
    ];

}

And added the following to the mysite.yml file:

SilverStripe\Blog\BlogPost:
  extensions:
    - SilverStripe\Blog\BookDataExtension

I also tried adding the above to the config.yml file for the blog module itself. However, no matter what I try, when I rebuild the system it creates new table(s) for BookDataExtension rather than adding the fields to the BlogPost table. What am I doing wrong?

Upvotes: 0

Views: 514

Answers (2)

Darjan Pezer
Darjan Pezer

Reputation: 1

try this:

<?php

namespace {

    use SilverStripe\ORM\DataExtension;
    use SilverStripe\Forms\FieldList;

    class BookDataExtension extends DataExtension {

        private static $db = [
            'db_field_example' => 'Varchar'
        ];

        public function updateCMSFields(FieldList $fields) {
            // Add fields here
        }

    }

}

add your extension to app/src/extensions/

and in your config:

SilverStripe\Blog\Model\BlogPost:
  extensions:
    - BookDataExtension

Upvotes: 0

wmk
wmk

Reputation: 4626

you subclassed BlogPost instead of plugging an extension to it, aka. extending it...

Your BlogPostExtension has to subclass DataExtension; it can be in your own namespace:

namespace MyProject\Extensions;

use SilverStripe\ORM\DataExtension;

class BookDataExtension extends DataExtension
{

    private static $db = [
        'bookTitle' => 'Varchar',
        'bookAuthor' => 'Varchar',
        'bookSeries' => 'Varchar',
        'bookISBN' => 'Varchar',
        'bookSeriesNum' => 'Int',
        'bookRating' => 'Decimal',
        'bookCover' => 'Varchar'
    ];

}

Then you can configure BlogPost to add your extension like you did before:

SilverStripe\Blog\BlogPost:
  extensions:
    - MyProject\Extensions\BookDataExtension

Upvotes: 1

Related Questions