06. ZORM - Sokoro

PHP Object Relational Mapper

*Note sokoro currently supports mysql only. Support for SQLite will soon be included and we have plans for Postgre and Oracle.

Create a table:

ZORM::create("table_name", ['col_name'=>"varchar(30)"]);

*note when creating tables Sokoro automaticaly creates the following:

id - auto incrementing pk created_at timestamp that records time of record creation created_by integer possibly for link as FK updated_at timestamp that updates on each update updated_by int possibly for FK

the id column as PK which auto increments is essential to Sokoro functioning as expected

set the table pointer to the table you intend to work with:

ZORM::table("table_name");

To insert a new record:

ZORM::add(['col_name'=>"value"...]); //alias is ZORM::insert($records);

this will scafold the rest ie create auto incrementing id, set created on value

To update an existing record:

ZORM::update('id', ['col_name'=>"value"...]);

this will update the record with the id given. Alternatively:

ZORM::update('col', 'val', ['col_name'=>"value"...]);

this will update the table with the new params for columnname with the value val.

deleting a record:

ZORM::remove(1);

this will delete record on the set table with id=1

alternatiely:

ZORM::remove('col_name', 'value');

will delete all records with the value val on col_name

THE READ VARIANTS

To read all recods in the set table:

ZORM::rows(); //alias ZORM::read();

this returns as array of objects;

It may take an argument which is an sql statement:

ZORM::rows("SELECT * FROM another_table LIMIT 14"); //alias ZORM::read($sql);

to do a search on a column inclusing partial matches

ZORM::find('col_name', 'partial_match');

this returns a row of objects

THE ORMRecord Object

To get a single record as object:

$row = ZORM::row(1); //alias is record()

this will return record with id=1 as object

alternatively:

$row = ZORM::row('col_name', 'value');

this will return the most recent record that matches the critera as object

the following actions may be performed on an ORMRecord object:

to see the value of a column on the record

print $row->col_name;

to set a new value:

print $row->col_name = "new value";
$row->commit(); //this writes to DB

to remove the entire record from DB:

$row->destroy(); //alias $row->delete();

Other ways of accessing single records:

ZORM::firstRecord(); //matches first record in the set table

ZORM::firstRecord('col_name', 'val'); //matches first record in the set table that matches the criteria

ZORM::lastRecord(); //as in firstRecord

ZORM::lastRecord('col_name', 'val'); //as in firstRecord

ZORM::findFirst('col_name', 'val'); //find first match on the table for the critera

ZORM::findLast('col_name', 'val'); //find last match on the table for the critera

ZORM::previous(10); //this returns record with id preceeding 10

ZORM::next(10); //this returns record with id after 10

Transactions

ZORM::beginTransaction();
ZORM::commitTransaction();
ZORM::rollbackTransaction();

Others

ZORM::truncate(); //truncates the set table if an array it passed it trucates tables with array value names

ZORM::exists(1); //chcks if record with id=1 exists in the DB and returns a boolean

ZORM::exists('col_name', 'value'); //checks if record with col_name=val exists in the DB and returns a boolean

ZORM::exists(['col_name'=>'value'...]); //checks if record with array pairs as col_name=val exists in the DB and returns a boolean

ZORM::count() or ZORM::length() will return the number of records in the index table

You may change the DB accessed by pointing to a new cnf file:

ZORM::config("/path/to/new_config");

*ZORM was designed deliberately not to be singleton.