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.

We're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

best way to create default records

Go to End

2 Posts   1329 Views


Community Member, 48 Posts

23 September 2012 at 2:25am

I'm currently working on a fairly basic project tracking app. I'm using model admin and dataobjects. Each project can have many deliverables. I'm managing this with dataobjectmanager. We've now decided that each project should have a default set of deliverables (probably around 15) in addition to these other deliverables.

I'm thinking that I'll create another dataobject defaultdeliverables that contains the records I want added. Then in project create a field defaultsadded and if it hasn't been set get the defaultdeliverables and write them to deliverables including the current project id. I'm guessing I'll need to iterate over them and write them one by one.

Does this sound like the way to go? I'm asking this because every time I read the forums I think I know how to go about something and someone posts a much better and easier way of doing it.

Thanks in advance,


Community Member, 254 Posts

28 September 2012 at 11:37pm

There are two functions for defaults that I find useful.


Populate Defaults is great for get setting things like time stamps on the object, and other variables if not otherwise supplied

public function populateDefaults() {
$this->Date = date('Y-m-d H:i:s');

But there is a problem when you want to add default relationships to the object, because it hasn't saved yet you can't use the ID for reference.
It ain't pretty but I add a new boolean var in the DB array something like BuildDefault.

static $db = array(
'BuildDefault' => 'Boolean'

static $defaults = array(
'BuildDefault' => 1

public function onAfterWrite() {
if( $this->BuildDefault ) {
$wireframe = new ProjectStage();
$wireframe->Title = 'Sitemap';
$wireframe->ProjectID = $this->ID;
$wireframe = new ProjectStage();
$wireframe->Title = 'Wireframe';
$wireframe->ProjectID = $this->ID;
$wireframe = new ProjectStage();
$wireframe->Title = 'Concept';
$wireframe->ProjectID = $this->ID;
$this->BuildDefault = 0;

I don't like it as it makes a new column in the db which you only use once.
Looks like we're working on a similar thing, I'm building a client portal to allow stage sign off on projects. Let me know how you go, perhaps we could share ideas.