Helping ordinary people create extraordinary websites!

Zend Framework Tutorial

By Lyndon Baptiste
2008-08-13

Back To The Controller

Assuming our validation went okay, we have to cater for the form that must be saved. Let's look at the modified saveAction and then discuss the elements (I'm assuming that this code is pasted into your editor from page 28 of the IndexController therefore line numbers in such manner:

public function saveAction() {
require_once './application/modules/default/models/IndexForm.php';
$form = IndexForm::registerForm();

//handle if form is invalid
if (!$form->isValid($_POST)) {
$this->view->form = $form;
$this->render('register');
return;
}

//get form values
$v = $form->getValues();

//include model
require_once 'models/User.php';
$user = new User();

$data = array(
'email' => $v['email'],
'password' => $v['password'],
'gender' => $v['gender']
);
$user->insert($data);
$this->render();
}

If the boolean expression on line 33 returned true, control would have jumped to line 40 where the filtered values of the form would have been retrieved using the method getValues. To prevent hacking sql together for the insert the programmer can
create an associative array which maps to the database field and through an instantiated User object make a call to the insert method passing the array as a parameter as shown on line 51. What joy! The insert method handles quoting of any data in the array therefore this doesn't need to be explicitly done. Thereafter a call to the render method will display a script congratulating the user on their registration. As a small test create a such phtml script. Remember the rules and conventions!

It will be a good idea in such actions as the one above to test whether the request was indeed a post. This can be done via a call to the isPost method of the request object and is accessible through the action controller through $this->getRequest(). Therefore testing whether something is a post can be done as follows if (!$this->getRequest()->isPost()). If the expression results in false the programmer can forward the script to a preferred location.





Tutorial pages:
 11 Votes

You might also want to check these out:


Leave a Comment on "Zend Framework Tutorial"
You must be logged in to post a comment.

Link to This Tutorial Page!


GET OUR NEWSLETTERS