andrewhoule / silverstripe-photogallery

Simple photo gallery with albums for Silverstripe
BSD 3-Clause "New" or "Revised" License
11 stars 13 forks source link

Issue with OnBeforeDelete in Photo Album #26

Open Station95 opened 3 years ago

Station95 commented 3 years ago

When trying to unpublish a photo gallery that has an Album I am getting the following error:

[Emergency] Uncaught InvalidArgumentException: DataObject::getComponents(): Unknown 1-to-many component 'PhotoItems' on class 'AndrewHoule\PhotoGallery\Models\PhotoAlbum' Trace SilverStripe\ORM\DataObject->getComponents(PhotoItems) PhotoAlbum.php:205 AndrewHoule\PhotoGallery\Models\PhotoAlbum->OnBeforeDelete() DataObject.php:1686

Specifically, it appears that the code that deletes the photo items in the album is not working:

        // Delete the photo items in that album
        $photoitems = $this->getComponents("PhotoItems");
        foreach ($photoitems as $photoitem) {
            $photoitemfile = Image::get()->byID($photoitem->Photo()->ID);
            if ($photoitemfile) {
                $photoitemfile->delete();
            }
        }
Station95 commented 3 years ago

I figured out that the above code should probably be something like:

        // Delete the photo items in that album
        $photoitems = $this->getManyManyComponents("PhotoItems");
        foreach ($photoitems as $photoitem) {
            $photoitemfile = Image::get()->byID($photoitem->ID);
            if ($photoitemfile) {
                $photoitemfile->delete();
            }
        }
andrewhoule commented 3 years ago

Good catch, thank you! Any chance you could do a pull request for that?

Station95 commented 3 years ago

Thanks @andrewhoule I have created a pull request