Core_ORM
in package
Object-relational mapping. Implement an Active record pattern.
Creating objects
// Create an empty object of model Book_Model
$object = Core_ORM::factory('Book');
// Create an object of model Book_Model and load from database where primary key is 1
$object = Core_ORM::factory('Book', 1);
Set property, the first way:
$object->value(123)->otherValue('my value');
The second way:
$object->value = 123;
$object->otherValue = 'my value';
Saving object
// Change column and save
$object = Core_ORM::factory('Book', 1);
$object
->value(123)
->save();
Finding objects
// Find all objects of model Book_Model
$aBooks = Core_ORM::factory('Book')->findAll();
foreach ($aBooks as $oBook) { // do something }
// Find objects of model Book_Model with conditions
$object = Core_ORM::factory('Book');
$object->queryBuilder()
->where('value', '=', 99);
$aBooks = $object->findAll();
foreach ($aBooks as $oBook)
{
// do something
}
Tags
Table of Contents
- $config : mixed
- ORM config
- $_belongsTo : array<string|int, mixed>
- Belongs to relations
- $_changedColumns : array<string|int, mixed>
- List of changed columns
- $_dataValues : array<string|int, mixed>
- data-values, e.g. dataMyValue
- $_hasMany : array<string|int, mixed>
- One-to-many or many-to-many relations
- $_hasOne : array<string|int, mixed>
- One-to-one relations
- $_modelColumns : array<string|int, mixed>
- List of columns in model with values
- $_modelName : mixed
- Model name, e.g. 'book' for 'Book_Model'
- $_preloadValues : array<string|int, mixed>
- List of preloaded values those will set for new object which does not have primary key
- $_primaryKey : string
- Primary key
- $_queryBuilder : Core_QueryBuilder_Select
- Select query builder
- $_skipColumns : array<string|int, mixed>
- List of skipped columns from table
- $_sorting : array<string|int, mixed>
- Default sorting for models <code> protected $_sorting = array( 'tablename.sorting' => 'ASC' ); </code>
- $_tableColumns : array<string|int, mixed>
- List of columns in table
- $_tableName : mixed
- Table name, e.g. 'books' for 'Book_Model'
- add() : self
- Add related object. If main object does not save, it will save.
- changed() : bool
- Check model has been changed.
- check() : self
- Check model values. If model has incorrect value, one will correct or call exception.
- chunk() : bool
- Retrieve a small chunk and feeds each one into $callback for processing. It stops looping when $callback returns FALSE
- clear() : self
- Clear object
- create() : self
- Insert new object data into database
- delete() : self
- Delete object from database
- deleteAll() : self
- Delete all object
- factory() : mixed
- Create and return an object of model
- find() : self
- Find object in database and load one. Use clear() before find() with conditions!
- findAll() : array<string|int, mixed>
- Find all objects
- getChangedData() : array<string|int, mixed>
- Get changed columns with values
- getClassName() : string
- Get Model CallsName
- getCount() : int|false
- Get count object
- getDatabaseDriver() : string
- Get name of Database Driver
- getDataValues() : array<string|int, mixed>
- Get dataValues
- getFirst() : null|Core_ORM
- Get fist entity, ordered by primary key
- getLast() : null|Core_ORM
- Get last entity, ordered by primary key
- getModelName() : string
- Get model name, e.g. 'book' for 'Book_Model'
- getPrimaryKey() : mixed
- Get primary key value
- getPrimaryKeyName() : string
- Get primary key name
- getRelations() : array<string|int, mixed>
- Get $this->_relations
- getTableColumns() : array<string|int, mixed>
- Get tableColumns
- getTableName() : string
- Get table name
- isCallable() : bool
- Verify that the contents of a variable can be called as a function
- isEmptyPrimaryKey() : bool
- Check is primary key NULL
- queryBuilder() : Core_QueryBuilder_Select
- Get query builder for select
- remove() : self
- Remove related object. If main object does not save, it will save.
- save() : self
- Save object. Uses self::update() or self::create()
- setDatabaseDriver() : mixed
- Set name of Database Driver
- setRelations() : self
- Set $this->_relations
- toArray() : array<string|int, mixed>
- Convert Object to Array
- update() : self
- Update object data into database
- getDatabase() : Core_DataBase
- Get Database
Properties
$config
ORM config
public
static mixed
$config
= \NULL
$_belongsTo
Belongs to relations
protected
array<string|int, mixed>
$_belongsTo
= array()
// Belongs to relation for Comment-Book:
protected $_belongsTo = array('book' => array());
// Equivalence belongs to relation for Comment-Book with detailed conditions:
// book - Model name
// foreign_key - Foreign key
// primary_key - Primary key in the parent table
protected $_belongsTo = array('book' => array(
'foreign_key' => 'book_id'
));
$_changedColumns
List of changed columns
protected
array<string|int, mixed>
$_changedColumns
= array()
$_dataValues
data-values, e.g. dataMyValue
protected
array<string|int, mixed>
$_dataValues
= array()
$_hasMany
One-to-many or many-to-many relations
protected
array<string|int, mixed>
$_hasMany
= array()
One-to-many relation
// Relation one-to-many for Book-Comments:
protected $_hasMany = array('comment' => array());
// Equivalence relation one-to-many for Book-Comments with detailed conditions:
// comment - Model name
// foreign_key - Foreign key
protected $_hasMany = array('comment' => array(
'foreign_key' => 'book_id'
));
Many-to-many relation
// Relation many-to-many for Book-Comments through model 'books_comment':
protected $_hasMany = array('comment' => array(
'through' => 'books_comment'
));
// Relation many-to-many for Book-Comments through model 'books_comment' with detailed conditions:
protected $_hasMany = array('comment' => array(
'foreign_key' => 'book_id',
'through' => 'books_comment',
'dependent_key' => 'comment_id'
));
$_hasOne
One-to-one relations
protected
array<string|int, mixed>
$_hasOne
= array()
// Relation one-to-one for Book-Comment:
protected $_hasOne = array('comment' => array());
// Equivalence relation one-to-one for Book-Comment with detailed conditions:
// comment - Model name
// foreign_key - Foreign key
protected $_hasOne = array('comment' => array(
'foreign_key' => 'book_id'
));
$_modelColumns
List of columns in model with values
protected
array<string|int, mixed>
$_modelColumns
= array()
$_modelName
Model name, e.g. 'book' for 'Book_Model'
protected
mixed
$_modelName
= \NULL
$_preloadValues
List of preloaded values those will set for new object which does not have primary key
protected
array<string|int, mixed>
$_preloadValues
= array()
$_primaryKey
Primary key
protected
string
$_primaryKey
= 'id'
$_queryBuilder
Select query builder
protected
Core_QueryBuilder_Select
$_queryBuilder
= \NULL
$_skipColumns
List of skipped columns from table
protected
array<string|int, mixed>
$_skipColumns
= array()
$_sorting
Default sorting for models <code> protected $_sorting = array( 'tablename.sorting' => 'ASC' ); </code>
protected
array<string|int, mixed>
$_sorting
= array()
$_tableColumns
List of columns in table
protected
array<string|int, mixed>
$_tableColumns
= array()
$_tableName
Table name, e.g. 'books' for 'Book_Model'
protected
mixed
$_tableName
= \NULL
Methods
add()
Add related object. If main object does not save, it will save.
public
add(Core_ORM $model[, string $relation = NULL ]) : self
Parameters
- $model : Core_ORM
- $relation : string = NULL
Tags
Return values
self —changed()
Check model has been changed.
public
changed() : bool
Return values
bool —check()
Check model values. If model has incorrect value, one will correct or call exception.
public
check([bool $exception = FALSE ]) : self
$object = Core_ORM::factory('Book', 1);
$object->value = 123;
$object->check(TRUE)->save();
Parameters
- $exception : bool = FALSE
-
Call exception (TRUE) or correct value (FALSE). Default FALSE.
Return values
self —chunk()
Retrieve a small chunk and feeds each one into $callback for processing. It stops looping when $callback returns FALSE
public
chunk(int $count, callable $callback[, bool $bCache = TRUE ]) : bool
Parameters
- $count : int
-
chunk size
- $callback : callable
- $bCache : bool = TRUE
-
use cache
Return values
bool —clear()
Clear object
public
clear() : self
Return values
self —create()
Insert new object data into database
public
create() : self
Tags
Return values
self —delete()
Delete object from database
public
delete([mixed $primaryKey = NULL ]) : self
// Delete object with lazy load
Core_ORM::factory('Book', 1)->delete();
// Delete object without load
Core_ORM::factory('Book')->delete(1);
Parameters
- $primaryKey : mixed = NULL
-
primary key for deleting object
Tags
Return values
self —deleteAll()
Delete all object
public
deleteAll([bool $bCache = TRUE ][, int $limit = 100 ]) : self
Parameters
- $bCache : bool = TRUE
-
use cache
- $limit : int = 100
-
default 100
Core_ORM::factory('Book')->Comments->deleteAll();
Return values
self —factory()
Create and return an object of model
public
static factory( $modelName[, $primaryKey = NULL ]) : mixed
Parameters
Return values
mixed —find()
Find object in database and load one. Use clear() before find() with conditions!
public
find([mixed $primaryKey = NULL ][, bool $bCache = TRUE ]) : self
Parameters
- $primaryKey : mixed = NULL
-
default NULL
- $bCache : bool = TRUE
-
use cache
// Find an object and load (without lazy load) // If an object is not found, primary key sets NULL $oBook = Core_ORM::factory('Book')->find(1);
Return values
self —findAll()
Find all objects
public
findAll([bool $bCache = TRUE ]) : array<string|int, mixed>
Parameters
- $bCache : bool = TRUE
-
use cache, default TRUE
// Find objects $aBooks = Core_ORM::factory('Book')->findAll(); foreach ($aBooks as $oBook) { var_dump($oBook->id); }
Tags
Return values
array<string|int, mixed> —getChangedData()
Get changed columns with values
public
getChangedData() : array<string|int, mixed>
Return values
array<string|int, mixed> —getClassName()
Get Model CallsName
public
static getClassName( $modelName) : string
Parameters
Return values
string —getCount()
Get count object
public
getCount([bool $bCache = TRUE ][, string $fieldName = '*' ][, bool $distinct = FALSE ]) : int|false
Parameters
- $bCache : bool = TRUE
-
use cache, default TRUE
- $fieldName : string = '*'
-
default '*'
- $distinct : bool = FALSE
-
default FALSE
Return values
int|false —
$iCount = Core_ORM::factory('Book')->getCount();
var_dump($iCount);
getDatabaseDriver()
Get name of Database Driver
public
static getDatabaseDriver() : string
Return values
string —getDataValues()
Get dataValues
public
getDataValues() : array<string|int, mixed>
Return values
array<string|int, mixed> —getFirst()
Get fist entity, ordered by primary key
public
getFirst([bool $bCache = TRUE ]) : null|Core_ORM
Parameters
- $bCache : bool = TRUE
-
use cache, default TRUE
Return values
null|Core_ORM —
$mObject = Core_ORM::factory('Book')->getFirst();
if (!is_null($mObject))
{
echo $mObject;
}
getLast()
Get last entity, ordered by primary key
public
getLast([bool $bCache = TRUE ]) : null|Core_ORM
Parameters
- $bCache : bool = TRUE
-
use cache, default TRUE
Return values
null|Core_ORM —
$mObject = Core_ORM::factory('Book')->getLast();
if (!is_null($mObject))
{
echo $mObject;
}
getModelName()
Get model name, e.g. 'book' for 'Book_Model'
public
getModelName() : string
Return values
string —getPrimaryKey()
Get primary key value
public
getPrimaryKey() : mixed
Return values
mixed —getPrimaryKeyName()
Get primary key name
public
getPrimaryKeyName() : string
Return values
string —getRelations()
Get $this->_relations
public
getRelations() : array<string|int, mixed>
Return values
array<string|int, mixed> —getTableColumns()
Get tableColumns
public
getTableColumns() : array<string|int, mixed>
Return values
array<string|int, mixed> —getTableName()
Get table name
public
getTableName() : string
Return values
string —isCallable()
Verify that the contents of a variable can be called as a function
public
isCallable(string $methodName) : bool
Parameters
- $methodName : string
-
method name
Return values
bool —isEmptyPrimaryKey()
Check is primary key NULL
public
isEmptyPrimaryKey() : bool
Return values
bool —queryBuilder()
Get query builder for select
public
queryBuilder() : Core_QueryBuilder_Select
Return values
Core_QueryBuilder_Select —remove()
Remove related object. If main object does not save, it will save.
public
remove(Core_ORM $model[, string $relation = NULL ]) : self
Parameters
- $model : Core_ORM
- $relation : string = NULL
Tags
Return values
self —save()
Save object. Uses self::update() or self::create()
public
save() : self
Tags
Return values
self —setDatabaseDriver()
Set name of Database Driver
public
static setDatabaseDriver(string $name) : mixed
Parameters
- $name : string
-
driver's name
Return values
mixed —setRelations()
Set $this->_relations
public
setRelations(array<string|int, mixed> $relations) : self
Parameters
- $relations : array<string|int, mixed>
-
Array with new relations
Return values
self —toArray()
Convert Object to Array
public
toArray() : array<string|int, mixed>
Return values
array<string|int, mixed> —update()
Update object data into database
public
update() : self
Tags
Return values
self —getDatabase()
Get Database
protected
getDatabase() : Core_DataBase