I'm new to PHP and therefore SS too. So forgive if this is entry level stuff.
I have worked through the tutorials and now I am trying my own stuff, setting myself some tasks.
I have defined a page that I use to search for students and then bring up and edit form. The page is working fine. But I am convinced the code could be better.
See my comment in the "doUpdateStudent" method. This explains the bit I am trying to improve.
Help, comments, much appreciated.
/ EDIT STUDENT RECORDS.
// Student Edit form
function StudentEdit() {
Us
///Get studentdata for specified ID in url.
$student = DataObject::get_by_id('Student',$_GET['id']);
$fields = new FieldSet(
new HiddenField("ID","","$student->ID"),
new TextField("FirstName","First Name","$student->FirstName"),
new TextField("Lastname","Last Name","$student->Lastname"),
new TextField("Nationality","Nationality","$student->Nationality")
);
$actions = new FieldSet(
new FormAction('doUpdateStudent','Save'),
new FormAction('StudentEditCancel','Back to list')
);
$validator = new RequiredFields('FirstName','Lastname','Nationality');
return new Form($this,'StudentEdit',$fields,$actions,$validator);
}
// Returns the previous page.
function StudentEditCancel(){
Director::redirect('.');
}
// Update student record
function doUpdateStudent($data,$form) {
// get record.... again!!!! <<< this is the bit that I think I should not have to do
$student = DataObject::get_by_id('Student',$data['ID']);
// Update object values with current form values
$student->FirstName=$_POST['FirstName'];
$student->Lastname=$_POST['Lastname'];
$student->Nationality=$_POST['Nationality'];
// Write out record to table.
$student->write();
Director::redirectback();
}
}