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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

How to manage DataObjects


Go to End


6 Posts   1588 Views

Avatar
michael_geeky

Community Member, 10 Posts

19 October 2013 at 7:10pm

Hi all,

I am using GridField to manage a many_many relationship between a page type and a data object. My question is, if you delete the relation (Not the object itself), how do you get it back later if you don't remember the object name. For example, now I have a page which contains product A, when I click "unlink" in the GridField, you don't see product A anymore, but actually it still exists, I know you can search "product A" to find it and link it again, but what if you don't remember it? Is there a way to independently manage Product objects?

Thanks.

Avatar
(deleted)

Community Member, 473 Posts

19 October 2013 at 8:09pm

Have a look at ModelAdmin.

Avatar
michael_geeky

Community Member, 10 Posts

20 October 2013 at 12:10am

Edited: 20/10/2013 12:11am

Thanks a lot, Simon. BTW, is there a way to automatically delete relations when you delete an object? I tried to remove relations manually but it looks like the mapping table class isn't auto-loaded, e.g. Category_Product::get() will give you an error saying "cannot find this class".

Avatar
Bambii7

Community Member, 254 Posts

21 October 2013 at 12:35pm

checkout onBeforeDelete

I use

foreach( $this->relations() as $relation ) {
$relation->delete();
}

http://doc.silverstripe.com/framework/en/topics/datamodel#onbeforedelete

Avatar
(deleted)

Community Member, 473 Posts

21 October 2013 at 12:37pm

That'll delete the related objects, but it won't clean the relation table. Usually, that doesn't matter since the table has to be rather large before you'd notice any sort of performance penalty.

What you're after is the removeAll() method on RelationList. Something like:

	public function onBeforeDelete() {
		parent::onBeforeDelete();
		$this->RelationName()->removeAll();
	}

Avatar
Bambii7

Community Member, 254 Posts

23 October 2013 at 1:46pm

Interesting I never knew :)
Will update my code to removeAll();

Thanks Simon_w