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:

Customising the CMS /

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

ManyManyComplexTableField in Popup


Go to End


11 Posts   5560 Views

Avatar
Matze0681

Community Member, 25 Posts

18 March 2011 at 5:08pm

Maybe not the best way but i solved this in onBeforeWrite().

Example:

        
        static $write_count = 0;


	function onBeforeWrite(){
		parent::onBeforeWrite();

		//workaround for saving correct ManyMany Relation in Popup 
                //$write_count : Make sure only updated once
		if(self::$write_count == 0 && isset($_REQUEST['ProductVariationDetails'])){

			$variationDetails = $_REQUEST['ProductVariationDetails'];
                        //remove not needed array key
			unset($variationDetails['selected']);
                        //remove all relations to the DataObject first.(Needed if relations get de-selected in Table)
			$this->owner->ProductVariationDetails()->removeAll();

			foreach($variationDetails as $detailKey => $detailID){
				$ProductVariationDetail = DataObject::get_by_id('ProductVariationDetail',(int)$detailID);
				if(isset($ProductVariationDetail->ID)){
					$this->owner->ProductVariationDetails()->add($ProductVariationDetail);
				}
			}
			self::$write_count++;
		}
	}

Avatar
mark_s

Community Member, 78 Posts

3 May 2011 at 10:44pm

Edited: 03/05/2011 10:45pm

Hi. I got it to save by doing this:

	function getCMSFields() {
		$fields = parent::getCMSFields();
		$fields->addFieldToTab(
			"Root.Content.Media",
			new ManyManyComplexTableField(new Page_Controller($this), "Media", "Image")
		);
		return $fields;
	}

Note this is inside getCMSFields, which is in Page. ManyManyComplexTableField requires a Controller as its first parameter, so I create a new Page_Controller and pass $this (the page) as a parameter to the controller. If you pass $this to ManyManyCompexTableField when you're in getCMSFields, you're passing in a Page object not a Controller.

Mark

Avatar
HARVS1789UK

Community Member, 31 Posts

26 September 2012 at 12:22am

Edited: 26/09/2012 12:23am

mark_s unfortunatly your solution didnt work for me.

Matze0681 however yours worked like a charm!

I was unsure of where to add your code at first so for the benefit of others, my context is that I have extended Member to create LMM_Member. My LMM_Member and Segment objects have a Many - Many relation. I hadded the ManyManyComplexTableField and onBeforeWrite() in my LMM_Member extension and my LMM_Segment object remained the same. See below for code example:

LMM_Member

class LeadManagementMember extends DataObjectDecorator {
    
    //For some reason with this SS build extraStatics() doesnt build properly, you must build with extraDBfields (which works but throws deprecated warning)
    //public function extraDBfields() {
    public function extraStatics() {
        return array(
            'has_many' => array(
                'Leads' => 'LMM_Lead'
            ),
            'many_many' => array(
                'Segments' => 'LMM_Segment'
            )
        );
    }

    public function updateCMSFields(FieldSet &$fields) { 

        //Only add these fields if the current user is a LMM_User
        if(Permission::checkMember($this->owner->ID, "LMM_ACCESS")) {
                $table = new ManyManyComplexTableField(
                $this->owner,
                'Segments',
                'LMM_Segment',
                array(
                    'ID' => 'ID',
                    'DisplayVal' => 'Segment Name'
                )
            );
            $table->showPagination = false;
            $table->setParentClass($this->owner->ClassName);
            $fields->addFieldToTab('Root.Segments', $table);
        } 

    }
    
    public static $write_count = 0;
    
    function onBeforeWrite(){ 
        
        parent::onBeforeWrite();

        //workaround for saving correct ManyMany Relation in Popup 
        //$write_count : Make sure only updated once 
        if(self::$write_count == 0 && isset($_REQUEST['Segments'])){
            
            $segments = $_REQUEST['Segments']; 
            
            //remove not needed array key 
            unset($segments['selected']);
             
            //remove all relations to the DataObject first.(Needed if relations get de-selected in Table) 
            $this->owner->Segments()->removeAll();

            foreach($segments as $segmentKey => $segmentID){ 
                $segments = DataObject::get_by_id('LMM_Segment',(int)$segmentID); 
                if(isset($segments->ID)){ 
                    $this->owner->Segments()->add($segments);
                }
            } 
            
        self::$write_count++;
    
        } 
    }
   
}

LMM_Segment

class LMM_Segment extends DataObject {
    
    public static $db = array(
        'DataVal' => 'Varchar(255)',
        'DisplayVal' => 'Varchar(255)'
    );
    
    public static $belongs_many_many = array(
        'Members' => 'Member'
    );
    
}

Thanks again Matzeo!

Go to Top