I have 'Resouce' dataobject as below with attachment as has_one relation. I would like to delete the attachment when 'resource' object is deleted.
But what I get is Fatal error: Call to a member function delete() on a non-object in
class Resource extends DataObject
private static $db = array (
'Name' => 'Varchar(200)',
'Description' => 'Text',
'Category' => "Enum('Data, Drafts, Drawings, Reports, Images, Other')",
'SortOrder' => 'Int'
private static $has_one = array (
'Attachment' => 'File',
'ResourcePage' => 'ResourcePage'
public function onBeforeDelete()
$myAttachment = $this->Attachment();
$file = DataObject::get_by_id('File', $myAttachment->ID); //we have to make sure it is a Dataobject object