Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » automated deletion

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: 522 Views
  • theAlien
    Community Member
    131 Posts

    automated deletion Link to this post

    I have an ImageDataObject with a 1- relation to Image.
    If the Image is deleted (fx in Files and Images), the related ImageDataObject isn't necessary anymore.
    So I'm trying to automate the deletion of the ImageDataObject, but I'm kind of stuck...

    This is what I have right now:

    class MyImageDecorator extends DataObjectDecorator {


       public function onBeforeDelete() {
          $query = new SQLQuery('*', array('ImageDataObject'), $ImageID = $this->ID);
          $query->delete = true;

    However, this prevents the deletion of the image in Files and Images.
    Can someone see what I'm doing wrong and/or give me some advice on automating it?

  • MateuszU
    Community Member
    89 Posts

    Re: automated deletion Link to this post

    You apply that decorator to the File, right? Then I think because it's a decorator, you should use $this->owner->ID instead of $this->ID. Hmm what do you think about using onAfterDelete instead of before? This would be better in case the Image does not get removed for some odd reason and you want to keep your ImageDataObject after all. Also, when decorating, don't use parent call - you are not extending the object in question, so that's not needed.


  • Hamish
    Community Member
    712 Posts

    Re: automated deletion Link to this post

    Also the where statement is malformed:

    $query = new SQLQuery('*', array('ImageDataObject'), $ImageID = $this->ID);

    should probably be:

    $query = new SQLQuery('*', array('ImageDataObject'), "ImageID = {$this->owner->ID}");

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.