Instantiate your entities and manipulate them just like regular objects.
Save them when ready.
<?php
$outlet = Outlet::getInstance();
$client = new Client;
$client->Name = 'Test Client';
$project = new Project;
$project->Name = 'Cool Project';
$project->setClient( $client );
$bug = new Bug;
$bug->Title = "Button doesn't work";
$project->addBug( $bug );
// inserts the project
// and all of the related entities
// in one transaction
$outlet->save( $project );
You can select using straight SQL. You can also use the class properties instead of the column names.
<?php
$outlet = Outlet::getInstance();
// select using prepared statement
$projects = $outlet->select(
'Project',
'WHERE StatusID = ? OR StatusID = ?',
array(0, 1)
);
// select one entity by primary key
$project = $outlet->load('Project', 1);
// select using a relationship method
$bugs = $project->getBugs('WHERE {Bug.StatusID} = 1');
// select using fluent-interface
$bugs = $outlet->from('Bug b')
->with('Project')
->where('{b.Status} = ?', array(Bug::OPEN))
->find();
Entities contain no database code.
They don't extend Persistent, ActiveRecord, or any other class.
They are plain old PHP objects that can extend (or not) from whatever you want them to.
<?php
// Bug Entity
class Bug {
public $ID;
public $Title;
public $ProjectID;
public $Description;
private $project;
public function getProject () {
return $this->project;
}
public function setProject (Project $project) {
$this->project = $project;
}
}
Configuration is just a PHP array. You can define it using straight php or parse it from JSON, XML, INI, etc.
You can also easily cache it in memory using APC or another op-code cache.
<?php
// configuration is just a php array
return array(
// configure the underlying pdo connection
'connection' => array(
'dsn' => 'mysql:host=myserver.com;dbname=mydb',
'username' => 'user',
'password' => 'pass',
'dialect' => 'mysql'
),
// map each class and their properties to their corresponding tables and columns
// also map the associations between classes
'classes' => array(
'Project' => array(
'table' => 'projects',
'props' => array(
'ID' => array('id', 'int', array('pk'=>true, 'autoIncrement'=>true)),
'Name' => array('name', 'varchar')
),
'associations' => array(
array('one-to-many', 'Bug', array('key'=>'ProjectID'))
)
),
'Bug' => array(
'table' => 'bugs',
'props' => array(
'ID' => array('id', 'int', array('pk'=>true, 'autoIncrement'=>true)),
'Title' => array('title', 'varchar'),
'ProjectID' => array('project_id', 'int'),
'Description' => array('description', 'varchar')
),
'associations' => array(
array('many-to-one', 'Project', array('key'=>'ProjectID'))
)
)
)
);