Last active
July 21, 2026 03:33
-
-
Save sunnysideup/ed6cb6831d2a0248b0e1e6d9e09c0e09 to your computer and use it in GitHub Desktop.
reasissgn silverstripe classname
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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