Skip to content
Devin Smith edited this page Feb 3, 2016 · 11 revisions

Tipsy provides a simple ORM for SQL.


Methods

load, o

Load is called on class instantiation, or when called using object notation. Accepts either an id, an array, or ab object to construct.

Load the object by $id from the database

$User->load($id);

or

User::o($id);

Create a new object with an array. You may also pass an object instead of an array in replace of any of the 3 examples below.

$user = $User->load([
	name => 'devin'
]);

or

$user = new User([
	name => 'devin'
]);

or

$user = User::o([
	name => 'devin'
]);
query, q

Send a query to retrieve a list of items. This will return a Looper object.

$User->query('select * from user where name like ', ['devin%']);

or

User::q('select * from user where name like ', ['devin%']);

Note that both the Resource object and the Looper object have the method get. You can always safely use $object->get(0) regardless of object type.

dbId

Returns the database id of the object

$user = $User->load(1);
echo $user->dbId(); // prints 1
properties

Returns the objects properties as an array

$user = new User([
	name => 'devin'
]);
var_dump($user->properties()); // dumps an array with the user devin
exports

By default, returns the object's properties. This method is called internally by json(), and is best used when dumping different data for different users. See the example below for more info.

$user = new User([
	name => 'devin'
]);
var_dump($user->exports()); // dumps an array with the user devin
json

Resource allows use of $resource->json() and json_encode($resource) interchangeably. Both these method call the exports method.

$user = new User([
	name => 'devin'
]);
echo $user->json(); // prints the json output of the resources properties
createTable

Not yet fully implemented

dropTable

Not yet fully implemented


Resource creation

Resources are defined using Tipsy's Service interface. See User-Defined-Services for more information.

$tipsy->service('User', [
	_id => 'id',
	_table => 'user'
]);

Examples

Loading an item using route params
// create the User model based on the resource class
$tipsy->service('User', [
	_id => 'id',
	_table => 'user'
]);
// load the user when the route is visited and display the user name
$tipsy->when('user/:id', function($Params, $User) {
	echo $User->load($Params->id)->name;
});
Overload the exports method for a user
// create the User model
class User extends \Tipsy\Resource {
	public function exports() {
		$props = $this->properties();

		// properties viewable by anyone
		$public = ['id', 'name', 'location', 'website', 'image'];

		// properties viewable only by the logged in user
		$private = ['email', 'gender', 'auth'];

		if (Tipsy::middleware('Session')->user()) {
			$public = array_merge($public, $private);
		}

		foreach ($props as $key => $prop) {
			if (!in_array($key, $public)) {
				unset($props[$key]);
			}
		}

		return $props;
	}

	public function __construct($id = null) {
		$this->idVar('id')->table('user')->load($id);
	}
}

// load the user when the route is visited and display the users properties
$tipsy->when('user/:id', function($Params, $User) {
	echo $User->load($Params->id)->json();
});