Skip to content

Instantly share code, notes, and snippets.

@sunnysideup
Last active July 21, 2026 03:33
Show Gist options
  • Select an option

  • Save sunnysideup/ed6cb6831d2a0248b0e1e6d9e09c0e09 to your computer and use it in GitHub Desktop.

Select an option

Save sunnysideup/ed6cb6831d2a0248b0e1e6d9e09c0e09 to your computer and use it in GitHub Desktop.
reasissgn silverstripe classname
<?php
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\ORM\DB;
use SilverStripe\Versioned\Versioned;
class ApiReassignClassName {
protected function setNewClassNameForPage(SiteTree $record, string $newValue, int $id, ?bool $forceInsert = false)
{
$isPublished = $record->isPublished();
$isModifiedOnDraft = $record->isModifiedOnDraft();
$doPublish = $isPublished && !$isModifiedOnDraft;
$record = $record->newClassInstance($newValue);
$record->writeToStage(Versioned::DRAFT);
if ($doPublish) {
$record->publishRecursive();
}
foreach (['', '_Live'] as $suffix) { // also '_Versions'???
$idField = $suffix === '_Versions' ? 'RecordID' : 'ID';
$tableName = 'SiteTree' . $suffix;
if ($forceInsert) {
$sql = '
INSERT INTO "'.$tableName.'" ("'.$idField.'", "ClassName")
VALUES ('.$id.', \''.addslashes($newValue).'\')
ON DUPLICATE KEY UPDATE "ClassName" = \''.addslashes($newValue).'\'';
} else {
$sql = 'UPDATE "'.$tableName.'" SET "ClassName" = \''.addslashes($newValue).'\' WHERE "'.$idField.'" = '.$id;
}
DB::query($sql);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment