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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

Display custom many_many relations


Go to End


1179 Views

Avatar
Fmateo

Community Member, 5 Posts

17 April 2015 at 6:00pm

Edited: 17/04/2015 6:18pm

Hi guys,
I have Category DataObject with $many_many products and a Product DataObject with $belongs_many_many Category. I want display categories on CMS admin on different group to make interface be more usable. But I only can put one field for each Product...

I want display...

Product.php

//Relations...
    static $belongs_many_many = array(
        'Categories' => 'PCCategory'
    );

//On getCMS()
new CheckboxSetField('Category', 'Root categories', $this->getRootCategories()),
new CheckboxSetField('Category', 'Level 2 categories', $this->getLevel2Categories()) //Wrong because i used 'Categorý' on previous line, How i can do this on correct way?

//Functions...

   function getRootCategories()
    {
        $map = array();

        foreach (PCCategory::get() as $category) {
            if($category->RootCategory) {
                $map[$category->ID] = $category->Title;
            }
        }
        return $map;
    }
    function getLevel2Categories(){
        $map = array();

        foreach (PCCategory::get() as $category) {
           foreach($category->ParentCategories() as $parentCategory){
                if($parentCategory->RootCategory){
                    $map[$category->ID] = $category->Title;
                }
           }
        }
        return $map;
    }

Category.php

//Relations
private static $many_many = array(
			'ChildCategories' => 'Category',
			'Products' => 'PCProduct'
		);
private static $belongs_many_many = array(
			'ParentCategories' => 'Category'
		);

How i can display custom field?