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.

General Questions

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

Willr, Where Are You? (Can Rename To: "Custom Form and Database Interaction Problems")


Reply

2 Posts   651 Views

Avatar
SilverFox55

28 June 2013 at 11:18pm (Last edited: 28 June 2013 11:28pm), Community Member, 2 Posts

[Don't want to discourage anyone else from dropping input, but (let's face it) Willr's answers are guaranteed to be impeccable.]

After thoroughly following the tutorials and reading and re-reading the official book, I am still having trouble understanding the difference in functionality between the model and controller portion of the MyPage.php file. I do understand that the model handles the content and the controller handles the processing of data (as per http://doc.silverstripe.org/framework/en/tutorials/2-extending-a-basic-site). Intuitively, it would seem that only the form information and attributes should go in the model portion and the submit function should go in the controller. After viewing numerous code snippets, it seems to be possible to do it either way (but this is the most common). Also having trouble understanding proper DataObject creation. Should it be in its own file for permanent reference, or is it supposed to be declared in the Code/MySite.php file (which seems more temporary, so-to-speak).

The Final Goal: Have a 'userprofile' table to store individual user information and a 'profiledatabase' table to nest the 'userprofile' tables into (much like the ArticlePage and ArticleHolder in the aforementioned tutorial link). The form should then create a 'userprofile' object, write the form information into the corresponding attributes of the 'userprofile' object, and then write that 'userprofile' table/object into a new row of the 'profiledatabase' table/object. I'm thinking I might also need to store the username as a separate field in the 'profiledatabase' for profile lookup, assuming I store the whole 'userprofile' object in the 'profiledatabase' as its own attribute (please confirm). Otherwise, the 'userprofile' attributes would need to be written across the columns of the row for that particular 'userprofile'. Before this post gets any longer, please correct any incorrect assumptions (or confirm correct ones) that I have made in the previous paragraphs and then take a look the actual code. The form submit result is posted after the code,

Now to the implementation:

*Note: This is a mix of code inspired from both the tutorial previously mentioned as well as material found in the Official SilverStripe Manual (complete Guide to CMS Development) from pages 146-150.

1: Database Creation

--> mysite/code/UserProfile.php

<?php

class UserProfile extends DataObject
{
static $db = array(
'FirstName'=>'Varchar(30)',
'MiddleName'=>'Varchar(30)',
'LastName'=>'Varchar(30)');
}
?>

--> mysite/code/ProfileDatabase.php

<?php

class ProfileDatabase extends DataObject
{
//Not sure about this yet
}
?>

2: UserForm.php Page/Controller

--> /mysite/code/UserForm.php

<?php

class UserForm extends Page
{

function Form()
{
$fields = new FieldList(
new TextField('First Name:'),
new TextField('Middle Name:'),
new TextField('Last Name:'));

$actions = new FieldSet(new FormAction('submitUserForm', 'Submit'));

$validator = new RequiredFields('First Name', 'Last Name');

$form = new Form($this, 'Form', $fields, $actions, $validator);

return $form;
}
}

class UserForm_Controller extends Page_Controller
{

function submitUserForm($data, $form)
{
$userprofile = new UserProfile();
$form->saveInto($userprofile);
// Almost certain this next line is way wrong
$userprofile->UserRegistrationFormID = $this->dataRecord->ID;
$job->write();
$form->sessionMessage('Form Submitted Successfully', 'good');
return;
}
}
?>

3: UserForm.ss Layout

--> /themes/mytheme/templates/layout

<div id="User Form">
<h2>User Form</h2>
$Form
</div>

CANNOT GET MUCH SIMPLER THAN THIS.

Current Result:

The form displays correctly when called for and the database tables seem to appear created in phpmyadmin, but when clicking submit, I am redirected to the page:

Page not found - Sorry, it seems you were trying to access a page that doesn't exist. Please check the spelling of the URL you were trying to access and try again.

--and the tables displayed in phpmyadmin are not updated with the information in the form.

"Willr" you please work your magic? Sorry about the formatting, the indentations aren't appearing.

Avatar
Willr

1 July 2013 at 6:33pm Forum Moderator, 5511 Posts

Ha well I'm glad I can help. In the process of producing new documentation and how-to instructional videos for a couple developers so they will be released to the community at some point. Know documentation is tight but we're a lot better at it than a couple years ago!

IRC (if you get sometime for UT-12 timezone guys) is also a great place for discussions

Let me try and answer your questions one by one


Also having trouble understanding proper DataObject creation. Should it be in its own file for permanent reference, or is it supposed to be declared in the Code/MySite.php file (which seems more temporary, so-to-speak).

You can define classes anywhere you want. Put everything in 1 big site.php file if you want. BUT SilverStripe convention (and practise) will recommend 1 file per classname (i.e MyDataObject.php, MyPageType.php).

For your code, you have a number of things missing. It's best to work on solving things one step at a time but a few things that have caught you out.

1) You need to state that your form action is an allowed action on the controller.

class UserForm_Controller extends Page_Controller {
static $allowed_actions = array(
'Form'
);
..

2) Saving the form looks like you've copied a bit too much, should look like

$profile = new UserProfile();
$form->saveInto($profile);
$profile->write();

Not completely sure I understand the need for profiledatabase , userprofile database. ArticlePage, ArticleHolder is different, it's used to describe parent relationships. In your application I figure you'd have a single page UserForm.php which creates UserProfile objects (DataObjects). You may also then have another pagetype UsersPage which outputs the content of the UserProfiles on the page but you wouldn't need to setup any relationships.