/** * */ public function testLinkSetCreation() { $config = static::getConfig('connect'); $client = new PhpOrient('localhost', 2424); $client->configure(array('username' => $config['username'], 'password' => $config['password'])); $client->connect(); try { $client->dbDrop('temp', Constants::STORAGE_TYPE_MEMORY); } catch (\Exception $e) { // echo $e->getMessage(); $client->getTransport()->debug($e->getMessage()); } $client->dbCreate('temp', Constants::STORAGE_TYPE_MEMORY, Constants::DATABASE_TYPE_DOCUMENT); $client->dbOpen('temp'); $client->sqlBatch(' create class Test1; create property Test1.aString string; insert into Test1 (aString) VALUES ("b"),("c"),("d"); create class Test2; create property Test2.aString string; create property Test2.anEmbeddedSetOfString embeddedset string; create property Test2.aLinkedSetOfTest1 linkset Test1;'); $clusterTest1 = $client->query("select classes[name='Test1'].defaultClusterId from 0:1", -1)[0]['classes']; $clusterTest2 = $client->query("select classes[name='Test2'].defaultClusterId from 0:1", -1)[0]['classes']; $this->assertEquals('9', $clusterTest1); $this->assertEquals('10', $clusterTest2); $newRecord = ['oClass' => 'Test2', 'oData' => ['aString' => 'Test record', 'anEmbeddedSetOfString' => ['something 1', 'something 2', 'more other'], 'aLinkedSetOfTest1' => [new ID($clusterTest1, 1), new ID($clusterTest1, 2)]]]; $newRecordObject = Record::fromConfig($newRecord); $newRecordObject->setRid(new ID($clusterTest2)); $tmp = $client->recordCreate($newRecordObject); $this->assertInstanceOf('\\PhpOrient\\Protocols\\Binary\\Data\\Record', $tmp); /** * @var \PhpOrient\Protocols\Binary\Data\Record $record */ $record = $client->recordLoad($tmp->getRid())[0]; $this->assertEquals('Test record', $record->aString); $this->assertArrayHasKey(0, $record->anEmbeddedSetOfString); $this->assertArrayHasKey(1, $record->anEmbeddedSetOfString); $this->assertArrayHasKey(2, $record->anEmbeddedSetOfString); $this->assertEquals('something 1', $record->anEmbeddedSetOfString[0]); $this->assertEquals('something 2', $record->anEmbeddedSetOfString[1]); $this->assertEquals('more other', $record->anEmbeddedSetOfString[2]); $this->assertArrayHasKey(0, $record->aLinkedSetOfTest1); $this->assertArrayHasKey(1, $record->aLinkedSetOfTest1); $this->assertInstanceOf('\\PhpOrient\\Protocols\\Binary\\Data\\ID', $record->aLinkedSetOfTest1[0]); $this->assertInstanceOf('\\PhpOrient\\Protocols\\Binary\\Data\\ID', $record->aLinkedSetOfTest1[1]); $aLinkedSetOfTest1 = $record->aLinkedSetOfTest1; /** * @var \PhpOrient\Protocols\Binary\Data\ID[] $aLinkedSetOfTest1 */ $this->assertEquals('#9:1', $aLinkedSetOfTest1[0]->jsonSerialize()); $this->assertEquals('#9:2', $aLinkedSetOfTest1[1]->__toString()); }
public function testRecordData() { $db_name = 'test_record_data'; $client = new PhpOrient('localhost', 2424); $client->connect('root', 'root'); $this->skipTestByOrientDBVersion(['2.0.13', '1.7.10']); try { $client->dbDrop($db_name, Constants::STORAGE_TYPE_MEMORY); } catch (\Exception $e) { // echo $e->getMessage(); $client->getTransport()->debug($e->getMessage()); } $client->dbCreate($db_name, Constants::STORAGE_TYPE_MEMORY, Constants::DATABASE_TYPE_GRAPH); $client->dbOpen($db_name, 'admin', 'admin'); $client->command("create class Test extends V"); $client->command("create property Test.id string"); $client->command("alter property Test.id DEFAULT uuid()"); $rec = (new Record())->setOData([])->setRid(new ID(11)); $record = $client->recordCreate($rec); $this->assertArrayHasKey('id', $record); $this->assertNotEmpty($record['id']); }
public function testNullValueHandling() { try { $client = new PhpOrient('localhost', 2424); $client->username = '******'; $client->password = '******'; $client->connect(); $this->skipTestByOrientDBVersion(['2.1.3', '2.0.13', '1.7.10']); try { $client->dbDrop('temp', Constants::STORAGE_TYPE_MEMORY, Constants::DATABASE_TYPE_DOCUMENT); } catch (\Exception $e) { // echo $e->getMessage(); $client->getTransport()->debug($e->getMessage()); } $client->dbCreate('temp', Constants::STORAGE_TYPE_MEMORY, Constants::DATABASE_TYPE_GRAPH); $client->dbOpen('temp'); $client->sqlBatch('create class Prova1;' . 'create property Prova1.aString string;' . 'insert into Prova1 (aString) VALUES ("b"),("c"),("d");' . 'create class Prova2;' . 'create property Prova2.aString string;' . 'create property Prova2.anEmbeddedSetOfString embeddedset string;' . 'create property Prova2.prova1 link Prova1;'); $clusterProva1 = $client->query("select classes[name='Prova1'].defaultClusterId from 0:1", -1)[0]['classes']; $clusterProva2 = $client->query("select classes[name='Prova2'].defaultClusterId from 0:1", -1)[0]['classes']; // echo "Default cluster for Prova1: $clusterProva1\n"; // echo "Default cluster for Prova2: $clusterProva2\n\n"; $newRecord = ['oClass' => 'Prova2', 'oData' => ['aString' => 'record di prova', 'anEmbeddedSetOfString' => ['qualcosa 1', 'qualcosa 2', 'ancora altro'], 'prova1' => null]]; $newRecordObject = Record::fromConfig($newRecord); $newRecordObject->setRid(new ID($clusterProva2)); $tmp = $client->recordCreate($newRecordObject); $record = $client->recordLoad($tmp->getRid())[0]; $this->assertEquals('record di prova', $record->getOData()['aString']); $this->assertEquals(null, $record->getOData()['prova1']); // print_r($record->getOData()); } catch (\Exception $e) { // echo $e . "\n"; } }
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>"; echo '<pre>' . json_encode($document->getOData(), JSON_PRETTY_PRINT) . '</pre>'; }