use SilverStripe\ORM\DataObject; class SiteConfig extends DataObject { private static $db = [ 'Title' => 'Varchar(255)', 'Tagline' => 'Varchar(255)' ]; public static function requireDefaultRecords() { parent::requireDefaultRecords(); $siteConfig = self::get()->first(); if (!$siteConfig) { $siteConfig = SiteConfig::create(); $siteConfig->Title = 'My Site'; $siteConfig->Tagline = 'Welcome to my site!'; $siteConfig->write(); } } }
use SilverStripe\ORM\DataObject; use SilverStripe\CMS\Model\SiteTree; class HomePage extends SiteTree { private static $db = [ 'FeaturedTitle' => 'Varchar(255)', 'FeaturedContent' => 'HTMLText' ]; public static function requireDefaultRecords() { parent::requireDefaultRecords(); $homePage = HomePage::get()->first(); if (!$homePage) { $homePage = HomePage::create(); $homePage->Title = 'Home'; $homePage->Content = 'Welcome to my homepage!'; $homePage->FeaturedTitle = 'Featured Content'; $homePage->FeaturedContent = 'In this example, a HomePage data object is defined as a subclass of SiteTree, which represents a page in the site hierarchy. During the requireDefaultRecords() call, a check is performed to see if a home page record exists. If it does not exist, a new record is created with default values and published to the live site. These examples show how requireDefaultRecords() can be used to automatically create default records for pages, site configurations, or any other kind of data stored in the database. This method is part of the SilverStripe ORM library, which is included in the SilverStripe web application framework.This is some featured content.
'; $homePage->writeToStage('Stage'); $homePage->publish('Stage', 'Live'); } } }