<?php

use PhpOrient\PhpOrient;
use PhpOrient\Protocols\Binary\Data\ID;
use PhpOrient\Protocols\Binary\Data\Record;
require 'vendor/autoload.php';
$client = new PhpOrient('orientdb3', 2424);
$client->connect('root', 'rootpwd');
if ($client->dbExists('demo')) {
    $client->dbDrop('demo');
}
$client->dbCreate('demo');
$client->dbOpen('demo');
// Create a new collection
$clusterId = $client->command('create class Elements extends V');
// Add new documents
$document1 = new Record();
$document1->setOData(['now' => date('Y-m-d H:i:s')]);
$document1->setRid(new ID($clusterId));
$document2 = new Record();
$document2->setOData(["created" => ['during' => ['VilniusPHP', 'event']]]);
$document2->setRid(new ID($clusterId));
$record1 = $client->recordCreate($document1);
$record2 = $client->recordCreate($document2);
// Read them all
$documents = $client->query('select from Elements');
foreach ($documents as $document) {
    /** @var $document Record */
    echo '<h2>' . $document->getRid() . '</h2>';
    echo "<div><b>Class:</b> {$document->getOClass()}</b>";
    echo "<div><b>Revision:</b> {$document->getVersion()}</b>";
示例#2
0
 public function testDatabaseOps()
 {
     // ROOT Table creation
     $client = new PhpOrient('localhost', 2424);
     $client->setSessionToken(true);
     $clusterID = $client->connect('root', 'root');
     if ($client->dbExists('new_test_db_2')) {
         $client->dbDrop('new_test_db_2', PhpOrient::STORAGE_TYPE_MEMORY);
     }
     $client->dbCreate('new_test_db_2', PhpOrient::STORAGE_TYPE_MEMORY);
     $this->assertNotEmpty($clusterID);
     $this->assertNotEmpty($client->getSessionToken());
     $this->assertEmpty($this->client->getSessionToken());
     $this->client->setSessionToken(true);
     $this->client->dbOpen('new_test_db_2', 'admin', 'admin');
     $admin_Token = $this->client->getSessionToken();
     $c = $this->client->command("Create class test_class extends V");
     $rec = (new Record())->setOClass('test_class')->setOData([true])->setRid(new ID(-1, -1));
     $rec = $this->client->recordCreate($rec);
     $rec1 = $this->client->recordLoad($rec->getRid());
     $rec2 = $this->client->query("select from {$rec->getRid()}");
     $this->assertEquals($rec1, $rec2);
     // useful?? they are empty
     //renew the token and connect to new database as user
     $this->client->setSessionToken(true);
     $this->client->dbOpen('new_test_db_2', 'admin', 'admin');
     $new_token = $this->client->getSessionToken();
     $this->assertNotEquals($admin_Token, $new_token);
     $GLOBALS['old_db_token'] = $new_token;
     $rec = (new Record())->setOClass('test_class')->setOData([true])->setRid(new ID(-1, -1));
     $rec = $this->client->recordCreate($rec);
     $rec2 = $this->client->query("select from {$rec->getRid()}");
     $this->assertEquals($rec, $rec2[0]);
     $this->client->setSessionToken($admin_Token);
     //Not valid token to perform database ops
     $this->setExpectedException('\\PhpOrient\\Exceptions\\PhpOrientException');
     $this->client->dbDrop('new_test_db_2');
 }