Leap ORM

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 ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_Binary($this, array(
'callback' => 'callback_function',
'default' => '0101010101010101',
'label' => 'My Label',
'max_length' => 16,
'nullable' => FALSE,
'savable' => TRUE,
)),
...
);
Metadata
callback string   optional
default string NULL | (empty) optional
label string (name) optional
max_length integer   required
nullable boolean TRUE optional
savable boolean TRUE optional
"Bit" Field ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_Bit($this, array(
'callback' => 'callback_function',
'default' => 0x0FF0FFF0F,
'label' => 'My Label',
'nullable' => FALSE,
'pattern' => array('A' => 1, 'B' => 4, 'C' => 7, 'D' => 12, 'E' => 8),
'savable' => TRUE,
)),
...
);
Metadata
callback string   optional
default integer NULL | 0 optional
label string (name) optional
nullable boolean TRUE optional
pattern array array('bits' => ((PHP_INT_SIZE == 8) ? 64 : 32)) optional
savable boolean TRUE optional
"Blob" Field ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_Blob($this, array(
'callback' => 'callback_function',
'default' => new Data('Sample Textual Data', Data::STRING_DATA),
'label' => 'My Label',
'nullable' => FALSE,
'savable' => TRUE,
)),
...
);
Metadata
callback string   optional
default Data NULL | new Data('', Data::HEXADECIMAL_DATA) optional
label string (name) optional
nullable boolean TRUE optional
savable boolean TRUE optional
"Boolean" Field ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_Boolean($this, array(
'callback' => 'callback_function',
'default' => TRUE,
'label' => 'My Label',
'nullable' => FALSE,
'savable' => TRUE,
)),
...
);
Metadata
callback string   optional
default boolean NULL | FALSE optional
label string (name) optional
nullable boolean TRUE optional
savable boolean TRUE optional
"Date" Field ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_Date($this, array(
'callback' => 'callback_function',
'control' => 'select',
'default' => '2011-12-24',
'enum' => array('2011-12-24', '2011-12-25'),
'label' => 'My Label',
'nullable' => FALSE,
'savable' => TRUE,
)),
...
);
Metadata
callback string   optional
control string auto optional
default string NULL | 0000-00-00 optional
enum array   optional
label string (name) optional
nullable boolean TRUE optional
savable boolean TRUE optional
"DateTime" Field ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_DateTime($this, array(
'callback' => 'callback_function',
'control' => 'select',
'default' => '2011-12-24 00:00:00',
'enum' => array('2011-12-24 00:00:00', '2011-12-25 00:00:00'),
'label' => 'My Label',
'nullable' => FALSE,
'savable' => TRUE,
)),
...
);
Metadata
callback string   optional
control string auto optional
default string NULL | 0000-00-00 00:00:00 optional
enum array   optional
label string (name) optional
nullable boolean TRUE optional
savable boolean TRUE optional
"Decimal" Field ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_Decimal($this, array(
'callback' => 'callback_function',
'control' => 'select',
'default' => 60.00,
'enum' => array(0.00, 20.00, 40.00, 60.00, 80.00, 100.00),
'label' => 'My Label',
'nullable' => FALSE,
'precision' => 15,
'savable' => TRUE,
'scale' => 2,
)),
...
);
Metadata
callback string   optional
control string auto optional
default double NULL | 0.0 optional
enum array   optional
label string (name) optional
nullable boolean TRUE optional
precision integer   required
savable boolean TRUE optional
scale integer   required
"Double" Field ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_Double($this, array(
'callback' => 'callback_function',
'control' => 'select',
'default' => 60.00,
'enum' => array(0.00, 20.00, 40.00, 60.00, 80.00, 100.00),
'label' => 'My Label',
'max_decimals' => 2,
'max_digits' => 15,
'nullable' => FALSE,
'range' => array(0.00, 100.00),
'savable' => TRUE,
'unsigned' => FALSE,
)),
...
);
Metadata
callback string   optional
control string auto optional
default double NULL | 0.0 optional
enum array   optional
label string (name) optional
max_decimals integer   optional
max_digits integer   optional
nullable boolean TRUE optional
range array   optional
savable boolean TRUE optional
unsigned boolean FALSE optional
"Integer" Field ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_Integer($this, array(
'callback' => 'callback_function',
'control' => 'select',
'default' => 60,
'enum' => array(0, 20, 40, 60, 80, 100),
'label' => 'My Label',
'max_length' => 11,
'nullable' => FALSE,
'range' => array(0, 100),
'savable' => TRUE,
'unsigned' => FALSE,
)),
...
);
Metadata
callback string   optional
control string auto optional
default integer NULL | 0 optional
enum array   optional
label string (name) optional
max_length integer   required
nullable boolean TRUE optional
range array   optional
savable boolean TRUE optional
unsigned boolean FALSE optional
"String" Field ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_String($this, array(
'callback' => 'callback_function',
'control' => 'select',
'default' => 'data',
'enum' => array('data', 'text'),
'label' => 'My Label',
'max_length' => 5,
'nullable' => FALSE,
'regex' => '/^regex(pr)?$/i',
'savable' => TRUE,
)),
...
);
Metadata
callback string   optional
control string auto optional
default string NULL | (empty) optional
enum array   optional
label string (name) optional
max_length integer   required
nullable boolean TRUE optional
regex string   optional
savable boolean TRUE optional
"Text" Field ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_Text($this, array(
'callback' => 'callback_function',
'default' => 'data',
'label' => 'My Label',
'nullable' => FALSE,
'savable' => TRUE,
)),
...
);
Metadata
callback string   optional
default string NULL | (empty) optional
label string (name) optional
nullable boolean TRUE optional
savable boolean TRUE optional
"Time" Field ( API | SRC )
$this->fields = array(
...
'FieldName' => new DB_ORM_Field_Time($this, array(
'callback' => 'callback_function',
'control' => 'select',
'default' => '12:00:00',
'enum' => array('00:00:00', '12:00:00', '23:00:00'),
'label' => 'My Label',
'nullable' => FALSE,
'savable' => TRUE,
)),
...
);
Metadata
callback string   optional
control string auto optional
default string NULL | 00:00:00 optional
enum array   optional
label string (name) optional
nullable boolean TRUE optional
savable boolean TRUE optional
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
$this->adaptors = array(
...
'AdaptorName' => new DB_ORM_Field_Adaptor_Boolean($this, array(
'field' => 'answer',
'values' => array('good', 'bad'),
)),
...
);
Metadata
field string   required
values array array('yes', 'no') optional
"DateTime" Field Adaptor
$this->adaptors = array(
...
'AdaptorName' => new DB_ORM_Field_Adaptor_DateTime($this, array(
'field' => 'DateModified',
'format' => 'Y-m-d H:i:s',
)),
...
);
Metadata
field string   required
format string Y-m-d H:i:s optional
"Encryption" Field Adaptor
$this->adaptors = array(
...
'AdaptorName' => new DB_ORM_Field_Adaptor_Encryption($this, array(
'config' => 'default',
'field' => 'password',
)),
...
);
Metadata
config string | array NULL optional
field string   required
"GZ" Field Adaptor
$this->adaptors = array(
...
'AdaptorName' => new DB_ORM_Field_Adaptor_GZ($this, array(
'field' => 'image',
'level' => 9,
)),
...
);
Metadata
field string   required
level integer 9 optional
"JSON" Field Adaptor
$this->adaptors = array(
...
'AdaptorName' => new DB_ORM_Field_Adaptor_JSON($this, array(
'field' => 'data',
'prefix' => 'while(1); [',
'suffix' => ']',
)),
...
);
Metadata
field string   required
prefix string (empty) optional
suffix string (empty) optional
"List" Field Adaptor
$this->adaptors = array(
...
'AdaptorName' => new DB_ORM_Field_Adaptor_List($this, array(
'delimiter' => ';',
'field' => 'roles',
)),
...
);
Metadata
delimiter string , optional
field string   required
"Number" Field Adaptor
$this->adaptors = array(
...
'AdaptorName' => new DB_ORM_Field_Adaptor_Number($this, array(
'delimiter' => ',',
'field' => 'balance',
'precision' => 2,
'separator' => '.',
)),
...
);
Metadata
delimiter char , optional
field string   required
precision integer 0 optional
separator char . optional
"Object" Field Adaptor
$this->adaptors = array(
...
'AdaptorName' => new DB_ORM_Field_Adaptor_Object($this, array(
'class' => 'MyObject',
'field' => 'data',
)),
...
);
Metadata
class string   required
field string   required
"UOM" Field Adaptor
$this->adaptors = array(
...
'AdaptorName' => new DB_ORM_Field_Adaptor_UOM($this, array(
'field' => 'freight_weight',
'measurement' => 'weight',
'units' => array('pounds', 'kilograms'),
)),
...
);
Metadata
field string   required
measurement string   required
units array   required
"XML" Field Adaptor
$this->adaptors = array(
...
'AdaptorName' => new DB_ORM_Field_Adaptor_XML($this, array(
'field' => 'data',
)),
...
);
Metadata
field string   required
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:
$this->aliases = array(
...
'AliasName' => new DB_ORM_Field_Alias($this, 'FieldName');
...
);

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
$this->relations = array(
...
'RelationName' => new DB_ORM_Relation_BelongsTo($this, array(
'child_key' => array('Address_ID'),
'parent_key' => array('ID'),
'parent_model' => 'address',
)),
...
);
Metadata
child_key array   required
parent_key array   optional
parent_model string   required
"Has Many" Relation
$this->relations = array(
...
'RelationName' => new DB_ORM_Relation_HasMany($this, array(
'child_key' => array('User_ID'),
'child_model' => 'address',
'options' => array(
array('where', array('Addresses.User_ID', '>=', '10')),
array('order_by', array('Addresses.ID', 'DESC')),
array('limit', array(5)),
),
'parent_key' => array('ID'),
)),
...
);
Metadata
child_key array   required
child_model string   required
options array array() optional
parent_key array   optional
"Has Many Through" Relation
$this->relations = array(
...
'RelationName' => new DB_ORM_Relation_HasMany($this, array(
'child_key' => array('rID'),
'child_model' => 'role',
'options' => array(
array('where', array('rID', '>=', '10')),
array('order_by', array('rID', 'DESC')),
array('limit', array(5)),
),
'parent_key' => array('uID'),
'through_keys' => array(
array('uID'), // [0] matches with parent
array('rID'), // [1] matches with child
),
'through_model' => 'user_role',
)),
...
);
Metadata
child_key array   required
child_model string   required
options array array() optional
parent_key array   optional
through_keys array   required
through_model string   required
"Has One" Relation
$this->relations = array(
...
'RelationName' => new DB_ORM_Relation_HasOne($this, array(
'child_key' => array('User_ID'),
'child_model' => 'log',
'parent_key' => array('ID'),
)),
...
);
Metadata
child_key array   required
child_model string   required
parent_key array   optional
Licensed under the Apache License, Version 2.0
Fork me on GitHub