ostico/phporient
PhpOrient is a stable PHP driver for OrientDB using the binary protocol. Supports OrientDB 1.7.4+ and PHP 5.4+ (socket). Works on 32/64-bit; treats numbers as strings for consistency, with BCMath/GMP recommended on 32-bit.
Installation
composer require ostico/phporient
Ensure your OrientDB 3.x server is running (default: localhost:2424). This release drops support for versions below 3.0.
Basic Connection
use Ostico\PhpOrient\Client;
$client = new Client('localhost', 2424, 'root', 'root', [
'protocol' => 'binary', // Default, but explicitly supported in 3.x
'security' => false, // Enable if using TLS in 3.x
]);
$database = $client->connect('testDB');
First Query (CRUD Example)
// Create a record (OrientDB 3.x syntax)
$record = $database->createRecord('V', ['name' => 'Test']);
$database->save($record);
// Fetch with 3.x-compatible query
$result = $database->query("SELECT FROM V WHERE name = 'Test'");
$record = $result->one();
Key Files
src/Client.php: Updated for OrientDB 3.x protocol handshake.src/Database.php: Added 3.x-specific query optimizations.src/Record.php: Enhanced type validation for 3.x schema constraints.Schema Management (3.x Features)
// Create a class with 3.x schema constraints
$database->createClass('User', [
'name' => ['type' => 'STRING', 'mandatory' => true],
'age' => ['type' => 'INTEGER', 'min' => 0],
'metadata' => ['type' => 'EMBEDDEDMAP']
]);
// Drop with 3.x safety checks
$database->dropClass('User', ['force' => false]); // Defaults to false in 3.x
Querying with Parameters (3.x Optimized)
$users = $database->query(
"SELECT FROM User WHERE age > :age AND name CONTAINS :name",
['age' => 25, 'name' => 'A%'],
['index' => 'User_age_idx'] // Leverage 3.x indexes
);
Transactions (3.x ACID Compliance)
$database->beginTransaction();
try {
$record = $database->createRecord('User', ['name' => 'Alice']);
$database->save($record);
$database->commit(); // 3.x enforces stricter transaction isolation
} catch (\Exception $e) {
$database->rollback();
}
Bulk Operations (3.x Batch API)
$users = [
['name' => 'Bob', 'age' => 30],
['name' => 'Charlie', 'age' => 35],
];
$database->bulkInsert('User', $users, ['batchSize' => 50]); // 3.x batch optimization
Edge (Relationship) Handling (3.x Directed Graphs)
$user = $database->query("SELECT FROM User WHERE name = 'Alice'")->one();
$friend = $database->query("SELECT FROM User WHERE name = 'Bob'")->one();
$user->addEdge('knows', $friend, ['since' => 2020]); // 3.x edge properties
$database->save($user);
ostico/phporient-laravel (v1.2+) for 3.x ORM integration.Cache::forget('orientdb:schema:User');
$client = new Client('localhost', 2424, 'root', 'root', [
'security' => true,
'tls' => ['cert' => '/path/to/cert.pem']
]);
Binary Protocol Changes
ostico/phporient to ^1.2 for 2.x compatibility.ext/protocol or use the new BinaryProtocol3 class:
$client = new Client(..., ['protocol' => 'binary3']);
Connection Pooling (3.x Strictness)
$client and $database:
// Anti-pattern (avoid)
foreach ($users as $user) {
$newClient = new Client(...); // Creates new connection per loop
}
Type Handling (3.x Schema Enforcement)
MapType::EMBEDDEDMAP for nested data:
// Wrong (3.x will reject)
$record->setField('metadata', ['key' => 'value']);
// Correct
$record->setField('metadata', new EmbeddedMap(['key' => 'value']));
Query Deprecations
SELECT @rid syntax. Use SELECT FROM V WHERE @rid = #12:0 instead.LET for subqueries:
$database->query("
LET $users = SELECT FROM User
SELECT FROM $users WHERE age > 25
");
Enable 3.x Debug Mode:
$client = new Client(..., ['debug' => true, 'logLevel' => 'verbose']);
Check logs for protocol version mismatches.
Validate Schema Compatibility:
if (!$database->isSchemaCompatible()) {
throw new \RuntimeException("Schema version mismatch. Upgrade OrientDB.");
}
Handle 3.x-Specific Errors:
try {
$database->query("INVALID SYNTAX");
} catch (\Ostico\PhpOrient\Exception\SyntaxError $e) {
// 3.x throws SyntaxError for malformed queries
}
Custom Protocol Handlers (3.x)
Extend Ostico\PhpOrient\Protocol\BinaryProtocol3 for 3.x-specific commands:
class CustomProtocol3 extends BinaryProtocol3 {
public function customCommand($command) {
return $this->sendCommand(['custom' => $command]);
}
}
Event Listeners (3.x Schema Events) Hook into 3.x schema change events:
$database->on('schemaChange', function($event) {
Log::info("Schema changed: {$event->getClass()}");
});
Query Builder (3.x Features) Add 3.x-specific methods:
class QueryBuilder {
public function let($alias, $query) {
return new static("LET $alias = " . $query);
}
}
How can I help you explore Laravel packages today?