Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive /

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Pre-populating a table


Reply


4 Posts   1441 Views

Avatar
UncleCheese

Forum Moderator, 4094 Posts

6 June 2008 at 7:20am

Edited: 06/06/2008 7:21am

How can I make a Silverstripe module that will build a database table that comes pre-populated with several records? (A look-up table, for instance).

Avatar
UncleCheese

Forum Moderator, 4094 Posts

20 June 2008 at 4:34am

Any ideas on this? I know it can be done.

Avatar
Willr

Forum Moderator, 5513 Posts

20 June 2008 at 5:33pm

Have a look at the Blog and Forum modules - by default they create Holders and example posts / threads etc

/**
    * Create default blog setup
    */
   function requireDefaultRecords() {
      parent::requireDefaultRecords();
      
      if(!DataObject::get_one('BlogHolder')) {
         $blogholder = new BlogHolder();
         $blogholder->Title = "Blog";
         $blogholder->URLSegment = "blog";
         $blogholder->Status = "Published";
         
         $widgetarea = new WidgetArea();
         $widgetarea->write();
         
         $blogholder->SideBarID = $widgetarea->ID;
         $blogholder->write();
         $blogholder->publish("Stage", "Live");
         
         $managementwidget = new BlogManagementWidget();
         $managementwidget->ParentID = $widgetarea->ID;
         $managementwidget->write();
         
         $tagcloudwidget = new TagCloudWidget();
         $tagcloudwidget->ParentID = $widgetarea->ID;
         $tagcloudwidget->write();
         
         $archivewidget = new ArchiveWidget();
         $archivewidget->ParentID = $widgetarea->ID;
         $archivewidget->write();
         
         $widgetarea->write();
         
         $blog = new BlogEntry();
         $blog->Title = _t('BlogHolder.SUCTITLE', "SilverStripe blog module successfully installed");
         $blog->URLSegment = 'sample-blog-entry';
         $blog->setDate(date("Y-m-d H:i:s",time()));
         $blog->Tags = _t('BlogHolder.SUCTAGS',"silverstripe, blog");
         $blog->Content = _t('BlogHolder.SUCCONTENT',"Congratulations, the SilverStripe blog module has been successfully installed. This blog entry can be safely deleted. You can configure aspects of your blog (such as the widgets displayed in the sidebar) in [url=admin]the CMS[/url].");
         $blog->Status = "Published";
         $blog->ParentID = $blogholder->ID;
         $blog->write();
         $blog->publish("Stage", "Live");
         
         Database::alteration_message("Blog page created","created");
      }

Avatar
UncleCheese

Forum Moderator, 4094 Posts

21 June 2008 at 1:34am

Perfect. That's exactly the code I was looking for. Thanks.