Jump to:

23370 Posts in 18160 Topics by 2864 members

General Questions

SilverStripe Forums » General Questions » best way to create default records

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

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

Page: 1
Go to End
Author Topic: 593 Views
  • woodb2
    Avatar
    Community Member
    48 Posts

    best way to create default records Link to this post

    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,
    Brian

  • Bambii7
    Avatar
    Community Member
    254 Posts

    Re: best way to create default records Link to this post

    There are two functions for defaults that I find useful.

    populateDefaults()
    and
    onAfterWrite()

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

    public function populateDefaults() {
    parent::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() {
    parent::onAfterWrite();
    if( $this->BuildDefault ) {
    $wireframe = new ProjectStage();
    $wireframe->Title = 'Sitemap';
    $wireframe->ProjectID = $this->ID;
    $wireframe->write();
    $wireframe = new ProjectStage();
    $wireframe->Title = 'Wireframe';
    $wireframe->ProjectID = $this->ID;
    $wireframe->write();
    $wireframe = new ProjectStage();
    $wireframe->Title = 'Concept';
    $wireframe->ProjectID = $this->ID;
    $wireframe->write();
    $this->BuildDefault = 0;
    $this->write();
    }
    }

    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.

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