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.

Archive

Our old forums are still available as a read-only archive.

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

Adding user fields + Register function + Admin area SLOW


Reply

18 Posts   4991 Views

Avatar
Willr

16 October 2008 at 1:01am Forum Moderator, 5511 Posts

Yes. Have a read of the tutorials and they will cover some examples of adding 'DataObjects' esp the forms one - #3.

Avatar
Piklets

16 October 2008 at 10:44am Community Member, 36 Posts

In that tutorial the submissions are added to the database once by each person, then retrieved as a whole.

Is it possible to save the users ID in the submission, so it would be possible to view the results of a single user?

$submissions = DataObject::get('BrowserPollSubmission');
That gets them all. Wouldn't it be slow if there were many fields, you only needed to view one users set of answers, and there were thousands of sets?

Avatar
Willr

16 October 2008 at 12:17pm Forum Moderator, 5511 Posts

I fully recommend you read the docs (as painful as they are) before you post. DataObject::get() has a number of parameters which you can use to filter down. Eg

DataObject::get(Object, SQL Filter Statement, Sort Option, Join Fields, Limit on how many to return);

And you can leave off any of the last 4 or pass "". So to get all the submissions from a user ID...

DataObject::get("Submission", "UserID = '$SomeID'");

UserID is the field on the Submission Object and $SomeID is a number :)

Avatar
Piklets

16 October 2008 at 12:40pm (Last edited: 16 October 2008 12:40pm), Community Member, 36 Posts

I can't find any documentation about this...

http://api.silverstripe.com/ - is it in there? Or somewhere in http://doc.silverstripe.com/doku.php

Avatar
Willr

16 October 2008 at 12:50pm Forum Moderator, 5511 Posts

http://doc.silverstripe.com/doku.php?id=datamodel is a useful page

Avatar
Piklets

16 October 2008 at 1:07pm Community Member, 36 Posts

So I could do this:

function doform($data, $form) { //what is the difference between data and form?
if(Member::currentUserID()){
$myPlayer = new Player();
$myPlayer->UserID = Member::currentUserID();
$myPlayer->Firstname = $form->Firstname; // or $data->Firstname
$myPlayer->Surname = $form->Surname; // or $data->Surname
$myPlayer->write();
} else {
// Not logged in
}
}

Avatar
Willr

16 October 2008 at 1:49pm Forum Moderator, 5511 Posts

Yes you are on the correct track. Though the $data is not an object it is a standard php array of data where the key is your field name so if you have a TextField('Firstname') the value in the submitted function would be $data['Firstname'].

The other thing you can do is if your field names in your form match the fields names in the dataobject eg your 'Player' has Firstname you can call a method called saveInto() which is a feature on the form. It will take all the data from $data and try and save into the dataobject

function doform($data, $form) {
$player = new Player();
$player->UserID = Member::currentUserID(); // save the ID
$form->saveInto($player);
$player->write();
}

Avatar
Piklets

16 October 2008 at 11:16pm (Last edited: 17 October 2008 12:56am), Community Member, 36 Posts

Thanks for all of the help!

-- With some midnight coding and some trying it out for myself, I have solved the first two of my problems. Now for some sleep =D

I have come up with this:
(you need code highlighting)
http://pastebin.com/f5e75d403
- That is my form - it handles the display of the form and the submission.

And I have my database PHP file:
class MedCheck extends DataObject {
static $db = array(
"UserID" => "Int",
"Field1" => "Text",
"Field2" => "Text",
"Field3" => "Text"
);
}

At the moment I have three problems.

The main problem is that I'm not sure how to output data from DataObject::get().
At the moment I have:

function ViewInfo() {
$info = DataObject::get_one("MedCheck", "UserID = '1'");
foreach($info as $key => $value){
echo "$key: $value";
}
}

Which isn't working. It comes up with "0: MedCheck" at the top of the page, not with the rest of the content.

EDIT: echo $info->Field1; seemed to work, building on that...
EDIT: return instead of echo works perfectly!

EDIT: woot! progress!

function ViewInfo() {
$info = DataObject::get_one("MedCheck", "UserID = '" . Member::currentUserID() . "'");
return "<p><b>Information for user $info->UserID</b><br />
$info->Field1<br />
$info->Field2<br />
$info->Field3</p>";
}

EDIT: got that sorted. Now I need the drop down list.

The second problem is that I'm not sure how to have a drop down list of all of the users, with the values of the drop down list as the corresponding user ID. Then use the submission of that to show information about that user (by passing the corresponding user ID to another function, not put the submission into the database).

EDIT: WOOT even more progress! I have the drop down menu working, but I can't do anything with the values yet. Here it is - could you see why my doform shows absolutely nothing?
http://pastebin.com/f55ed1573

EDIT: nuuu.. i took data as an object not an array. Still need help with sending the form data to another page to use in php.

$this->customise($data)->renderWith(array('Page_results', 'Page'));
I'm guessing that won't work because it passes it for use in the .ss page, not the .php page.

EDIT (yet again): I worked it out! I have done it! Woot!
Hopefully using the GET method is acceptable (with the security: (int)$_GET[...)
(midnight coding lol)
Now on to the next problem:

The third problem is I want only one submission allowed for each user, and if the user submits again it updates the previous submission. I saw in the datamodel documentation it talked about 'relations' with the 'has_one' array. How would I implement that?