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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

All other Modules /

Discuss all other Modules here.

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

Subsite: delete related pages


Go to End


3 Posts   1553 Views

Avatar
dompie

Community Member, 88 Posts

29 September 2010 at 4:43am

Edited: 29/09/2010 4:57am

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?

Avatar
dompie

Community Member, 88 Posts

21 October 2010 at 3:05am

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?

Avatar
Martijn

Community Member, 271 Posts

21 October 2010 at 7:25am

Edited: 21/10/2010 7:26am

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