Translators from Russian to English Wanted!


Accessing the Database

The Database class allows you to manage the connection to the database. The usual way to establish a connection to the database is as follows:


$params = array('path' => './podb.fs', 'mode' => 'r', 'handler' => 'db3');
$podb = PODB::connect("Berkeley", $params);

$root =& $podb->get_root();




Database Storage Handlers


$params = array('host' => 'localhost',
                'user' => 'root',
                'pass' => '',
                'database' => 'podb');
$podb = PODB::connect("MySQL", $params);


$params = array('path' => './test6.fs',
                'mode' => 'w-',
                'handler' => 'db3',
                'persistent' => true);
$podb = PODB::connect("Berkeley", $params);


$params = array('path' => './test6.fs', 
                'perm' => O666,
                'persistent' => true);
$podb = PODB::connect("SQLite", $params);


Transaction access

To update the database, a transaction has to be started. Starting the transaction must be done explicitly; there is no implicit transaction when accessing the database. Only one transaction at a time can be opened on a connection.

$podb = PODB::connect("Berkeley", $params);

// update your data

// Commit the changes


Version access

Only one version access at a time can be opened on a connection. The argument of the versionBegin method, when it is specified, must be the name of a database version.

$params = array('path' => './test2.fs', 'mode' => 'w-', 'handler' => 'db3');
$podb = PODB::connect("Berkeley", $params);
$podb->versionBegin('test'); $podb->begin();
// Working with object in version test
$podb->commit(); $podb->close();


$podb = PODB::connect("Berkeley", );
if (
$podb->versionExists('test')) { // Starting version access and applying changes in version // to default object storage. $podb->versionBegin('test');     $podb->versionCommit();     $podb->versionDrop(); }


Persistent like PHP Class

Object of any class derived from Persistent base class is considered as persistent capable.

class Counter extends Persistent { 
    var  =
0;     function count() {         $this->setChanged(); // Talking storage this object is changed.         return ++;     } }


Root Object

The storage has one special root object. Root object is the only persistent object accessed in the special way (using get_root method). All other persistent objects are accessed in normal way: either by traversing by references from another persistent objects, either using ReferenceList or ReferenceMap objects. Unlike many other OODBMS, there can be only one root in the storage. If you need to have several named roots, you should create Persistent object with attribute RelationMap object and use it as root object.

When you create new storage get_root() returns NULL. You should create root object and store reference to it by set_root() method. Next time you are opening storage, root object can be retrieved by get_root().

$podb = PODB::connect("Berkeley", );
$root =& $podb->get_root();


Updating an Objects

Как уже говорилось, изменение базы данных возможно только в доступе к базе внутри запущенной транзакции.

Пример реализации счетчика посещений. Класс Counter используемый в примере описан выше.

$podb = PODB::connect("Berkeley", $params);

$counter =& $podb->get_root();
if (
$counter == null || get_class($counter) != 'counter') {     $counter = new Counter();     $podb->set_root($counter); } echo "Counter value: ".$counter->count()."\n";
// Commit the changes $podb->commit(); $podb->close();


Manipulating Relationships

class Root extends Persistent {
    var $folders;

class Folder extends Persistent {
    var $name;

    function Folder($n) {
        $this->name = $n;

$podb = PODB::connect("Berkeley", $params);

$root = new Root();
$root->foders = $podb->createRelationList();

$folder1 = new Folder("Folder1");
$folder2 = new Folder("Folder2");

$root->folders->insert($folder1); $root->folders->insert($folder2); $podb->set_root($root); $podb->commit(); $podb->close();


Accessing Relationship Objects

In an example shown above, we have created the small structure of the data consisting of object Root containing a little bit objects Folder. Now we shall consider a retrieving method of these objects:

$podb = PODB::connect("Berkeley", $params);

$root = $podb->get_root();

$folders = $root->folders->iterator();
$folder =& $folders->current();

while ($folder) {
"Folder: ".$folder->name." OID ".$folder->oid."\n";     $folder =& $folders->next(); } $podb->close();


Removing Objects




Copyright © 2004
Sergey Volkov

SourceForge.net Logo