Jump to:

23379 Posts in 18298 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » How to manage DataObjects

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

Page: 1
Go to End
Author Topic: 446 Views
  • michael_geeky
    Avatar
    Community Member
    10 Posts

    How to manage DataObjects Link to this post

    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.

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: How to manage DataObjects Link to this post

    Have a look at ModelAdmin.

  • michael_geeky
    Avatar
    Community Member
    10 Posts

    Re: How to manage DataObjects Link to this post

    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".

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: How to manage DataObjects Link to this post

    checkout onBeforeDelete

    I use

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

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

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: How to manage DataObjects Link to this post

    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();
       }

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: How to manage DataObjects Link to this post

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

    Thanks Simon_w

    446 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.