Quick start guide
From Outlet ORM Wiki
Contents |
Set Up
Download and install Outlet according to the instructions. For this guide, we will assume a project structure like this:
MYAPP |- classes |- |- outlet // outlet classes | `- domain // your entity classes |- docroot | |- css | |- img | `- index.php `- outlet-config.php // outlet configuration file
Entities
You must start by creating the entities that you wish to map to the database. Outlet does not generate the entities for you. For a very straight forward mapping you generally create a class for each table that has identity. And you create a public property on that class for each of the fields on the table.
We will start with entities that only have properties, no relationships.
MYAPP/classes/domain/Project.php
class Project { public $ID; public $Name; }
MYAPP/classes/domain/Bug.php
class Bug { public $ID; public $Title; public $ProjectID; public $Description; }
Relationships
Let's create some associations. The Bug entity has a many-to-one relationship to the Project entity. So if we are working with an instance of Bug we would expect a getter and a setter for it's related Project instance:
MYAPP/classes/domain/Bug.php
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; } }
The Project entity has a one-to-many relationship to the Bug entity, so we would expect that an instance of Project would contain an array of Bug instances. For this relationship we will need the following methods: getBugs, setBugs, and addBug:
MYAPP/classes/domain/Project.php
class Project { public $ID; public $Name; private $bugs; public function __construct () { $this->bugs = new Collection(); } public function getBugs () { return $this->bugs; } public function setBugs (Collection $bugs) { $this->bugs = $bugs; } }
Configuration
Create a config file:
outlet-config.php
<?php 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')) ) ) ) );
Usage
MYAPP/docroot/index.php
<?php // for convenience define('APPROOT', dirname(__FILE__).'/..'); set_include_path(APPROOT.'/classes:' . get_include_path()); // initialize outlet with the configuration from above require 'outlet/Outlet.php'; Outlet::init(include APPROOT.'/outlet-config.php'); $con = Outlet::getInstance(); // generate the proxies // this needs to be done only once $con->createProxies(); // let's create a Project $project = new Project; $project->Name = "Outlet Project"; // let's add a bug to our project $bug = new Bug; $bug->Title = "Error on site"; $bug->Description = "I get an error when I go to the site"; $project->getBugs()->add( $bug ); // now let's save the project (it will also save the bug) $con->save( $project );

