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

automated deletion


Reply

3 Posts   526 Views

Avatar
theAlien

29 January 2010 at 4:30am Community Member, 131 Posts

Hi,
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;
      $query->execute();
      parent::onBeforeDelete();
   }
}

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?

Avatar
MateuszU

1 February 2010 at 9:51am Community Member, 89 Posts

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.

cheers,
mat

Avatar
Hamish

1 February 2010 at 10:21am Community Member, 712 Posts

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