Chapter 5. Usage

Selecting

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

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

select

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

load

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

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');