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.

Data Model Questions /

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

Linking/Unlinking Many-Many records in silverstripe, also update database-field based on action ie link/unlink


Go to End


2 Posts   1794 Views

Avatar
thomas.paulson

Community Member, 107 Posts

1 September 2015 at 6:36pm

I have created a Customer dataobject by extending Member, also created many_many data-relation with Package dataobject as mention, I would like increment/decrement 'Credits' field in Customer dataobject, when package is linked/unlinked ie based on Package.Limit

       <?php   
		class Customer extends Member {

		  private static $db = array(
		    'Gender' => 'Varchar(2)',
		    'DateOfBirth' => 'Date',
		    'Featured' => 'Boolean',
		    'Credits' => 'Int'
		  ); 
	
		  private static $many_many = array(
		    "Packages" => "Package",
		  );

		  
	
			public function getCMSFields() {
				//$fields->addFieldToTab('Root.Main',new TextField('Team',"Team"));
				$fields = new FieldList();

		
			// Create a default configuration for the new GridField, allowing record editing
			$config = GridFieldConfig_RelationEditor::create();
				$config->removeComponentsByType('GridFieldAddNewButton');
			// Create a gridfield to hold the package
			$packageField = new GridField(
			    'Packages', // Field name
			    'Package', // Field title
			    $this->Packages(), // List of all related students
			    $config
			);        
			// Create a tab named "Package" and add our field to it
			$fields->addFieldToTab('Root.Package', $packageField); 
				
			Session::set("SingleID", $this->ID);
			//echo $this->ID;
		
				$this->extend('updateCMSFields', $fields);

				return $fields;

			}
	
	
		}

		class Package extends DataObject{
	
			private static $db = array(
					'Title' => 'Varchar(255)',
					'Limit' => 'Int',
				);
	
			private static $belongs_many_many = array(
				"Customers" => "Customer"
			);	
	
	
		}

Avatar
thomas.paulson

Community Member, 107 Posts

2 September 2015 at 3:58pm

i tried the below code, but the onAfterWrite event does seems to work.

class  Customer_Packages extends DataObject{

	private static $db = array(
	);
	
	private static $has_one = array(
		"Customer" => "Customer",
		"Package" => "Package"
	);
	

    public function onAfterWrite(){	
		parent::onAfterWrite();	
		if($this->CustomerID){			
			$customer = Customer::get()->filter(array(
				'ID' => $this->CustomerID
				));
			$customer->Credits += 10;
			$customer->write();			 
		}		
		$fd = fopen(dirname(__FILE__).'/test.txt', "w");			
		fwrite($fd, print_r($this,true));
		fclose($fd);				
		
	}		
	
}