gapi_database.inc
A simple implementation of the ActiveRecord pattern
License
MIT-style license.
Version
v0.3
GAPI Copyright
copyright © 2007 Gregory Koberger http://www.gkoberger.net
ToDo
- Make array attributes case insensitive when possible
Summary
A simple implementation of the ActiveRecord pattern | |
Set up a database connection | |
Connect to the database | |
Checks if the database is currently connected | |
Create a new table, or retrieve one from the database | |
Create a new record, or retrieve one from the database | |
Get the attribute(s) requested | |
Set the attribute(s) passed into the function | |
Save the record to the database |
gapiDatabase
Set up a database connection
Parameters
| database | The name of the database |
| connectionParams | An array of parameters for the connection (optional; see below) |
Connection Paramaters
An array of connection parameters
| user | Username (defaults to mysql.default_user, or “” if empty) |
| password | Password (defaults to mysql.default_password, or “” if empty) |
| host | Hostname (defaults to mysql.default_host, or “localhost” if empty) |
| auto | Should the database automatically be connected to? Defaults to true |
Summary
Connect to the database | |
Checks if the database is currently connected |
isConnected
function isConnected( $strict = false )
Checks if the database is currently connected
Parameters
The $strict (boolean) parameter is optional. If true, it also matches the user and the host. It defaults to false, which means it just matches the current database name to $database
Examples
$database = new gapiDatabase('database_name', $connectionParams);
if( $database->isConnected() )
echo "Currently connected!";Returns
| true | If the database is connected |
| false | If the database is not connected |
gapiRecord
Create a new record, or retrieve one from the database
Parameters
| $table | The table name, required |
| $params | Array of optional params, see below |
Optional Parameters
| fetch | An array of columns to retrieve (defaults to all available columns) |
| where | Where clause for the statement |
| order | Order By clause for the statement |
| limit | array([$offset,] $rowcount) CURRENTLY ALWAYS SET TO “array(1)” |
| autoAdd | Boolean; if true, nonexistant tables or columns will be added automatically (Defaults to true) |
| autoSave | Boolean, if true the record will be updated on every set() call (Defaults to false, so you must call “save()”) |
set
function set( $attribute, $value = false )
Set the attribute(s) passed into the function
Parameters
| $attribute | The name of a single attribute OR (if using just the $attribute parameter) an associative array of the column name(s) and value(s) |
| $value | (optional) If you want to set just one attribute, you can use this parameter (see below) |
Example (Single attribute)
$record->set('name', 'Michael');Example (Multiple attributes)
$record->set(array('name'=>'Michael', 'phone'=>'987.555.1234') );Returns
| boolean | Will always be true unless autoSave is on; then it will return if the query was successful |
Connect to the database
function connect( )
Checks if the database is currently connected
function isConnected( $strict = false )
Get the attribute(s) requested
function get( $attribute )
Set the attribute(s) passed into the function
function set( $attribute, $value = false )
Save the record to the database
function save( )