phpsilverstripedata-objects

Silverstripe 3.4.0 - Set Relationship between DataObject and Page - Data not saving to database


I have a website with a project gallery, and there are subpages on the site dedicated to project types. I want to allow content managers to be able to select any project gallery items to display on any subpage of their choosing. A subpage can have many project gallery items, and a project gallery item can be display on one or more subpages.

I set up a CheckboxSetField on Page.php that displays a list of all project names from the ProjectGalleryItem DataObject. I have the checkbox list displaying in a separate tab on all subpages, but I cannot get any selected values to save--the checkboxes checked are automatically unchecked upon saving. No errors are showing in the log files or in the console.

I'm wondering if my relationships between Page and ProjectGalleryItem are incorrect?

Here is the code for ProjectGalleryItem:

<?php

class ProjectGalleryItem extends DataObject
{
    private static $db = array(
        'ProjectName'      => 'Varchar(250)',
        'ProjectDescription' => 'Text',
    );

    private static $has_one = array(
        'ProjectImage' => 'Image',
    );

    private static $belongs_many_many = array(
        'Pages' => 'Page',
    );

    private static $summary_fields = array(
        'ProjectName',
        'ProjectDescription',
    );
}

class ProjectGalleryItemAdmin extends ModelAdmin {
    private static $managed_models = 'ProjectGalleryItem';
    private static $url_segment = 'ProjectGalleryItems';
    private static $menu_title = 'Project Gallery Items';
}

And here is half of the Page.php file:

<?php

class Page extends SiteTree
{
    private static $db = array(
        'BannerH1' => 'HTMLText',
        'BannerSubH1' => 'HTMLText',
        'H1' => 'varchar(250)',
        'PageTitle' => 'varchar(250)',
        'BottomContent' => 'HTMLText',
        'StylizedList' => 'HTMLText',
    );

    private static $has_one = array(
        'BannerImg' => 'Image',
    );

    private static $has_many = array(
        'RotatorImages' => 'RotatorImage',
    );

    private static $many_many = array(
        'ProjectGalleryItems' => 'ProjectGalleryItem'
    );

    public function getCMSFields()
    {
        $fields = parent::getCMSFields();

        $fields->addFieldToTab("Root.Main", new TextField("H1"), "Content");
        $fields->addFieldToTab("Root.Main", new HTMLEditorField("BannerH1"), "Content");
        $fields->addFieldToTab("Root.Main", new HTMLEditorField("BannerSubH1"), "Content");
        $fields->addFieldToTab("Root.Main", new HTMLEditorField('BottomContent', "Bottom Content"));
        $fields->addFieldToTab("Root.Main", new HTMLEditorField('StylizedList', "Stylized List"), "BottomContent");
        $fields->addFieldToTab('Root.Main', new TextField('PageTitle', 'Page Title'), 'MetaDescription');
        $fields->addFieldToTab("Root.Main", new UploadField('BannerImg', "Subpage Banner"), "Content");


        $metadataTab = $fields->findOrMakeTab('Root.Main.Metadata');
        $fields->removeByName('Metadata');
        $fields->addFieldToTab('Root.Main', $metadataTab);

        $gridFieldConfig = GridFieldConfig_RecordEditor::create();

        $gridFieldConfig->addComponent(new GridFieldBulkUpload());
        $gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));

        $gridFieldConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
            // field from drawer class => label in UI
            'ID' => 'ID',
            'Title' => 'Title',
            'Thumbnail' => 'Thumbnail',
            'InternalURL.Link' => 'Internal URL',
        ));

        $gridfield = new GridField(
            "RotatorImages",
            "Rotator Images",
            $this->RotatorImages()->sort("SortOrder"),
            $gridFieldConfig
        );

        $fields->addFieldToTab('Root.Rotator Images', $gridfield);

        $projects = DataObject::get('ProjectGalleryItem');

        if(!empty($projects))
        {
            $map = $projects->Map('ID', 'ProjectName');

            $fields->addFieldToTab('Root.Projects',
            CheckboxSetField::create(
                $name = "Projects",
                $title = "Select Projects",
                $source = $map
            ));
        }

        return $fields;
    }
}

Solution

  • It looks to me like the issue is your form field name, it should match the name of the relation. The first parameter to CheckboxsetField::create() should be ProjectGalleryItems, not Projects.