SilverStripe - 从一个管理模型中选择复选框字段以供另一个管理模型使用

时间:2017-07-21 12:48:25

标签: php silverstripe

我有一个名为PortfolioItemPage的管理模型,与另一个名为$many_many的管理模型有MediaTypeTag关系。在PortfolioItemPage中,我已将所有值映射到MediaTypeTag的复选框到单独的标签中(这样,投资组合项可以与不同的媒体标签相关联,以便在网站上进行过滤)。

每个PortfolioItemPage数据对象与数据对象$has_many具有PortfolioChildItem关系。基本上,PortfolioItemPage代表公司,PortfolioCildItem代表公司的工作样本。 (即公司ABC有6个样本组合件,XYZ公司有4个组合件等)

我想要做的是向MediaTypeTags数据对象添加PortfolioChildItem的复选框列表,但仅添加由子项目的相应PortfolioItemPage选择的复选框列表。例如,如果公司ABC(一个PortfolioItemPage数据对象)选择了以下媒体标签:电视,网站,广告牌(所有3个都是MediaTypeTag数据对象),我想只有这3个媒体标签显示为每个投资组合样本的复选框选项(每个都是PortfolioChildItem)。

我不确定这样的事情是否可能,因为我之前从未尝试过这样的事情。这是可行的吗?

以下是提到的数据对象的代码: PortfolioChildItem

<?php
class PortfolioItemPage extends Page {

    private static $db = array(
        'Excerpt' => 'Text',
        'PreferredItem' => 'Boolean'
    );

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

    private static $has_many = array(
        'PortfolioChildItems' => 'PortfolioChildItem'
    );

    private static $many_many = array(
        'MediaTypeTags' => 'MediaTypeTag',
    );

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

        if ($this->ID) {
            $fields->addFieldToTab('Root.Media Type Tags', CheckboxSetField::create(
                'MediaTypeTags',
                'Media Type Tags',
                MediaTypeTag::get()->map()
            ));
        }

        $gridFieldConfig = GridFieldConfig_RecordEditor::create();

        $gridFieldConfig->addComponent(new GridFieldBulkUpload());

        $gridFieldConfig->addComponent(new GridFieldSortableRows('SortOrder'));
        $gridFieldConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
            'YouTubeEmbedURL' => 'YouTube Video ID',
            'SoundCloudEmbedURL' => 'SoundCloud Embed Code',
            'Thumb' => 'Thumb (135px x 135px)',
        ));

        $gridfield = new GridField(
            "ChildItems",
            "Child Items",
            $this->PortfolioChildItems(),
            $gridFieldConfig
        );

        $fields->addFieldToTab('Root.Child Items', $gridfield);

        $fields->addFieldToTab("Root.Main", new TextareaField("Excerpt"), "Content");
        $fields->addFieldToTab("Root.Main", new UploadField('Thumbnail', "Thumbnail (400px x 400px)"), "Content");

        $fields->addFieldToTab("Root.Main", new CheckboxField('PreferredItem', 'Show on all pages?', 0), "Content");


        return $fields;
    }

    public function getChildItems() {
        return $this->PortfolioChildItems()->sort('SortOrder');
    }

    public function getFormattedContent() {
        return addslashes($this->Content);
    }
}

class PortfolioItemPage_Controller extends Page_Controller {


    private static $allowed_actions = array (
    );

    public function init() {
        parent::init();
    }

}

PortfolioChildItem:

<?php
class PortfolioChildItem extends DataObject {

    private static $db = array(
        'YouTubeEmbedURL' => 'Varchar(25)',
        'SoundCloudEmbedURL' => 'Text',
        'SortOrder' => 'Int'
    );


    private static $has_one = array(
        'Banner' => 'Image',
        'Thumb' => 'Image',
        'PortfolioItemPage' => 'Page'
    );

    private static $many_many = array(
        'MediaTypeTags' => 'MediaTypeTag',
        'IndustryTags' => 'IndustryTag'
    );


    // tidy up the CMS by not showing these fields
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->removeFieldFromTab("Root.Main","PortfolioItemPageID");

        //code to create checkboxes goes here

        return $fields;
    }

    private static $field_labels = array(
        'Banner' => 'Banner (1280px x 700px)',
        'Thumb' => 'Thumb (135px x 135px)',
        'YouTubeEmbedURL' => 'YouTube Video ID (ex: OVgpJIkHmAw from video URL)',
        'SoundCloudEmbedURL' => 'SoundCloud Embed Code',
    );

    // this function creates the thumnail for the summary fields to use
    public function getThumbnail() {
        return $this->Image()->CMSThumbnail();
    }

    public function getSoundCloudURL() {
        return;
    }
}

MediaTypeTag:

<?php
class MediaTypeTag extends DataObject {

    private static $db = array(
        'Name' => 'varchar(250)',
    );

    private static $summary_fields = array(
        'Name' => 'Title',
    );

    private static $field_labels = array(
        'Name'
    );

    private static $belongs_many_many = array(
        'PortfolioItemPages' => 'PortfolioItemPage'
    );

    // tidy up the CMS by not showing these fields
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->removeByName("PortfolioItemPages");

        return $fields;
    }

    static $default_sort = "Name ASC";
}

1 个答案:

答案 0 :(得分:2)

您应该可以通过获取MediaTypeTag ID然后将其用作过滤器来执行此操作。

在课程getCMSFields的{​​{1}}函数中添加类似的内容。

PortfolioChildItem

然后将$mediaTypeTagIDs = array(); $mediaTypeTags = $this->PortfolioItemPage()->MediaTypeTags(); if ($mediaTypeTags->count()) { foreach ($mediaTypeTags as $mediaTypeTag) { $mediaTypeTagIDs[] = $mediaTypeTag->ID; } $mediaTypeTagList = DataList::create('MediaTypeTag')->byIDs($mediaTypeTagIDs); } 添加到网格字段中。