This is an old revision of the document!
All models in Rack applications extend from the abstract <php>Model</php> class, defined in include/models/Model.class.php
. This class provides a basic implementation for CRUD (create, read, update, and delete) functionality for a single database table. It utalizes the PDO
prepared statements to construct queries to help preventing SQL injections.
A <php>Model</php> object should never be manually created, as Rack provide a function <php>get_model($name)</php> in include/init.php
that creates an instance of a <php>Model</php> object for you when need it. The following snippet demonstrates its use:
<PHP> $model = get_model('Registration'); $registrations = $model→get(); </PHP>
Basic queries can be executed using the functions <php>get</php>, <php>create</php>, <php>update</php> and <php>delete</php> or their by_id
counterparts. The <php>create</php> and <php>update</php> functions accept a list with <php>'fieldname'⇒'value'</php> pairs that should be used to modify the database.
The <php>get</php>, <php>update</php> and <php>delete</php> functions also accept an array with filter conditions. These conditions will be appended to the query and multiple conditions will be joined with AND
. In this array, filter conditions should be specified as <php>'fieldnamefunction' ⇒ $value</php>.
Supported comparison functions are:
* eq: Tests if the field value is equal to (=
) another value.
* ne: Tests if the field value is not equal to (<>
) another value.
* lt: Tests if the field value is less than (<
) another value.
* lte: Tests if the field value is less than or equal to (⇐
) another value.
* gt: Tests if the field value is greater than (>
) another value.
* gte: Tests if the field value is greater than or equal to (>=
) another value.
* in: Tests if the field value is in a list.
* contains: Tests if the field contains a string value (LIKE '%value%'
). Note that %
and _
will not be automatically escaped.
* isnull: Tests if the field value is NULL
(if <php>'fieldisnull'⇒true</php>) or not NULL
(if <php>'fieldisnull'⇒false</php>).
If the comparison function is omitted (<php>'field' ⇒ $value</php>), the default of eq is used.
The <php>get</php> function also supports custom ordering by passing a list of field names. Descending ordering can be achieved by adding a minus in front of the field name. The following example will order first by study and then by birthdate in descending ordering: <php>['study', '-birthdate']</php>.
===== Functions =====
* <php>construct($db, $table=null)</php>: The variable <php>$db</php> is a PDO object. This value will be set by <php>get_model</php>. <php>$table</php> is the name of the table as it's stored in the database.