Jump to:

7935 Posts in 1536 Topics by 943 members

DataObjectManager Module

SilverStripe Forums » DataObjectManager Module » Images In DataObjectManager not deleting!

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Page: 1
Go to End
Author Topic: 711 Views
  • chrisabey
    Avatar
    Community Member
    11 Posts

    Images In DataObjectManager not deleting! Link to this post

    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 {

    }

  • chrisabey
    Avatar
    Community Member
    11 Posts

    Re: Images In DataObjectManager not deleting! Link to this post

    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!!

  • chrisabey
    Avatar
    Community Member
    11 Posts

    Re: Images In DataObjectManager not deleting! Link to this post

    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.

  • UncleCheese
    Avatar
    4085 Posts

    Re: Images In DataObjectManager not deleting! Link to this post

    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

  • chrisabey
    Avatar
    Community Member
    11 Posts

    Re: Images In DataObjectManager not deleting! Link to this post

    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

  • chrisabey
    Avatar
    Community Member
    11 Posts

    Re: Images In DataObjectManager not deleting! Link to this post

    Figured it out! - Thanks Uncle Cheese!

  • stevanovich
    Avatar
    Community Member
    60 Posts

    Re: Images In DataObjectManager not deleting! Link to this post

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

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