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've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

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

Adding db data through php


Go to End


7 Posts   969 Views

Avatar
ismooth

Community Member, 25 Posts

15 January 2012 at 1:34am

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?

Avatar
danzzz

Community Member, 175 Posts

15 January 2012 at 1:53am

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
}

Avatar
ismooth

Community Member, 25 Posts

15 January 2012 at 3:43am

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?

Avatar
martimiz

Forum Moderator, 1391 Posts

15 January 2012 at 6:40am

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

Avatar
Willr

Forum Moderator, 5523 Posts

15 January 2012 at 8:51pm

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();
}
}

Avatar
martimiz

Forum Moderator, 1391 Posts

15 January 2012 at 11:19pm

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

martine

Avatar
ismooth

Community Member, 25 Posts

20 January 2012 at 11:55pm

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