Skip to content
Rosario Carvello edited this page Feb 10, 2018 · 18 revisions

Introduction

In an MVC software architecture, a model is a component that has the responsibility for data management. In other words, the model maintains a repository of data and provides the methods for data recording and retrieval. It is worthwhile to observe that the decomposition into the three components of an MVC architecture reflects the approach of divide et impera in which the controller assumes the role of coordinator that assigns the tasks of data management and data presentation to the model and view components respectively.

WebMVC Model

In WebMVC, the instantiation of a model is similar to that of a controller or a view; in fact, it is sufficient to extend the framework\Model class. As an example, we can further discuss the problem of showing a list of people in a browser. In the previous page, the list of users, actions, and roles were taken from the controller; while this could be convenient when the problem to solve is of small dimension (we could do without the model), it is more frequent the case where the data are managed by the model. So we can build a models\NestedBlocks in this way:

namespace models;

use framework\Model;

class NestedBlocks extends Model
{
    /**
     * Provides users data.
     *
     * @return array  Array of users in the
     *                format array( array('Username'=>'','UserEmai'=>'') )
     */
    public function getUsersData()
    {
        $users = array(
            array('UserName' => 'Mark', 'UserEmail' => '[email protected]'),
            array('UserName' => 'Elen', 'UserEmail' => '[email protected]'),
            array('UserName' => 'John', 'UserEmail' => '[email protected]')
        );
        return $users;
    }


    /**
     * Provides users actions.
     *
     * @return array Array of actions in the format:
     *                       array( array('ActionName'=>'','ActionCaption'=>'') )
     */
    public function getUserActions()
    {
        $userActions = array(
            array("ActionName" => "email" ,"ActionCaption" => "Send email"),
            array("ActionName" => "edit"  ,"ActionCaption" => "Edit information"),
            array("ActionName" => "erase","ActionCaption" => "Delete user")
        );
        return $userActions;
    }

    /**
     * Provides users roles.
     *
     * @return array Array of users roles in the format:
     *                       array ( array("UserEmail"=>'',"UserRoles"=>array(r1,r2...,rn)))
     */
    public function getUsersRoles()
    {
        $usersRoles = array(
                        array(  "UserEmail" => "[email protected]",
                                "UserRoles" => array("admin","webmaster","moderator")
                        ),
                        array(  "UserEmail" => "[email protected]",
                                "UserRoles" => array("editor","webmaster","user")
                        ),
                        array(  "UserEmail" => "[email protected]",
                                "UserRoles" => array("user","editor")
                        )
                    );
        return $usersRoles;
    }
}

By using the new class models\NestedBlocks, in which we coded methods for providing all the necessary data, we can now refactor the controllers\NestedBlocks by:

  • Adding a reference to models\NestedBlocks
  • Eliminating its previos methods getUsersData(), getUserActions(), and getUsersRoles() because they are now provided by models\NestedBlocks Se the code below:
<?php

namespace controllers;

use framework\Controller;
use views\NestedBlocks as NestedBlockView;
use models\NestedBlocks as NestedBlockModel;

class NestedBlocks extends Controller
{
    /**
     * @override framework\Controller __construct()
     */
    public function __construct()
    {
        $this->view = new NestedBlockView();
        $this->model = new NestedBlockModel();
        parent::__construct($this->view,$this->model);

        $actions = $this->model->getUserActions();
        $this->view->setUserActionsBlock($actions);

        $users = $this->model->getUsersData();
        $this->view->setUsersBlock($users);

        $usersRoles = $this->model->getUsersRoles();
        $this->view->setUsersRoles($usersRoles);
    }

    /**
     * Set the default behaviour when no actions is performed
     */
    protected function autorun($parameters = null)
    {
        $this->view->setVar("CurrentAction","Please, perform an action on user");
    }

    /**
     * Performs the given action on a given user.
     *
     * @param string $actionName The action to performs
     * @param string $userEmail The user email on which to perform the action
     */
    public function doAction($actionName,$userEmail)
    {
        $currentAction = "Current action: $actionName on user $userEmail";
        $this->view->setVar("CurrentAction",$currentAction);
        $this->render();
    }
}

We don't need to update the previuos views\NestedBlokcs and templates\nested_blocks.html.tpl because they are already decoupled from control and data logic.
So we can run again controllers\NestedBlocks by typing again http://localhost/nested_blocks to obtain the same result of the previous page:

Model

Summary

Clone this wiki locally