Jump to:

10848 Posts in 2516 Topics by 1792 members

All other Modules

SilverStripe Forums » All other Modules » Subsite: delete related pages

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1108 Views
  • dompie
    Avatar
    Community Member
    88 Posts

    Subsite: delete related pages Link to this post

    Hello community,

    I use subsites module with SS 2.4.1 and unfortunately Subsites does not delete related pages, when deleting a Subsite. I have adjusted the onAfterDelete method of the Subsite class:

       public function onAfterDelete(){
          parent::onAfterDelete();
          $id = $this->getField('ID');
          
          if(!empty($id)){
             DB::query('DELETE IGNORE FROM SubsiteDomain WHERE SubsiteID = '.$id);
             $res = DB::query('SELECT ID, ClassName FROM SiteTree WHERE SubsiteID = '.$id);
             $res = $res->map();
             foreach($res as $id => $className){
                $somePage = DataObject::get_by_id($className, $id);
                $somePage->deleteFromStage('Live');
                $somePage->deleteFromStage('Stage');
                $somePage->delete();
             }
             
          }else{
             $e = new Exception("Could not delete related SubsiteDomains and Pages with SubsiteID: $id!");
             SS_Log::log($e, SS_Log::WARN);
          }
          
       }


    I have also tried to put it in onBeforeDelete and DataObject::use get_one() but for some reason always 'false' is returned by the getXXX DataObject. But when looking in SiteTree table (or SiteTree_Live, or XXX_Live), the pages still exist. How can I get rid of them?

  • dompie
    Avatar
    Community Member
    88 Posts

    Re: Subsite: delete related pages Link to this post

    Any new ideas on this topic?
    I have made some progress to this code:

       public function onBeforeDelete(){
          parent::onBeforeDelete();
          
          $subsiteId = $this->getField('ID');

          if(!empty($subsiteId)){
             DB::query('DELETE IGNORE FROM SubsiteDomain WHERE SubsiteID = '.$subsiteId);
             $res = DB::query('SELECT ID, ClassName FROM SiteTree WHERE SubsiteID = '.$subsiteId);
             $res = $res->map();
             
             foreach($res as $id => $className){
                $DOS = DataObject::get($className, "\"$className\".\"ID\" = $id");
                if($DOS instanceof DataObjectSet){
                   $somePage = $DOS->First();
                   $somePage->deleteFromStage('Live');
                   $somePage->deleteFromStage('Stage');
                   $somePage->delete();
                }
             }
             
          }else{
             $e = new Exception("Could not delete related SubsiteDomains and Pages with SubsiteID: $id!");
             SS_Log::log($e, SS_Log::WARN);
          }
       }


    In theory this should work, am I right? However, the Pages retain in Database Tables 'SomePage' and 'SomePage_Live' ... why? How can I delete them from Database?

  • Martijn
    Avatar
    Community Member
    271 Posts

    Re: Subsite: delete related pages Link to this post

    You might want to disable the Subsite Filter before you do DataObject::get();

    $oldState = Subsite::$disable_subsite_filter; //store current filterstate
    Subsite::$disable_subsite_filter = true; // disable filter
    // Do DataObject operation
    $page = DataObject::get_one('Page','YourWhereStament');
    $page ->deleteFromStage('Live');
    $page ->deleteFromStage('Stage');
    $page ->delete();
    Subsite::$disable_subsite_filter = $oldState; //restore filterstate

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