21278 Posts in 5728 Topics by 2599 members
|
Page:
1
|
Go to End | |
| Author | Topic: | 322 Views |
-
Adding db data through php

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?
-
Re: Adding db data through php

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
} -
Re: Adding db data through php

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?
-
Re: Adding db data through php

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
-
Re: Adding db data through php

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();
}
} -
Re: Adding db data through php

15 January 2012 at 11:19pm
I stand corrected... thati absolutely is a good option and I just plain missed it
martine
-
Re: Adding db data through php

20 January 2012 at 11:55pm
Just tried it out, and works like a charm!! Thanks guys!
| 322 Views | ||
|
Page:
1
|
Go to Top |



