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: 15 years ago
Size: 1,317 bytes
 

Contents

Class file image Download
<?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'


?>