PHP Classes

File: demo7-optimizingscalability.php

Recommend this page to a friend!
  Classes of Bob Gombocki   PersistClass   demo7-optimizingscalability.php   Download  
File: demo7-optimizingscalability.php
Role: Example script
Content type: text/plain
Description: Optimizing for high-scale
Class: PersistClass
DB access wrapper & storing objects in DB tables
Author: By
Last change: added web tutorial info
Date: 13 years ago
Size: 1,317 bytes


Class file image Download

    for updates, documentation, tutorials

// connect to database (executing demo 1)

// get a connection object
$con = DbConnectionPool::instance()->getConnection();

// defining an object for a test
class TestTable extends PersistClass {
$sqlTableName = 'TESTTABLE';
$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';
$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'