Jump to:

3373 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Delete users Avatar before update

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 719 Views
  • Routh
    Avatar
    Community Member
    11 Posts

    Delete users Avatar before update Link to this post

    Ok, I want to minimize the amount of files created by members. I am trying to create an onBeforeWrite that will delete the users current avatar whenever it is updated. I am using a CommunityRole decorator to define the extra fields for my members, so I have the onBeforeWrite function there.

    However, despite my attempts, the original avatar is never deleted. This is my current code:

       function onBeforeWrite(){
          parent::onBeforeWrite();
          if (isset($this->Avatar)) {
             $member = DataObject::get_one('Member', "ID = ".$this->ID);
             $member->Avatar->delete();
          }
       }

    How do I correct this?

  • Routh
    Avatar
    Community Member
    11 Posts

    Re: Delete users Avatar before update Link to this post

    Ok, I determined that I had forgotten to add the required ->owner layer to the array and the if statement was being ignored; so I have updated my code to:

       function onBeforeWrite(){
          parent::onBeforeWrite();
          if (isset($this->owner->Avatar)) {
             $member = DataObject::get_one('Member', "ID = ".$this->owner->ID);
             $member->Avatar->delete();
          }
       }

    However I can't figure out how to actually get the avatar to delete. The code as it is now spits out the error:

    [Sat Apr 09 18:10:24 2011] [error] [client 67.193.94.39] PHP Fatal error: Call to a member function delete() on a non-object in ****/community/code/CommunityRole.php on line 64, referer: http://amateurwriting.org/community/editprofile?dev=1

  • joern
    Avatar
    Community Member
    28 Posts

    Re: Delete users Avatar before update Link to this post

    hi,
    try something like this, if your method is still in the decorator. (it's untested…)

    function onBeforeWrite() {
    if ($this->owner->AvatarID) {
    $this->owner->Avatar()->delete();
    }
    parent::onBeforeWrite();
    }

    719 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.