Usage - Redpill-Linpro/gamine GitHub Wiki

Retrieving objects from the backend

You retrieve objects (entities) from the backend using the corresponding Manager's find*() methods. These take parameters which are passed on to the configured endpoint, and translates the returned data into instantiated entity objects. Available methods for finding collection of objects are:

  • $manager->findAll($params = array())
  • $manager->findByKeyVal($key, $val, $params = array())

This method will only return one instantiated object if found: $manager->findOneById($id, $params = array())

It is good practice to extend these methods with more specific methods in your own manager classes, so f.ex. you can call $manager->findAllUsersbyUsername($username) instead of $manager->findByKeyVal('username', $username)

Saving objects to the backend

The manager also handles saving objects back to the backend. You do this by invoking the manager's save() method.

After the save() method is called - if the backend returns a representation of the saved object, the object will be re-populated with the returned data.

Removing objects from the backend

The manager has a remove() function that will issue a DELETE request to the backend, causing the object to be removed. There is no way to see if an object has been removed after the remove() function has been called.

Lazyloading

To invoke Gamine's lazyloading mechanism, you need to call the _populateRelatedObject() method inside your object. This function takes two parameters: the first is a string with the name of the property you want to lazyload, and the second is any parameters you want to pass on to the lazyloader.

Calling _populateRelatedObject() is safe, as it will only actually hit the backend if the property has not already been lazyloaded. Any subsequent calls will simply be ignored, unless the value has been cleared.

For more information on how to define lazyloading, see the annotations reference.

Direct backend calls

An entity can use the configured manager to perform direct calls to backend endpoints via these methods:

  • _apiGet($routename, $params = array())
  • _apiCall($routename, $params = array())
  • _apiSet($routename, $params = array(), $post_params = array())
  • _apiUnset($routename, $params = array(), $post_params = array())

These methods will look for endpoints configured in the manager. The endpoints must be saved in a static property in the manager, like this: protected static $resource_routes = array( 'resetpassword' => "trigger/new/password", 'resendverificationmail' => "/trigger/new/emailverification/{:userId}", 'addteam' => "add/team/{:teamId}", );

These urls can be triggered relative to the unique resource location for the current object, or as a top-level url if the corresponding location starts with a forward slash (see the "resendverificationemail" resource defined above for an example of a non-relative route). As shown above, these routes can take parameters where necessary, which are passed in to the respective methods when called.