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:
include_once("./PODB.php"); $params = array('path' => './podb.fs', 'mode' => 'r', 'handler' => 'db3'); $podb = PODB::connect("Berkeley", $params); $root =& $podb->get_root(); var_dump($root); $podb->close(); |
MySQL
$params = array('host' => 'localhost', 'user' => 'root', 'pass' => '', 'database' => 'podb'); |
Berkeley
$params = array('path' => './test6.fs', 'mode' => 'w-', 'handler' => 'db3', 'persistent' => true); $podb = PODB::connect("Berkeley", $params); |
SQLite
$params = array('path' => './test6.fs', 'perm' => O666, 'persistent' => true); $podb = PODB::connect("SQLite", $params); |
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); $podb->begin(); // update your data // Commit the changes $podb->commit(); $podb->close(); |
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", ); |
Object of any class derived from Persistent base class is considered as persistent capable.
class Counter extends Persistent { |
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(); $podb->close(); |
Как уже говорилось, изменение базы данных возможно только в доступе к базе внутри запущенной транзакции.
Пример реализации счетчика посещений. Класс Counter
используемый
в примере описан выше.
$podb = PODB::connect("Berkeley", $params); $podb->begin(); |
class Root extends Persistent { var $folders; } class Folder extends Persistent { var $name; function Folder($n) { $this->name = $n; } } $podb = PODB::connect("Berkeley", $params); $podb->begin(); $root = new Root(); $root->foders = $podb->createRelationList(); $folder1 = new Folder("Folder1"); $folder2 = new Folder("Folder2"); |
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) { |
Copyright © 2004
Sergey Volkov