Jump to:

22993 Posts in 11595 Topics by 2824 members

General Questions

SilverStripe Forums » General Questions » Adding db data through php

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: 454 Views
  • ismooth
    Avatar
    Community Member
    20 Posts

    Adding db data through php Link to this post

    It's pretty straightforward to build up database, prepare site structure and so on with SS, but I was wondering weather is possible to fill in some data, e.g. add some users, security groups, maybe even some pages? I'm especially interested in adding security groups.

    Some further explanation: If I by some accident lose my database, i just run dev/build and db will be built (or say prepared) from scratch. But contents will be empty. Since I'm building a user registration form, I want it to add users to a specific security group, but what if that group doesn't exist? Can I prepare (or in db sence build) that group (only once, ofcourse) so that if I only pass that php file to someone else, everything work out of the box?

  • danzzz
    Avatar
    Community Member
    175 Posts

    Re: Adding db data through php Link to this post

    hi

    first you save your member, then add the member to a group ...

    // code to save member here

    $Member = new Member();
    $form->saveInto($Member);
    $Member->write(); // you should do this in try block

    // check if group exists

    $userGroup = DataObject::get_one('Group', "Code = 'Mitglieder'");
    if($userGroup) {
       $userGroup->Members()->add($Member);
    } else {
    // report error or craete group
    }

  • ismooth
    Avatar
    Community Member
    20 Posts

    Re: Adding db data through php Link to this post

    that's okay, but if group doesn't exist, how do I create it (from php script)? Furthermore, how do I create it only once, and that's when I build my database?

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Adding db data through php Link to this post

    For something like that, you'd probably have to somehow extend the /dev/build functionality. since that is outside core, you might lose that as well some dark day In that case you might just as well backup your database - and use that if you ever need to install again... Or am I missing something?

    I always use the database backup of a complete install for a certain type of site, to reinstall whenever I want...

    Cheers, martine

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Adding db data through php Link to this post

    You should use the requireDefaultRecords() function on a DataObject subclass (i.e Page) for this. You can see on SiteTree.php this function is used to populate the default db. Same with blog / forum. The function is called whenever you run dev/build

    Example:
    function requireDefaultRecords() {
    parent::requireDefaultRecords();

    if(!DataObject::get_one('Group', "Code = 'foo'")) {
    $group = new Group();
    $group->Code = 'foo';
    $group->write();
    }
    }

  • martimiz
    Avatar
    Forum Moderator
    1035 Posts

    Re: Adding db data through php Link to this post

    I stand corrected... thati absolutely is a good option and I just plain missed it

    martine

  • ismooth
    Avatar
    Community Member
    20 Posts

    Re: Adding db data through php Link to this post

    Just tried it out, and works like a charm!! Thanks guys!

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