Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Getting server error when trying to use GridField to create relationship between DataObjects

Go to End



Community Member, 3 Posts

16 March 2014 at 2:17pm

Edited: 16/03/2014 9:04pm

Edit: A solution to this problem was posted on my StackOverflow post for the same question:

I'm learning SilverStripe by creating a small website that lets the user manage their fragrances (i.e. perfumes/colognes). The user adds ingredients (that are used in the fragrances they have), then adds their fragrances, at which point they choose which ingredients are in the fragrance they're adding.

I've created the Ingredient and Fragrance classes which both extend DataObject. I've also created the IngredientsPage page which lets the user add/edit/delete ingredients (made up of a name and description) and lists all of the ingredients added so far, and this page is fully functional. I'm now trying to create the FragrancesPage page which will let the user add/edit/delete fragrances (made up of a name, description and ingredients) and list all the ones added so far, but I'm having some trouble.

The only way I know of to create the relationship between a Fragrance and Ingredients (one fragrance has many ingredients, and one ingredient belongs to many fragrances) is using a GridField (if there's a better way, let me know!), as this is what the SilverStripe tutorials get you to do (although in the tutorial it's for the CMS rather than the front-end). However, as soon as I try to add a GridField into the mix, I just get taken to an error page that says "Server Error: Sorry, there was a problem with handling your request.".

My code is as follows.



class Ingredient extends DataObject {
    private static $db = array(
        'Name' => 'Text',
        'Description' => 'Text'

    private static $belongs_many_many = array(
        'Fragrances' => 'Fragrance'




class Fragrance extends DataObject {
    private static $db = array(
        'Name' => 'Text',
        'Description' => 'Text'

    private static $many_many = array(
        'Ingredients' => 'Ingredient'




class FragrancesPage extends Page {
    private static $icon = 'cms/images/treeicons/reports-file.png';
    private static $description = 'Fragrances page';

class FragrancesPage_Controller extends Page_Controller {
    private static $allowed_actions = array('FragranceAddForm');

    function FragranceAddForm() {
        $config = GridFieldConfig_RelationEditor::create();
            'Name' => 'Name',
            'Ingredient.Name' => 'Ingredient'

        $fragrances_field = new GridField(

        $fields = new FieldList(
            new TextField('Name', 'Fragrance Name'),
            new TextareaField('Description', 'Fragrance Description'),

        $actions = new FieldList(
            new FormAction('doFragranceAdd', 'Add Fragrance')

        $validator = new RequiredFields('Name', 'Description');

        return new Form($this, 'FragranceAddForm', $fields, $actions, $validator);

    public function doFragranceAdd($data, $form) {
        $submission = new Fragrance();

        return $this->redirectBack();

    public function FragranceList() {
        $submissions = Fragrance::get()->sort('Name');

        return $submissions;


If I remove everything from FragrancesPage.php relating to the GridField, the page works fine. I just can't seem to get the GridField working, and don't know of any other way to create the relationship between Fragrances and Ingredients on the front-end. If the code for IngredientsPage.php will be helpful also, let me know and I'll add it.