Difference between revisions of "ModelBase API"
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. |
||
+ | ==Static Methods== |
||
− | =Core API= |
||
− | == |
+ | === get === |
+ | <code>ModelBase ModelBase::get( mixed $primarykey ) |
||
+ | 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. |
||
+ | <code>Film::get(47);</code> |
||
− | ===get=== |
||
− | <code> |
+ | <code>Film::get( Array( 'shortname' => 'inception' ) );</code> |
− | ====description==== |
||
− | Returns a single object matching the condition. Raises an exception if 0 or more than one object match. |
||
− | === |
+ | === getSet === |
+ | <code>Collection<ModelBase> ModelBase::getSet( Array $searchparameters, [ Array $order = Array() ], [ integer $limit = 0 ], [ integer $start = 0 ])</code> |
||
− | <code>mixed filter(QueryCondition condition)</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. |
||
+ | You must specify at least one field/value pair in $searchparamaters. If you wish to retrieve all objects, use getAll. |
||
− | ====description==== |
||
− | Returns a list of objects matching the condition. |
||
+ | $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. |
||
− | ==Instance methods== |
||
+ | $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. |
||
− | ===save=== |
||
− | Updates the objects data in the database. If the data is new, it should insert a new row with the objects data. |
||
+ | $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". |
||
− | ===update=== |
||
− | Pulls the objects data from the database again and updates the object. |
||
+ | <code>$films_in_2009 = Film::getSet( Array ( 'year' => 2009 ) );</code> |
||
− | ===delete=== |
||
− | === |
+ | === getAll === |
+ | <code>Collection<ModelBase> ModelBase::getAll( [ Array $order = Array() ], [ integer $limit = 0 ], [ integer $start = 0 ])</code> |
||
− | If <code>new X($id)</code> is called, it should instantiate the object with the information associated with that id. |
||
+ | 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. |
||
Revision as of 16:39, 31 October 2010
An API Reference for classes that are imbued with ModelBase/SuperClass awesomeness.
Contents
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);