Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Pre-populating a table

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

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

Page: 1
Go to End
Author Topic: 1362 Views
  • UncleCheese
    Avatar
    4085 Posts

    Pre-populating a table Link to this post

    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).

  • UncleCheese
    Avatar
    4085 Posts

    Re: Pre-populating a table Link to this post

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

  • Willr
    Avatar
    Forum Moderator
    5464 Posts

    Re: Pre-populating a table Link to this post

    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 the CMS.");
             $blog->Status = "Published";
             $blog->ParentID = $blogholder->ID;
             $blog->write();
             $blog->publish("Stage", "Live");
             
             Database::alteration_message("Blog page created","created");
          }

  • UncleCheese
    Avatar
    4085 Posts

    Re: Pre-populating a table Link to this post

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

    1362 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.