Skip to content

Instantly share code, notes, and snippets.

@sunnysideup
Last active January 30, 2018 22:38
Show Gist options
  • Save sunnysideup/df819d1103d785941942b35e871cbfbd to your computer and use it in GitHub Desktop.
Save sunnysideup/df819d1103d785941942b35e871cbfbd to your computer and use it in GitHub Desktop.
Delete a Silverstripe Page for both STAGE and LIVE
<?php
class MyExampleDeleteAllForPage extends Object
{
private static function delete_page($idOrPage)
{
if($idOrPage instanceof SiteTree) {
$id = $idOrStage->ID;
}elseif(intval($idOrPage)) {
$id = $idOrStage;
//do nothing
} else {
user_error('idOrPage parameter can not be parsed: '.print_r($idOrPage, 1));
}
$stageRecord = Versioned::get_one_by_stage('SiteTree', 'Stage', "\"SiteTree\".\"ID\" = $id");
if ($stageRecord) {
$stageRecord->deleteFromStage('Stage');
}
if ($stageRecord) {
$stageRecord->delete();
}
$liveRecord = Versioned::get_one_by_stage('SiteTree', 'Live', "\"SiteTree\".\"ID\" = $id");
if ($liveRecord) {
$stageRecord->deleteFromStage('Live');
}
if ($liveRecord) {
$stageRecord->delete();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment