Difference between revisions of "ModelBase API"

From Warwick Student Cinema
Jump to: navigation, search
m
 
(5 intermediate revisions by one other user not shown)
Line 1: Line 1:
  +
An API Reference for classes that are imbued with ModelBase/SuperClass awesomeness.
An API that should be common to all model classes. Includes core methods that should be available in all classes, and optional methods that provide a common structure for frequently used extensions.
 
   
  +
PLEASE NOTE: This reference is '''very''' incomplete at the moment. Additions are being made as convenient. Though the API is fairly fixed, changes may be made without notice, so please be sure to read commit logs on bazaar regularly, as these are much more up to date than this page will be.
=Core API=
 
   
==Static methods==
+
==Static Methods==
   
===get===
+
=== get ===
  +
<code>ModelBase ModelBase::get( mixed $primarykey )
<code>mixed get(QueryCondition condition)</code>
 
  +
ModelBase ModelBase::get( Array $searchparameters )</code>
   
  +
Possibly the most important method on the class, use this to retrieve a single object from the database. You can either pass the objects primary key (usually the ID) or you can pass an array containing key/value pairs corresponding to fields and desired values. Note that if the specified parameters do not identify a single unique object, an exception will be thrown. If you wish to retrieve several objects based on a common parameter, use getSet.
====description====
 
Returns a single object matching the condition. Raises an exception if 0 or more than one object match.
 
   
  +
<code>Film::get(47);</code>
===filter===
 
<code>mixed filter(QueryCondition condition)</code>
+
<code>Film::get( Array( 'shortname' => 'inception' ) );</code>
   
====description====
 
Returns a list of objects matching the condition.
 
   
==Instance methods==
+
=== getSet ===
  +
<code>Collection<ModelBase> ModelBase::getSet( Array $searchparameters, [ Array $order = Array() ], [ integer $limit = 0 ], [ integer $start = 0 ])</code>
   
  +
Used to retrieve a Collection of objects with the specified parameters. This will always return a Collection, whether or not any objects actually matched the specified parameters.
===save===
 
Updates the objects data in the database. If the data is new, it should insert a new row with the objects data.
 
   
  +
You must specify at least one field/value pair in $searchparamaters. If you wish to retrieve all objects, use getAll.
===update===
 
Pulls the objects data from the database again and updates the object.
 
   
  +
$order allows you to order the objects in the collection. It should be an array containing fields to order by as keys, and "ASC" or "DSC" as values. You CANNOT order by fields that are other ModelBase objects, only basic types, such as integer or string.
===delete===
 
   
  +
$limit allows you to limit the number of objects retrieved. It should be an integer greater than 0. A limit of 0 will result in unlimited results.
===new/__construct===
 
  +
If <code>new X($id)</code> is called, it should instantiate the object with the information associated with that id.
 
  +
$start allows you to "skip" a number of objects. It should be an integer greater than 0. A start value of 0 will result in no "skipping".
  +
  +
<code>$films_in_2009 = Film::getSet( Array ( 'year' => 2009 ) );</code>
  +
  +
=== getAll ===
  +
<code>Collection<ModelBase> ModelBase::getAll( [ Array $order = Array() ], [ integer $limit = 0 ], [ integer $start = 0 ])</code>
  +
  +
Similar to getSet, except that all objects are retrieved, rather than a subset. The $order, $limit and $start parameters act as in getSet.
  +
  +
<code>$five_latest_news = News::getAll( Array( 'time'=>'DSC' ), 5);</code>
   
If <code>new X()</code> is called, it should leave all fields blank and when save is called, it inserts into the DB rather than updates.
 
   
   

Latest revision as of 22:33, 1 February 2019

An API Reference for classes that are imbued with ModelBase/SuperClass awesomeness.

PLEASE NOTE: This reference is very incomplete at the moment. Additions are being made as convenient. Though the API is fairly fixed, changes may be made without notice, so please be sure to read commit logs on bazaar regularly, as these are much more up to date than this page will be.

Static Methods

get

ModelBase ModelBase::get( mixed $primarykey ) ModelBase ModelBase::get( Array $searchparameters )

Possibly the most important method on the class, use this to retrieve a single object from the database. You can either pass the objects primary key (usually the ID) or you can pass an array containing key/value pairs corresponding to fields and desired values. Note that if the specified parameters do not identify a single unique object, an exception will be thrown. If you wish to retrieve several objects based on a common parameter, use getSet.

Film::get(47); Film::get( Array( 'shortname' => 'inception' ) );


getSet

Collection<ModelBase> ModelBase::getSet( Array $searchparameters, [ Array $order = Array() ], [ integer $limit = 0 ], [ integer $start = 0 ])

Used to retrieve a Collection of objects with the specified parameters. This will always return a Collection, whether or not any objects actually matched the specified parameters.

You must specify at least one field/value pair in $searchparamaters. If you wish to retrieve all objects, use getAll.

$order allows you to order the objects in the collection. It should be an array containing fields to order by as keys, and "ASC" or "DSC" as values. You CANNOT order by fields that are other ModelBase objects, only basic types, such as integer or string.

$limit allows you to limit the number of objects retrieved. It should be an integer greater than 0. A limit of 0 will result in unlimited results.

$start allows you to "skip" a number of objects. It should be an integer greater than 0. A start value of 0 will result in no "skipping".

$films_in_2009 = Film::getSet( Array ( 'year' => 2009 ) );

getAll

Collection<ModelBase> ModelBase::getAll( [ Array $order = Array() ], [ integer $limit = 0 ], [ integer $start = 0 ])

Similar to getSet, except that all objects are retrieved, rather than a subset. The $order, $limit and $start parameters act as in getSet.

$five_latest_news = News::getAll( Array( 'time'=>'DSC' ), 5);