Defining a Model

By default, all models for the LEAP ORM are stored in ‘classes/model/leap’ so that they do not conflict with any other models you have created to work with other ORM modules. This means that the class name for all LEAP models are prefixed with ‘Model_Leap_’.

Each LEAP model must extend ‘DB_ORM_Model’. This class’s constructor will define the fields for the model. It will also define all aliases, adaptors, and relations. A model may also override three methods to define which data source the model will use, the table’s name, and the table’s primary key.

Below is an example of a typical LEAP model:

 1 <?php defined('SYSPATH') or die('No direct script access.');
 2 
 3 class Model_Leap_User extends DB_ORM_Model {
 4 
 5    public function __construct() {
 6        parent::__construct();
 7        $this->fields = array(
 8            'ID' => new DB_ORM_Field_Integer($this, array(
 9                'max_length' => 11,
10                'nullable' => FALSE,
11                'unsigned' => TRUE,
12            )),
13            'Username' => new DB_ORM_Field_String($this, array(
14                'max_length' => 50,
15                'nullable' => FALSE,
16            )),
17            'Password' => new DB_ORM_Field_String($this, array(
18                'max_length' => 32,
19                'nullable' => FALSE,
20            )),
21            'FirstName' => new DB_ORM_Field_String($this, array(
22                'max_length' => 35,
23                'nullable' => FALSE,
24            )),
25            'LastName' => new DB_ORM_Field_String($this, array(
26                'max_length' => 35,
27                'nullable' => FALSE,
28            )),
29            'IsActive' => new DB_ORM_Field_Boolean($this, array(
30                'default' => TRUE,
31                'nullable' => FALSE,
32            )),
33        );
34        $this->relations = array(
35            'Roles' => new DB_ORM_Relation_HasMany($this, array(
36                'child_key' => array('User_ID'),
37                'child_model' => 'role',
38            )),
39        );
40    }
41 
42    public static function data_source($instance = 0) {
43        return ($instance > DB_DataSource::MASTER_INSTANCE) ? 'slave' : 'master';
44    }
45 
46    public static function table() {
47        return 'user';
48    }
49 
50    public static function primary_key() {
51        return array('ID');
52    }
53 
54 }

For more information on how to map fields, aliases, adaptors, and relations, see the tutorial on mapping models.

There are some other static methods worth mentioning, e.g. DB_ORM_Model::is_auto_incremented() and DB_ORM_Model::is_savable(). You should overload DB_ORM_Model::is_auto_incremented() when your table’s primary key is not a composite key and is a non-integer. To overload it, do the following:

1 <?php
2 public static function is_auto_incremented() {
3     return FALSE;
4 }

The purpose of overloading DB_ORM_Model::is_savable() is to prevent a model from attempting to delete or save a record. This is particularly useful with reference tables. It can be overloaded like so:

1 <?php
2 public static function is_savable() {
3     return FALSE;
4 }