I can't seem to find a way to detect relationship additions/changes in onAfterWrite(). E.g. I have a DataObject which $has_one MyFile. When I test onAfterWrite() and look for $this->MyFileID after adding an object it shows the new ID. This is great, but I remove the MyFile, the ID doesn't revert to 0. Likewise $this->MyFile()->exists(), $this->MyFile() and $this->MyFile->ID all return old data, only after saving for second time do I get the current values. Furthermore, $this->getChangedFields() behaves like MyFileID, showing only addtions, not deletions.
Is this normal behaviour or do I have something wrong and the relation ID should be resetting to 0?
What happens is the MyFile object was removed from the database, but the fields of the object that have this has_one relationship to MyFile wasn't updated. In your MyFile class, you need to declare a onBeforeDelete() function which updates the MyFileID field to zero.