PHP Models

Models provide a means of interacting with a database.

Types of Models


MogulMVC includes 2 different types of models.

MPDO models are the basic model type. They provide no abstraction and allows maximum control over you CRUD functions. It is also the faster of the two models.

MactiveRecord models are the advanced model type. They provide a robust abstraction layer for your model. These models are forks of the PHPActiveRecord project. They have been integrated with the MogulMVC database configuration.

Create a Model


MPDO

To create a PDO model you first make a class file named after your table followed by Model.php. This file would be placed in the backend model directory. For example if your table is called users then your model would be UserModel.php and be placed in the model directory.

/backend/
	/model/
		/UserModel.php
/frontend/
/.htaccess

You must load the 'core/MPDO' library from your autoload or from within the model class.

Inside this model you will create all your functions to interact with the database. These functions should be public functions and are commonly refereed to as CRUD, because they will do one of four things, Create, Read, Update, and Delete.

Below is an example of a read function.

public function read_by_id($id) {
	
	$db = MPDO::connection(0);

	$query = $db -> prepare('
		SELECT * from user
		WHERE id = ?');
		
	$query -> execute(array($id));

	$object = $query -> fetch();

	return $object;

}

Notice how we use MPDO::connection(0); on the first line of the function. MPDO::connection(0); will return a PDO object connected to the 0th database in your database.php file within the config directory.

Imagine your 0th entry in your database.php file is called 'user_connection'. You can also connect to this table with the following code.

MPDO::connection('user_connection');

MogulMVC will understand that you are looking for a specifically named index within the $GLOBALS['DB']; array.

Also, MogulMVC will only connect once to each database you need so as to minimize the amount of concurrent connections to the database.

All your model's methods should accept input and return a value. We recommend the following.

  • Create should accept an object to be added to a database. They return the object added to the database including the id or false if the object was not added.
  • Read should accept some means of finding the row. They return an object, or false if no object was found.
  • Update should accept an object to be updated. They return the updated object or false if the row was not updated.
  • Delete should accept some means of finding the row to delete. They return true if the row was deleted and false if there was an error.
  • MActiveRecord

    MActiveRecord models are models based on the Active Record pattern. They are based on the PHPActiveRecord project, but add some automatic configuration capabilities.

    Like MPDO models MActiveRecord models are named after your table. If your table is 'user' you would again name your model UserModel.php. However, unlike MPDO models you don't have to add any CRUD functions as these will be created dynamically by the MActiveRecord class.

    You need to load the core/db/MActiveRecord library in either the autoload.php file, or within the model class itself. After this your model must extend MActiveRecord. The final thing you will need to do is specify the table the model should connect to. Below is an example of a MActiveRecord model.

    <?php
    
    MLoad::php_framework('core/db/MActiveRecord');
    
    class UserModel extends MActiveRecord {
    
    	public static $table = 'user';
    
    }

    To learn more about how to write MActiveRecord models see the PHPActiveRecord project's documentation.

    MPDO vs MActiveRecord

    When should you use MPDO models over MactiveRecord models?

    MActiveRecord models are very automated and easy to write as all the SQL is written dynamically by the MActiveRecord class. Such a level of automation and ease of use compromises performance. MActiveRecord can be 8X slower than MPDO models and depending on the amount of rows being retrieved from the database can be much more memory intensive. However, MActiveRecord models are substantially faster to write and develop with. For this reason we recommend using MActiveRecord models for rapid development of prototypes. Once an application is working and more speed is required you should change your models to MPDO models.

    MPDO models use the PDO driver to connect to databases and retrieve data. This is the only level of abstraction as all the SQL is written by you. This makes MPDO models a lot faster than MActiveRecord models, but less intuitive.

    Load a Model


    Loading a model into memory is very simple. To load a model you use the MLoad class and the model function. MLoad will look in the model directory for your model and load it into memory.

    To load a model called UserModel.php do the following.

    MLoad::model('UserModel');

    Notice that we don't have to specify an extension as MogulMVC assumes models are PHP models.

    After loading a model you can then make a new instance of it and access the database using the model's functions.

    For an MPDO model you would do the following.

    MLoad::model("UserModel");
    $user_model = new UserModel();
    $user_object = $user_model -> read_by_id(2);

    An MActiveRecord model would do the following.

    MLoad::model("UserModel");
    $user_object = UserModel::find_by_id(2);

    Use a Model


    Using a model varies based on the type of model you are using. The following will cover MPDO models. If you want to learn how to use an MActiveRecord model please see the PHPActiveRecord documentation as it outlines the use of Active Record models better than we can.

    As stated above, to use a model you must first load it. This is done with the MLoad class and the model function.

    MLoad::model('UserModel);

    After loading the model you have can create a new instance of the model.

    $user_mode = new UserModel();

    As all functions in MPDO models are public function you can access them as you would any other method within a class.

    Imagine you have a model called UserModel which contains a function called read_by_id. You would call that function as follows.

    $user_info = $user_model -> read_by_id(2);

    We would pass in a number so the function can pull the appropriate data from the database.