Models

Models are primarily used for managing and interacting with a corresponding database table, they extends the Qt_Model base class. The Quantum framework by default uses ORM Idiorm to simplfy interaction with database, but it's possible to use any other as well.

Below is an example of basic model class with some of its attributes:


namespace Modules\Main\Models;
use Quantum\Mvc\Qt_Model;


class User extends Qt_Model {
    
    public $idColumn = 'user_id';

    public $table = 'users';
    
    public $fillable = [
        'firstname',
        'lastname',
        'email',
        'pass',
    ];
}

        

With attribute $table we define a database table with which the model will be bound. The $idColumn defines the primary key of the table.


Using Models in Controller

Models can be initilized in controller using Qt_Controller modelFactory() method


        $userModel = $this->modelFactory('User');
        

By passing the name of the model to the modelFactory() method, it will try to load that model from the same module. But if it will not find that model there, then will look at the `base\models` driectory and will load it from there.

Models in `base\models` directory are accessable from all modules.

The Quantum framework has several wrappers on top of Idiorm which allowes easily interact with database.


Retriving data

There are several ways to get data from database.

Gets the record with id equal to 1


    $userModel->findOne(1);
        

Gets the record with category_id column equal to 2


    $userModel->findOneBy('category_id', 2);
        

Gets the first record from the dataset


    $userModel->first();
        

To get the models data as array you can use method asArray()


    $user = $userModel->asArray();
        

Retiriving a dataset from the database


    $users = $userModel->get();
        

Sometimes you might need to get data by several criterias, for that reason criterias() method comes handy, the method accept several arrays of the following structure:


    $users = $userModel->criterias(['profession', '=', 'mechanic'])->get();
        

The above code will return users whose profession is a mechanic. To get more spcific result we will add more criterias


    $users = $userModel->criterias(['profession', '=', 'mechanic'], ['age', '>', 35])->get();
        

The Quantum model supports the following operators for criterias() method

  • = Equal
  • != Non Equal
  • > Greater then
  • >= Greater or equal
  • < Less then
  • <= Less or equal
  • LIKE Like
  • NOT LIKE Not Like

There are several other useful methods that will return desired result set

The orderBy() method accepts array as an argument where the array's key is the table column name and the array's value is direction of order and can be asc or desc.


    $users = $userModel->criterias(['profession', '=', 'mechanic'])->orderBy(['date' => 'desc'])->get();
        

The groupBy() method is grouping result set by given column.


    $users = $userModel->criterias(['profession', '=', 'mechanic'])->groupBy('class_id')->get();
        

The limit() method limits number of results in result set by given.


    $users = $userModel->criterias(['profession', '=', 'mechanic'])->limit(25)->get();
        

The offset() method skips a number of results from result set.


    $users = $userModel->criterias(['profession', '=', 'mechanic'])->offset(10)->get();
        

Sometimes you don't need to get the result set, but just the count of result set, for that purposes the method count() will be used.


    $users = $userModel->criterias(['profession', '=', 'mechanic'])->count();
        
Creating, Updating, Deleting

To create a new database record you will need to follow to this scheme:


$user = $userModel->create();
$user->firstname = 'John';
$user->lastname = 'Doh';
$user->profession = 'Writer';
$user->age = 52;
$user->save();
        

The create() method tells to ORM that new record will be created, so the variable $user contains the model, which we are filling in next several lines. The save() method saves the model's changes to the database.

To update an existing record in database, we should do the following:


$user = $userModel->findOne(1);
$user->firstname = 'Henry';
$user->lastname = 'Doh';
$user->profession = 'Actor';
$user->age = 46;
$user->save();
        

First we finding the user by given id then starting to change some of its attributes and then saving the changes to the database by calling save() method at the end.

In order to not assign values to each properties of the model, you can pass them as an array via fillObjectProps() method. Make sure to have the $fillable attribute in your model where you define the table columns that should be filled by fillObjectProps() method.


    $userModel->fillObjectProps([
    'firstname' => 'John', 
    'lastname' => 'Doh', 
    'email' => 'john@doe.com'
    'pass' => 'supersecretpassphrase'
]);
        

To delete a record from the database we call delete() method of the model.


$user = $userModel->findOne(1);
$user->delete();
        

To do more complex querying you will need to use Idiorm's functionality or write raw queries in your modeles.

<< Prev Next >>