Quantum PHP Framework — MVC Flow

In this series of tutorials we will explore Quantum Framework and create a project with step by step.
So far we get familiar with Quantum framework base concepts and learn how to install it, now it’s the time to get involved a bit more and investigate Controllers, Models and View (the MVC flow) and write some code.
As you already know Quantum comes with single module called Main, which is defined in base config/modules.php, so let’s get started with that module..
Routing
First let’s check the routes, which is inside Config directory, we will find a single route there:
return array(
array ('', 'GET', 'MainController', 'indexAction')
);Controller and View
The route tells us that indexAction will be called in MainController on HTTP GET method, when URI contains no additional parameters, i.e just on the main domain.
In modules/Main there is one controller called MainController which extends Qt_Controller and contains several methods,
- __construct()
- __before()
- __after()
- indexAction()
The __construct() is a constructor of MainController and uses setLayout() call to set the layout for page
$this->setLayout(‘layouts/main’);
As you can guess from the argument passed, the layout file is laid under views/layouts and called main.php
__before() and __after() are special methods of framework, which are called before or after any action called respectively.
We will ignore them for now and will go to the method indexAction(), where we see render() method
$this->render(‘index‘);
The render() method renders the file views/index.php file. If we look at main layout file, we will see view() function call
<?php echo view() ?>
Which will echo out the rendered content.
Model
Now let’s investigate the model. In the Models directory Quantum comes with simple User model which extends Qt_Model. But before going forward let’s create our user table, which will have the following structure:

Now let’s back to our model and explore more.
public $idColumn = 'user_id';$idColumn propery identifies which is the primary key of the table.
public $table = 'user';$table property defines the table, with which table model will work.
public $fillable = [
'firstname',
'lastname',
'email',
'pass'
];$fillable array contains table columns that can be filled with fillObjectProps() model method (this will be discussed later).
Create, Read, Update, Delete
Now let’s write some code and try to communicate with database using our model and then render them into the view.
As you might already noticed MainController uses model User at the top with other classes, so the User model is accessible in our MainController.
use Modules\Main\Models\User;
In order to use the model we should create instance, but it should be done through modelFactory() method and not called directly. The good place to it, is in constructor.
$userModel = $this->modelFactory(‘User’);
Ok, let’s create a record in user table.
$userModel->create();
$userModel->firstname = 'John';
$userModel->lastname = 'Doe';
$userModel->email = 'johndoe@gmail.com';
$userModel->pass = md5('topsecret'); // We use md5() for simplicity $userModel->created_at = date('Y-m-d H:i:s');
$userModel->save();That’s it, the user John Doe is now recorded to our database. To verify he is there, let’s try to fetch him (assuming the table was clear and the John Doe goes as a first record).
$userModel->findOne(1); // Where 1 is the Id of the record
$user = $userModel->asArray();Now the variable $user will contain array of user information:

In order to pass the data to index view, we will use render method by sending the data as a second parameter.
$this->render(‘index‘, $user);
Now, let’s try to update some of the fields of the record:
$userModel->findOne(1);
$userModel->email = 'johndoe@yahoo.com';
$userModel->save();As you can see, very simple, we again just find the user by id, and changed some property (the email in this example) and then saved it, so now our database table contains updated version of record.
The last operation we will do, is demonstrate the deletion of record, which is done by following way:
$userModel->findOne(1);
$userModel->delete();Wow, super simple, the record no longer exists in our table.
This is very basic approach to using CRUD (Create, Read, Update, Delete) with Quantum framework, to learn more and go deeper, continue follow to new posts.
Originally published at blog.softberg.org on September 2, 2018.