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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Images In DataObjectManager not deleting!


Reply


7 Posts   895 Views

Avatar
chrisabey

Community Member, 11 Posts

19 August 2011 at 4:18am

Hi there,

I've knocked up a banner management area using DataObjectManager within SiteConfig (with various hints and tips from this forum) and it works perfectly, or should I say WORKED perfectly until today during some testing. When I go to delete an image it 'deletes' fine from within the DataObjectManager but when I browse to the folder the images are stored in, they are still there. I can only remove them by either manually deleting them from the folder or from within 'Files & Images'.

As adding a DataObjectManager in SiteConfig has proven troublesome, I thought it might be an issue to do with that so I redid the code as a simple BannerPage but the problem still exists.

Here is the code - please help as this is driving me nuts!

<-- Banner2.php -->

<?php

class BannerImage2 extends DataObject
{
   static $db = array (
   );

   static $has_one = array (
      'myImage2' => 'Image',
      'BannerPage' => 'BannerPage'
   );

   public function getCMSFields_forPopup()
   {
         $upload = new ImageUploadField('myImage2', 'Upload Banner Image');
         $upload->removeFolderSelection();
         $upload->uploadFolder = 'BannerImages2';
         return new FieldSet($upload);

   }
   
   public function getThumb()
   {
      if($this->myImage2ID)
      return $this->myImage2()->setWidth(400);
      else
      return '(No Image)';
   }
}

<-- BannerPage.php -->

<?php

class BannerPage extends Page {

static $has_many = array(
         'Images' => 'BannerImage2'
   );

public function getCMSFields() {

   $fields = parent::getCMSFields();
   $fields->removeFieldFromTab("Root.Content.Main","Content");
   $fields->addFieldsToTab("Root.Content.Main", new DataObjectManager(
         $this,
         'Images',
         'BannerImage2',
         array('Thumbs'=>'Upload Image'),
         'getCMSFields_forPopup'
         ));
   
   return $fields;
   
   }
}

class BannerPage_Controller extends Page_Controller {

}

Avatar
chrisabey

Community Member, 11 Posts

19 August 2011 at 5:48pm

Ok, I've checked PHPMyAdmin and adding an image via the DataObjectManager DOES add it into the database and by deleting the recorded in the DataObjectManager it DOES delete it from the database but for some reason it is not deleting the actual physical image!!

Avatar
chrisabey

Community Member, 11 Posts

19 August 2011 at 6:05pm

One last little note to help you all to help me, if you click on the record and then delete the image from within the popup, it does physically delete the image but then I am left with an empty record which I have to manually delete.

If I just delete the record on its own, it seems to just remove the link to the image rather than the image itself.

Avatar
UncleCheese

Forum Moderator, 4096 Posts

20 August 2011 at 12:46am

DataObjectManager doesn't manage objects that are related to those that it is managing. That wouldn't make any sense to cascade deletion down to the has_one relationships because if a file is used in multiple places, you'd break stuff! Cascading deletes are business logic, and should only be used when you know that a file will only ever be associated with a single record. For that reason, this is an elective option. You can use an onAfterDelete() hook to delete the associated file if you want.

---------------
Silverstripe tips, tutorials, screencasts, and more. http://www.leftandmain.com

Avatar
chrisabey

Community Member, 11 Posts

20 August 2011 at 1:39am

Thanks for getting back to me UncleCheese. I think I understand what you mean but how would I go about using the onAfterDelete() hook?

Cheers

Avatar
chrisabey

Community Member, 11 Posts

20 August 2011 at 2:09am

Figured it out! - Thanks Uncle Cheese! :-)

Avatar
stevanovich

Community Member, 60 Posts

21 August 2012 at 3:47am

can you share your code for the solution for onAfterDelete() hook thanks