<?php
 
 
/*    
 
    http://coolpenguin.net/persistclass 
 
    for updates, documentation, tutorials
 
*/
 
 
// connect to database (executing demo 1)
 
require('demo1-connection.php');
 
 
// get a connection object
 
$con = DbConnectionPool::instance()->getConnection();
 
 
// defining an object for a test
 
class TestTable extends PersistClass {
 
    protected $sqlTableName = 'TESTTABLE';
 
    protected $sqlPrimaryKey = 'testid';
 
}
 
 
// retrieving many records
 
$ids = $con->queryFirstColumnSet('SELECT testid from testtable');
 
$testObj = new TestTable();
 
$objs = $testObj->getItemsWithIds($ids);
 
// an alternative is to iterate $objs[] = new TestTable($id);
 
 
// reading these records normally takes 1000 reads from the database. There is an alternate way of initializing an object, simply passing an array of column content to its constructor.
 
// this makes it possible to easily optimize for high scale:
 
$objs = array();
 
$q = 'select * from TESTTABLE';
 
while($row = $con->next()) {
 
    $objs[] = new TestTable($row);
 
}
 
 
// note: getAllItems() is optimized already, and no need to perform the optimization above. 
 
// note: getItemsWithIds() is not optimized by default. The reason for that is getItemsWithIds() keeps the order of the ids its given - thats more usual in practical use
 
 
echo 'Test successful'
 
 
 
?>
 
 |