Jump to:

22977 Posts in 11742 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Remove Asset when a dataobject is deleted

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: 525 Views
  • BenWu
    Avatar
    Community Member
    87 Posts

    Remove Asset when a dataobject is deleted Link to this post

    Hello

    I have got a class called project

    class project extends DataObject {

    static $has_one = array {
    'aFile' =>'MyFile'
    }

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

    An the MyFile class is

    MyFile extends File{

    public onBeforeDelete(){
    //do something
    parent::OnBeforeDelete()
    }

    }

    Problem:
    When I delete a Project within the ModelAdmin, I got this error:

    There has been an error

    and I am not sure where to go from here. I check the log file and I got

    [15-May-2012 02:03:15] Notice at d line d: d (/admin/projects/Project/3/EditForm?action_doDelete=Delete)
    [15-May-2012 02:03:15] Error at sapphire/core/model/DataObject.php line 1126: Uncaught Exception: DataObject::delete() called on a DataObject without an ID

  • BenWu
    Avatar
    Community Member
    87 Posts

    Re: Remove Asset when a dataobject is deleted Link to this post

    Let me answer myself:

    When you use the relation-getter, you got a ComponentSet. The delete method for it doesn't work

    Here is what i have to do in the OnbeforeDelete() function of the Project class

    public function OnBeforeDelete(){
    $myFile = $this->aFile();
    $file = DataObject::get_by_id('MyFile', $myFile->ID); //we have to make sure it is a Dataobject object
    $file->delete();
    $file->destroy();
    return parent::OnBeforeDelete();
    }

    525 Views
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.