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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Problems deleting - two issues with IDOM and DOM [ SOLVED ]


Go to End


3 Posts   1431 Views

Avatar
DesignCollective

Community Member, 66 Posts

9 April 2011 at 6:05am

Two things.

1. I've had complaints from people removing items in an ImageDOM, where they would delete, then the image file would be erased, but the record would remain in the IDOM, showing as a blank item in the IDOM, with a file icon and would not be able to be removed. Checking in the DB records, the FileID is 0 in such a one.

2. Another delete issue. I have a DataObject with an attached image (defined as 'Photo' => 'Image' ). In the class, there is this:

public function onBeforeDelete() {
	$this->Photo()->delete();
	parent::onBeforeDelete();
}

I noticed that since the 2.4.5 upgrade the DataObject WITHOUT a Photo attached will not delete anymore, no matter what. A file with an attached Photo will delete without problem.

I am assuming that it has to do with an onBeforeDelete() breaking the actual delete() action of the record, but I am not skilled enough to dig deeper.

Avatar
UncleCheese

Forum Moderator, 4102 Posts

9 April 2011 at 7:34am

That's because PHP is erroring out before the delete can be executed. You've daisy-chained a delete method against an object that is not known to exist.

public function onBeforeDelete() {
$this->Photo()->delete();
parent::onBeforeDelete();
}

Consider:

public function onBeforeDelete() {
if($p = $this->Photo()) {
$p->delete();
}
parent::onBeforeDelete();
}

Avatar
DesignCollective

Community Member, 66 Posts

9 April 2011 at 7:38am

Of course, I thought the method would still call and return null. Thanks!