RPL AR
From Rhypedigital
[edit] Active Record
The active record pattern is a mechanism for accessing records from a database. As well as hiding the raw SQL, it supports operations like update and insert, and stepping through multiple records.
In RiPHPLib an active record is created by specifying the database connection, the table name, and the columns (the second true argument to the first addColumn specifies this is the primary key column.
$ar = new RPL_DB_ActiveRecord ($dbh, 'people') ; $ar->addColumn ('id', true) ; $ar->addColumn ('forename') ; $ar->addColumn ('surname' ) ;
Individual records can then be located and modified, whence the record for Fred Bloggs is updated to Fredrica Bloggs:
$ar->addWhere ('surname = ? and forname = ?', 'Fred', 'Bloggs') ; $ar->find () ; $ar->surname = 'Fredrica' ; $ar->save () ;
Fetching multiple records, and stepping through them is supported:
$ar->setMultiple (true) ; $ar->find() ; while (! $ar->eof()) { // Do something .... $ar->next() ; }
The RPL_DB_CompoundRecord supports fetching from joined tables using multiple active records. RPL_DB_SQLRecord provides an read-only active-record like interface using arbitrary SQL select queries.
