Chapter 5. Usage

Select by Primary Key

You can select a single object by using the load method and specifying the primary key:

<?php
$outlet = Outlet::getInstance();
// retrieve a Bug by primary key
$bug = $outlet->load("Bug", 1);

Select using Queries

A query with outlet is just a regular SQL statement except for the ability to refer to columns by their class member names. Simply wrap the identifier in curly braces and outlet will automatically replace it with the corresponding table or column name. You can even add aliases:

$bugs = $outlet->select("Bug", "INNER JOIN {Project p} ON {Bug.ProjectID} = {p.ProjectID}");
// this code will be transformed to
// SELECT bugs.* 
// FROM bugs
// INNER JOIN projects p
//   ON bugs.project_id = p.project_id

<?php
$outlet = Outlet::getInstance();
// retrieve an array of Bug objects by using a prepared statement
$bugs = $outlet->select("Bug", "WHERE {Bug.StatusID} = ?", array(1));

Although referring to the columns by their class member name is recommended, you're always free to simple use straight sql.

Select using Fluent Interface

When using the fluent interface you get a few more features, such as eager fetching. Currently eager fetching will only fetch entities related by many-to-one and one-to-one relationships.

<?php
$outlet = Outlet::getInstance();

// select bugs with a status of one,
// include the projects (eager fetching)
// and paginates data
$bugs = $outlet->from("Bug")
  ->with('Project')
  ->where("{Bug.StatusID} = ?", array(1))
  ->limit(10)
  ->offset(20)
  ->find();

Inserting and Updating

<?php
$outlet = Outlet::getInstance();
// insert a bug
$bug = new Bug;
$bug->Title = 'This is a test bug';
$bug->ProjectID = 1;
 
$outlet->save( $bug ); // executes an insert statement
 
// now that the bug is saved and we're dealing with a proxy,
// any subsequent calls to $outlet->save() will execute an update statement
$bug->Title = 'New Title';
 
$outlet->save( $bug ); // executes an update statement

Relationships

You can also use the query syntax to filter what's returned through a relationship method:

<?php
$outlet = Outlet::getInstance();
$project = $outlet->load('Project', 1);
 
$bugs = $project->getBugs('WHERE {Bug.StatusID} = 1');