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.

Data Model Questions /

Delete users Avatar before update


Reply


3 Posts   906 Views

Avatar
Routh

Community Member, 11 Posts

10 April 2011 at 5:42am

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?

Avatar
Routh

Community Member, 11 Posts

10 April 2011 at 6:20am

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

Avatar
joern

Community Member, 28 Posts

10 May 2011 at 7:04pm

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();
}