-
Notifications
You must be signed in to change notification settings - Fork 10
Resource
Tipsy provides a simple ORM for SQL.
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'
]);
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.
Returns the database id of the object
$user = $User->load(1);
echo $user->dbId(); // prints 1
Returns the objects properties as an array
$user = new User([
name => 'devin'
]);
var_dump($user->properties()); // dumps an array with the user devin
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
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
Not yet fully implemented
Not yet fully implemented
Resources are defined using Tipsy's Service interface. See User-Defined-Services for more information.
$tipsy->service('User', [
_id => 'id',
_table => 'user'
]);
// 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;
});
// 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();
});
- Home
- Getting Started
- Server Config
- Installation
- Installing Composer
- App
- Route Shorthand
- Config
- Routes
- Methods
- Controller Types
- Params & Regex
- Aliases
- Dependency Injection
- Advanced Routing
- Services
- User Defined Services
- Built in Services
- Middleware
- Views
- Templates
- Scope
- Resource
- Factory
- Looper
- Examples
- Plugins
- About