Mapping a Model

Defining Fields

Fields are defined in the model’s constructor and are a representation of a table’s columns. LEAP provides 13 pre-defined field types (which enforce the integrity of the data being stored and/or accessed): Binary, Bit, Blob, Boolean, Data, Date, DateTime, Decimal, Double, Integer, String, Text, and Time fields.

Binary Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_Binary($this, array(
 5        'callback' => 'callback_function',
 6        'default' => '0101010101010101',
 7        'label' => 'My Label',
 8        'max_length' => 16,
 9        'nullable' => FALSE,
10        'savable' => TRUE,
11    )),
12    ...
13 );
Bit Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_Bit($this, array(
 5        'callback' => 'callback_function',
 6        'default' => 0x0FF0FFF0F,
 7        'label' => 'My Label',
 8        'nullable' => FALSE,
 9        'pattern' => array('A' => 1, 'B' => 4, 'C' => 7, 'D' => 12, 'E' => 8),
10        'savable' => TRUE,
11    )),
12    ...
13 );
Blob Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_Blob($this, array(
 5        'callback' => 'callback_function',
 6        'default' => new Data('Sample Textual Data', Data::STRING_DATA),
 7        'label' => 'My Label',
 8        'nullable' => FALSE,
 9        'savable' => TRUE,
10    )),
11    ...
12 );
Boolean Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_Boolean($this, array(
 5        'callback' => 'callback_function',
 6        'default' => TRUE,
 7        'label' => 'My Label',
 8        'nullable' => FALSE,
 9        'savable' => TRUE,
10    )),
11    ...
12 );
Date Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_Date($this, array(
 5        'callback' => 'callback_function',
 6        'control' => 'select',
 7        'default' => '2011-12-24',
 8        'enum' => array('2011-12-24', '2011-12-25'),
 9        'label' => 'My Label',
10        'nullable' => FALSE,
11        'savable' => TRUE,
12    )),
13    ...
14 );
DateTime Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_DateTime($this, array(
 5        'callback' => 'callback_function',
 6        'control' => 'select',
 7        'default' => '2011-12-24 00:00:00',
 8        'enum' => array('2011-12-24 00:00:00', '2011-12-25 00:00:00'),
 9        'label' => 'My Label',
10        'nullable' => FALSE,
11        'savable' => TRUE,
12    )),
13    ...
14 );
Decimal Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_Decimal($this, array(
 5        'callback' => 'callback_function',
 6        'control' => 'select',
 7        'default' => 60.00,
 8        'enum' => array(0.00, 20.00, 40.00, 60.00, 80.00, 100.00),
 9        'label' => 'My Label',
10        'nullable' => FALSE,
11        'precision' => 15,
12        'savable' => TRUE,
13        'scale' => 2,
14    )),
15    ...
16 );
Double Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_Double($this, array(
 5        'callback' => 'callback_function',
 6        'control' => 'select',
 7        'default' => 60.00,
 8        'enum' => array(0.00, 20.00, 40.00, 60.00, 80.00, 100.00),
 9        'label' => 'My Label',
10        'max_decimals' => 2,
11        'max_digits' => 15,
12        'nullable' => FALSE,
13        'range' => array(0.00, 100.00),
14        'savable' => TRUE,
15        'unsigned' => FALSE,
16    )),
17    ...
18 );
Integer Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_Integer($this, array(
 5        'callback' => 'callback_function',
 6        'control' => 'select',
 7        'default' => 60,
 8        'enum' => array(0, 20, 40, 60, 80, 100),
 9        'label' => 'My Label',
10        'max_length' => 11,
11        'nullable' => FALSE,
12        'range' => array(0, 100),
13        'savable' => TRUE,
14        'unsigned' => FALSE,
15    )),
16    ...
17 );
String Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_String($this, array(
 5        'callback' => 'callback_function',
 6        'control' => 'select',
 7        'default' => 'data',
 8        'enum' => array('data', 'text'),
 9        'label' => 'My Label',
10        'max_length' => 5,
11        'nullable' => FALSE,
12        'regex' => '/^regex(pr)?$/i',
13        'savable' => TRUE,
14    )),
15    ...
16 );
Text Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_Text($this, array(
 5        'callback' => 'callback_function',
 6        'default' => 'data',
 7        'label' => 'My Label',
 8        'nullable' => FALSE,
 9        'savable' => TRUE,
10    )),
11    ...
12 );
Time Field
 1 <?php
 2 $this->fields = array(
 3    ...
 4    'FieldName' => new DB_ORM_Field_Time($this, array(
 5        'callback' => 'callback_function',
 6        'control' => 'select',
 7        'default' => '12:00:00',
 8        'enum' => array('00:00:00', '12:00:00', '23:00:00'),
 9        'label' => 'My Label',
10        'nullable' => FALSE,
11        'savable' => TRUE,
12    )),
13    ...
14 );

Defining Field Adaptors

An adaptor makes working with certain data easier. The idea behind an adaptor is to process data that would otherwise be a pain to work with. An adaptor helps to minimize the amount of coding that you have to do over and over again without one. Adaptors are not fields, they just act as an interface for saving and fetching data from a field. Since there are some common types of data that developers typically deal with on a day-to-day basis, LEAP supplies a number of adaptors to handle such data.

Boolean Field Adaptor
1 <?php
2 $this->adaptors = array(
3    ...
4    'AdaptorName' => new DB_ORM_Field_Adaptor_Boolean($this, array(
5        'field' => 'answer',
6        'values' => array('good', 'bad'),
7    )),
8    ...
9 );
DateTime Field Adaptor
1 <?php
2 $this->adaptors = array(
3    ...
4    'AdaptorName' => new DB_ORM_Field_Adaptor_DateTime($this, array(
5        'field' => 'DateModified',
6        'format' => 'Y-m-d H:i:s',
7    )),
8    ...
9 );
Encryption Field Adaptor
1 <?php
2 $this->adaptors = array(
3    ...
4    'AdaptorName' => new DB_ORM_Field_Adaptor_Encryption($this, array(
5        'config' => 'default',
6        'field' => 'password',
7    )),
8    ...
9 );
GZ Field Adaptor
1 <?php
2 $this->adaptors = array(
3    ...
4    'AdaptorName' => new DB_ORM_Field_Adaptor_GZ($this, array(
5        'field' => 'image',
6        'level' => 9,
7    )),
8    ...
9 );
JSON Field Adaptor
 1 <?php
 2 $this->adaptors = array(
 3    ...
 4    'AdaptorName' => new DB_ORM_Field_Adaptor_JSON($this, array(
 5        'field' => 'data',
 6        'prefix' => 'while(1); [',
 7        'suffix' => ']',
 8    )),
 9    ...
10 );
List Field Adaptor
1 <?php
2 $this->adaptors = array(
3    ...
4    'AdaptorName' => new DB_ORM_Field_Adaptor_List($this, array(
5        'delimiter' => ';',
6        'field' => 'roles',
7    )),
8    ...
9 );
Number Field Adaptor
 1 <?php
 2 $this->adaptors = array(
 3    ...
 4    'AdaptorName' => new DB_ORM_Field_Adaptor_Number($this, array(
 5        'delimiter' => ',',
 6        'field' => 'balance',
 7        'precision' => 2,
 8        'separator' => '.',
 9    )),
10    ...
11 );
Object Field Adaptor
1 <?php
2 $this->adaptors = array(
3    ...
4    'AdaptorName' => new DB_ORM_Field_Adaptor_Object($this, array(
5        'class' => 'MyObject',
6        'field' => 'data',
7    )),
8    ...
9 );
UOM Field Adaptor
 1 <?php
 2 $this->adaptors = array(
 3    ...
 4    'AdaptorName' => new DB_ORM_Field_Adaptor_UOM($this, array(
 5        'field' => 'freight_weight',
 6        'measurement' => 'weight',
 7        'units' => array('pounds', 'kilograms'),
 8    )),
 9    ...
10 );
XML Field Adaptor
1 <?php
2 $this->adaptors = array(
3    ...
4    'AdaptorName' => new DB_ORM_Field_Adaptor_XML($this, array(
5        'field' => 'data',
6    )),
7    ...
8 );

Defining Field Aliases

Many database tables have poorly named fields and there are some circumstances where it is necessary to use another name to reference certain fields. LEAP, therefore, offers the ability to use aliases to reference to such fields. Below is an example of how aliases are declared in LEAP:

1 <?php
2 $this->aliases = array(
3    ...
4    'AliasName' => new DB_ORM_Field_Alias($this, 'FieldName');
5    ...
6 );

Defining Relations

By creating a relation, you can access data from another table. LEAP allows for three types of relations: belongs to, has many, and has one relations.

Belongs To Relation
 1 <?php
 2 $this->relations = array(
 3    ...
 4    'RelationName' => new DB_ORM_Relation_BelongsTo($this, array(
 5        'child_key' => array('Address_ID'),
 6        'parent_key' => array('ID'),
 7        'parent_model' => 'address',
 8    )),
 9    ...
10 );
Has Many Relation
 1 <?php
 2 $this->relations = array(
 3    ...
 4    'RelationName' => new DB_ORM_Relation_HasMany($this, array(
 5        'child_key' => array('User_ID'),
 6        'child_model' => 'address',
 7        'options' => array(
 8            array('where', array('Addresses.User_ID', '>=', '10')),
 9            array('order_by', array('Addresses.ID', 'DESC')),
10            array('limit', array(5)),
11        ),
12        'parent_key' => array('ID'),
13    )),
14    ...
15 );
Has Many Through Relation
 1 <?php
 2 $this->relations = array(
 3    ...
 4    'RelationName' => new DB_ORM_Relation_HasMany($this, array(
 5        'child_key' => array('rID'),
 6        'child_model' => 'role',
 7        'options' => array(
 8            array('where', array('rID', '>=', '10')),
 9            array('order_by', array('rID', 'DESC')),
10            array('limit', array(5)),
11        ),
12        'parent_key' => array('uID'),
13        'through_keys' => array(
14            array('uID'), // [0] matches with parent
15            array('rID'), // [1] matches with child
16        ),
17        'through_model' => 'user_role',
18    )),
19    ...
20 );
Has One Relation
 1 <?php
 2 $this->relations = array(
 3    ...
 4    'RelationName' => new DB_ORM_Relation_HasOne($this, array(
 5        'child_key' => array('User_ID'),
 6        'child_model' => 'log',
 7        'parent_key' => array('ID'),
 8    )),
 9    ...
10 );