In the previous example, we have created an URL that linked to edit action of our Students controller and also passed the id of the student. To show the edit form, we need to get the data about that student first. Add this code to students_controller.php:
function edit() { // create an instance of student with id == $_GET['id'] $student = new Student($_GET['id']); // load our HTML helper (you might not need this line in future releases) require_once("taniphp/html.php"); // pass the object to the view $this->view->set("student", $student); }
Now we will create a view for our edit action. Following the conventions, the view will be saved in views/students/edit.php:
<h1>Editing student details</h1> <form action="?controller=students&action=save" method="post"> <input type="hidden" name="id" value="<?=$student->id?>"> <?php HTML::printForm($student); ?> <input type="submit"> </form>
The HTML::printForm()1) method generates an edit form being given the instance of our model class. The form is colored using tr.color1 and tr.color2 set in files/style.css. If you wish to futher customize your form, but maintain the usability of autogenerated fields, you should take a look at HTML::getForm() method that returns an array of tags that you can print using your own markup.
You should note that no input tags were created for created_on, updated_on fields. These are special fields and how the framework is treating them is described in the conventions section. As for the id field, you should add it yourself if you wish to create an edit form (the HTML class generates tags that could be used both in edit/new forms).
Add the save() method to students_controller.php file:
function save() { $student = new Student; // assign vars we got from the form $student->assign($_POST); // try to update the table if($student->update()) { // everything ok, show the list of students again Framework::redirect('students','index'); } else { // something went wrong Framework::croak("Could not save!"); } }