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.

General Questions

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

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

How to manage DataObjects


Reply

6 Posts   486 Views

Avatar
michael_geeky

19 October 2013 at 7:10pm Community Member, 10 Posts

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
simon_w

19 October 2013 at 8:09pm Forum Moderator, 474 Posts

Have a look at [url=http://doc.silverstripe.org/framework/en/reference/modeladmin]ModelAdmin[/url].

Avatar
michael_geeky

20 October 2013 at 12:10am (Last edited: 20 October 2013 12:11am), Community Member, 10 Posts

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

21 October 2013 at 12:35pm Community Member, 254 Posts

checkout onBeforeDelete

I use

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

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

Avatar
simon_w

21 October 2013 at 12:37pm Forum Moderator, 474 Posts

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

23 October 2013 at 1:46pm Community Member, 254 Posts

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

Thanks Simon_w