Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Getting server error when trying to use GridField to create relationship between DataObjects

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

Page: 1
Go to End
Author Topic: 381 Views
  • jordanmk
    Avatar
    Community Member
    2 Posts

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

    Edit: A solution to this problem was posted on my StackOverflow post for the same question: http://stackoverflow.com/questions/22431880/getting-server-error-when-trying-to-use-gridfield-to-create-relationship-between

    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.

    Ingredient.php:

    <?php

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

    private static $belongs_many_many = array(
    'Fragrances' => 'Fragrance'
    );
    }

    ?>

    Fragrance.php:

    <?php

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

    private static $many_many = array(
    'Ingredients' => 'Ingredient'
    );
    }

    ?>

    FragrancesPage.php:

    <?php

    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();
    $config->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
    'Name' => 'Name',
    'Ingredient.Name' => 'Ingredient'
    ));

    $fragrances_field = new GridField(
    'Ingredients',
    'Ingredient',
    $this->Ingredients(),
    $config
    );

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

    $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();
    $form->saveInto($submission);
    $submission->write();

    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.

    381 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.