text stringlengths 1 330k |
|---|
It is most commonly applied to pages in the CMS (the SiteTree class). This means that draft content edited in the CMS can be different from published content shown to your website visitors. |
The versioning happens automatically on read and write. If you are using the SilverStripe ORM to perform these operations, you don't need to alter your existing calls. |
Versioning in SilverStripe is handled through the Versioned class. It's a DataExtension, which allow it to be applied to any DataObject subclass. |
Adding versioned to your DataObject subclass works the same as any other extension. It accepts two or more arguments denoting the different "stages", which map to different database tables. Add the following to your configuration file: |
- Versioned("Stage","Live") |
Note: The extension is automatically applied to SiteTree class. |
Database Structure |
Depending on how many stages you configured, two or more new tables will be created for your records. Note that the "Stage" naming has a special meaning here, it will leave the original table name unchanged, rather than adding a suffix. |
• MyRecord table: Contains staged data |
• MyRecord_Live table: Contains live data |
• MyRecord_versions table: Contains a version history (new record created on each save) |
Similarly, any subclass you create on top of a versioned base will trigger the creation of additional tables, which are automatically joined as required: |
• MyRecordSubclass table: Contains only staged data for subclass columns |
• MyRecordSubclass_Live table: Contains only live data for subclass columns |
• MyRecordSubclass_versions table: Contains only version history for subclass columns |
Reading Versions |
By default, all records are retrieved from the "Draft" stage (so the MyRecord table in our example). You can explicitly request a certain stage through various getters on the Versioned class. |
// Fetching multiple records |
$stageRecords = Versioned::get_by_stage('MyRecord', 'Stage'); |
// Fetching a single record |
$stageRecord = Versioned::get_by_stage('MyRecord', 'Stage')->byID(99); |
$liveRecord = Versioned::get_by_stage('MyRecord', 'Live')->byID(99); |
Historical Versions |
The above commands will just retrieve the latest version of its respective stage for you, but not older versions stored in the <class>_versions tables. |
$historicalRecord = Versioned::get_version('MyRecord', <record-id>, <version-id>); |
Caution: The record is retrieved as a DataObject, but saving back modifications via write() will create a new version, rather than modifying the existing one. |
In order to get a list of all versions for a specific record, we need to generate specialized Versioned_Version objects, which expose the same database information as a DataObject, but also include information about when and how a record was published. |
$record = MyRecord::get()->byID(99); // stage doesn't matter here |
$versions = $record->allVersions(); |
echo $versions->First()->Version; // instance of Versioned_Version |
Writing Versions and Changing Stages |
The usual call to DataObject->write() will write to whatever stage is currently active, as defined by the Versioned::current_stage() global setting. Each call will automatically create a new version in the <class>_versions table. To avoid this, use writeWithoutVersion()` instead. |
To move a saved version from one stage to another, call writeToStage(<stage>)on the object. The process of moving a version to a different stage is also called "publishing", so we've created a shortcut for this:publish(<from-stage>, <to-stage>)`. |
$record->MyField = 'changed'; |
// will update `MyRecord` table (assuming Versioned::current_stage() == 'Stage'), |
// and write a row to `MyRecord_versions`. |
// will copy the saved record information to the `MyRecord_Live` table |
$record->publish('Stage', 'Live'); |
Similarly, an "unpublish" operation does the reverse, and removes a record from a specific stage. |
// will remove the row from the `MyRecord_Live` table |
Forcing the Current Stage |
The current stage is stored as global state on the object. It is usually modified by controllers, e.g. when a preview is initialized. But it can also be set and reset temporarily to force a specific operation to run on a certain stage. |
$origMode = Versioned::get_reading_mode(); // save current mode |
$obj = MyRecord::getComplexObjectRetrieval(); // returns 'Live' records |
Versioned::set_reading_mode('Stage'); // temporarily overwrite mode |
$obj = MyRecord::getComplexObjectRetrieval(); // returns 'Stage' records |
Versioned::set_reading_mode($origMode); // reset current mode |
Custom SQL |
We generally discourage writing Versioned queries from scratch, due to the complexities involved through joining multiple tables across an inherited table scheme (see Versioned->augmentSQL()). If possible, try to stick to smaller modifications of the generated DataList objects. |
Example: Get the first 10 live records, filtered by creation date: |
$records = Versioned::get_by_stage('MyRecord', 'Live')->limit(10)->sort('Created', 'ASC'); |
The Versioned extension doesn't provide any permissions on its own, but you can have a look at the SiteTree class for implementation samples, specifically canPublish() and canDeleteFromStage(). |
Page Specific Operations |
Since the Versioned extension is primarily used for page objects, the underlying SiteTree class has some additional helpers. See the "sitetree" reference for details. |
Templates Variables |
In templates, you don't need to worry about this distinction. The $Content variable contain the published content by default, and only preview draft content if explicitly requested (e.g. by the "preview" feature in the CMS). If you want to force a specific stage, we recommend the Controller->init() method for this purp... |
The current stage for each request is determined by VersionedRequestFilter before any controllers initialize, through Versioned::choose_site_stage(). It checks for a Stage GET parameter, so you can force a draft stage by appending ?stage=Stage to your request. The setting is "sticky" in the PHP session, so any subseque... |
Important: The choose_site_stage() call only deals with setting the default stage, and doesn't check if the user is authenticated to view it. As with any other controller logic, please use DataObject->canView() to determine permissions, and avoid exposing unpublished content to your users. |
class MyController extends Controller { |
private static $allowed_actions = array('showpage'); |
public function showpage($request) { |
$page = Page::get()->byID($request->param('ID')); |
if(!$page->canView()) return $this->httpError(401); |
// continue with authenticated logic... |
The ContentController class responsible for page display already has this built in, so your own canView() checks are only necessary in controllers extending directly from the Controller class. |
Trapping the publication event |
Sometimes, you'll want to do something whenever a particular kind of page is published. This example sends an email whenever a blog entry has been published. |
class Page extends SiteTree { |
// ... |
public function onAfterPublish() { |
mail("", "Blog published", "The blog has been published"); |
blog comments powered by Disqus |
Sacred Texts Native American Southeast Index Previous Next |
p. 179 |
How the Turkey Hid Her Eggs |
The big turkey cock is so proud of himself that he does not want his wife to pay attention to anything but him. He looks for her eggs when she lays, and if he can find them he breaks them with his beak. If the eggs have hatched he sometimes kills the little ones. There was a time, so says an Indian story, when the turk... |
She began hiding them under dead leaves and twigs in the woods. The old man turkey looked everywhere for the eggs, but they were nowhere to be seen, |
p. 180 |
for they were lying under their little brown blankets. This made the turkey cock's wife happy. She kept on putting leaves on her white eggs, and often the dew fell and wet the brown leaves as they lay on the eggs. In time the dew washed some of the color from the leaves. It dripped upon the white eggs and spotted them. |
So it came to pass that the turkey's eggs became spotted with brown. She no longer had to hide them, because they looked so much like dead leaves that old man turkey could not see them any more as he walked around in the woods. |
Next: Why the Dog's Ears Flop |
Take the 2-minute tour × |
What does it mean by a screen? I've had a look in the codex and don't understand (I'm a newbie). Thanks. |
share|improve this question |
add comment |
2 Answers |
up vote 2 down vote accepted |
$current_screen is a global variable in admin screens. It holds the result of WP_Screen::get( $hook_name )->set_current_screen();. |
WP_Screen is a class, get() is a static method that returns a real object – an instance of WP_Screen with some predefined properties: |
$screen->base = $base; |
$screen->action = $action; |
$screen->post_type = (string) $post_type; |
$screen->taxonomy = (string) $taxonomy; |
$screen->is_user = ( 'user' == $in_admin ); |
$screen->is_network = ( 'network' == $in_admin ); |
$screen->in_admin = $in_admin; |
$hook_name is a page specific variable for different admin pages. |
set_current_screen() pushes the object properties into the global variable. I consider this as a rather poor step. It is done for backwards compatibility, I guess. |
You can use $current_screen on plugin pages, in metaboxes and other places to get some context information. |
if ( $GLOBALS['current_screen']->is_network and is_super_admin() ) |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.