really simple PHP

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

databaseThe name of the database
connectionParamsAn array of parameters for the connection (optional; see below)

Connection Paramaters

An array of connection parameters

userUsername (defaults to mysql.default_user, or “” if empty)
passwordPassword (defaults to mysql.default_password, or “” if empty)
hostHostname (defaults to mysql.default_host, or “localhost” if empty)
autoShould the database automatically be connected to?  Defaults to true
Summary
Connect to the database
Checks if the database is currently connected

Functions

connect

function connect( )

Connect to the database

Examples

$connectionParams["auto"] = false;

$database = new gapiDatabase('database_name', $connectionParams);

if($database->connect())
return "Connected!";

Returns

A boolean indicating if the connection was successful

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

trueIf the database is connected
falseIf the database is not connected

gapiTable

Create a new table, or retrieve one from the database

This does absolutely nothing... it will probably be deleted

gapiRecord

Create a new record, or retrieve one from the database

Parameters

$tableThe table name, required
$paramsArray of optional params, see below

Optional Parameters

fetchAn array of columns to retrieve (defaults to all available columns)
whereWhere clause for the statement
orderOrder By clause for the statement
limitarray([$offset,] $rowcount) CURRENTLY ALWAYS SET TO “array(1)”
autoAddBoolean; if true, nonexistant tables or columns will be added automatically (Defaults to true)
autoSaveBoolean, if true the record will be updated on every set() call (Defaults to false, so you must call “save()”)
Summary
Get the attribute(s) requested
Set the attribute(s) passed into the function
Save the record to the database

Functions

get

function get($attribute)

Get the attribute(s) requested

Parameters

$attributeThe name of a column (or an array of column names) to be returned

Returns

stringIf only one attribute is requested
arrayIf multiple attributes are requested

Example

$array = array( value1 => 'Value 1', value2 , value3 => 3);

set

function set($attribute,  
$value =  false)

Set the attribute(s) passed into the function

Parameters

$attributeThe 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

booleanWill always be true unless autoSave is on; then it will return if the query was successful

save

function save( )

Save the record to the database

Returns

booleanThe result of the saving
function connect( )
Connect to the database
function isConnected($strict =  false)
Checks if the database is currently connected
function get($attribute)
Get the attribute(s) requested
function set($attribute,  
$value =  false)
Set the attribute(s) passed into the function
function save( )
Save the record to the database