The Model - Zend

After the user has entered into the action inside the controller, the second step the controller takes is to process any information stored in any necessary models. Unlike the controller, the model exists to help the developer decouple business logic from the< application. Domain logic, or logic and rules that are specific to the application, will reside inside the model. In most cases, the model consists of scripts that connect to a database and processes any number of create, read, update, delete (CRUD) commands. Nothing besides business logic resides here to consider the script a model and loosely coupled with the other components of the MVC pattern.

Let’s look at another example of what might be inside one of the models. If you created an application that required the user to fill out a form and submit, as shown in Figure, the application will initialize the submitted data inside the action, but will save the information to a database inside a model.

This expands on the previous figure, in which you took a look at how the user’s event triggers the appropriate action in a controller. In the following figure, after the user has successfully submitted the form information for signup, the example is expanded by introducing the model into the controller’s action.

In this example, the model will receive all the user’s information sent in by the controller and save it into a database. If there are no errors, the model will return some type of value back to the controller to inform it that the data is processed. After that happens, the user is sent a message that all the data has been successfully processed.

MVC controller and model

The model allows you to decouple the business logic from both the controller and the view. Decoupling the process from the controller by placing it inside the model will allow the developer to use the code in not only this particular application but also in a new application. Using PHP, you’ll create a simple model for saving account information that is reusable and can be called from the controller.

Listing SaveAccount.php

<?php
/**
* Model - Save the data into the DB.
* This code base can be used in other applications that need
* data to be stored for new users.
*
**/
class SaveAccount {
/**
* Save Account
*
* @param String $username
* @param String $password
* @param String $email
*/
public function saveAccount($username, $password, $email){
//Clean up data
$username = $this->_db->escape($username);
$password = $this->_db->escape($password);
$email = $this->_db->escape($email);
//Set up mysqli instance
$dbconn = mysqli('localhost',
'<Your Username>',
'<Your Password>');
$dbconn->select_db('loudbite');
//Create the SQL statement and insert.
$statement = "INSERT INTO Accounts (username, password, email)
VALUES ('".$username."',
'".$password."',
'".$email."')";
$dbconn->query($statement);
//Close db connection
$dbconn->close();
}
}

The model you just created is a crude implementation that uses the mysqli to connect to a database, cleans the data passed into the function, and then saves the data into the database. To incorporate the model into the controller, let’s update the AccountController successAction().

Listing Updates to the AccountController’s successAction()

//Action #1 - Sign up for an account form.
public function successAction(){
//Check if the submitted data is POST type
if($this->_request->isPost()){
$email = $this->_request->getParam("email");
$username = $this->_request->getParam("username");
$password = $this->_request->getParam("password");
//Initiate the SaveAccount model.
require_once "SaveAccount.php";
$SaveAccount = new SaveAccount();
$SaveAccount->saveAccount($username, $password, $email);
}else{
throw new Exception("Whoops. Wrong way of submitting your information.");
}
}

The first few lines in successAction() retrieve any data the user has submitted as a POST. This example assumes that the form has three fields: a username, a password, and an e-mail address. After the data is fetched, you initialize the SaveAccount model and save the three fields by calling saveAccount().


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Zend Topics