doctrine/orientdb-odm
Doctrine OrientDB ODM integrates OrientDB with Doctrine, offering an object document mapper for PHP. Map documents to classes, manage persistence and queries via a familiar Doctrine-style API, and work with graph/document features using a structured domain model.
Installation
composer require doctrine/odm-orientdb
Since the package is archived, ensure compatibility with your PHP version (8.0+ recommended).
Basic Configuration
Create a config/orientdb.php file:
return [
'connection' => [
'host' => env('ORIENTDB_HOST', 'localhost'),
'port' => env('ORIENTDB_PORT', 2424),
'username' => env('ORIENTDB_USERNAME', 'root'),
'password' => env('ORIENTDB_PASSWORD', 'root'),
'database' => env('ORIENTDB_DATABASE', 'test'),
],
'driver_options' => [
'connect_timeout' => 2,
],
];
First Use Case: Connecting to OrientDB
Register the Doctrine service provider in config/app.php:
'providers' => [
// ...
Doctrine\ODM\OrientDB\OrientDBServiceProvider::class,
],
Publish the config:
php artisan vendor:publish --provider="Doctrine\ODM\OrientDB\OrientDBServiceProvider"
Define a Model
namespace App\Models;
use Doctrine\ODM\OrientDB\Mapping\Annotation as ODM;
/** @ODM\Document */
class User
{
/** @ODM\Id */
private $id;
/** @ODM\Field(type="string") */
private $name;
// Getters and setters...
}
Basic CRUD Operations
use Doctrine\ODM\OrientDB\DocumentManager;
$dm = app(DocumentManager::class);
$user = new User();
$user->setName('John Doe');
$dm->persist($user);
$dm->flush();
Create a Custom Repository
namespace App\Repositories;
use Doctrine\ODM\OrientDB\DocumentRepository;
use App\Models\User;
class UserRepository extends DocumentRepository
{
public function findByName(string $name): ?User
{
return $this->findOneBy(['name' => $name]);
}
}
Bind the Repository in Laravel
// In a service provider
$this->app->bind(
UserRepository::class,
function ($app) {
return $app->make(DocumentManager::class)
->getRepository(User::class);
}
);
Usage in Controllers
public function show(UserRepository $userRepository)
{
$user = $userRepository->findByName('John Doe');
return response()->json($user);
}
Leverage Laravel’s Eloquent-like Facades
Create a facade for DocumentManager:
// app/Facades/OrientDB.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class OrientDB extends Facade
{
protected static function getFacadeAccessor()
{
return 'orientdb.document_manager';
}
}
Register it in a service provider:
$this->app->singleton('orientdb.document_manager', function ($app) {
return $app->make(DocumentManager::class);
});
Event Listeners for Document Lifecycle
// Listen to pre-persist events
$dm->getEventManager()->addEventListener(
array('prePersist'),
function ($event) {
$document = $event->getDocument();
$document->setCreatedAt(new \DateTime());
}
);
Query Builder for Complex Queries
$query = $dm->createQueryBuilder('User')
->field('name')->equals('John Doe')
->field('age')->greaterThan(25)
->getQuery();
$users = $query->getResult();
Transactions
$dm->beginTransaction();
try {
$dm->persist($user1);
$dm->persist($user2);
$dm->flush();
$dm->commit();
} catch (\Exception $e) {
$dm->rollback();
throw $e;
}
Archived Package Risks
Schema Migrations
php artisan orientdb:schema:update --force
$dm->getConnection()->createIndex(
'UserNameIndex',
'User',
'name',
'UNIQUE'
);
Caching Queries
orientdb.php:
'query_cache' => [
'enabled' => true,
'ttl' => 3600,
],
Embedded Documents
@ODM\EmbeddedDocument for nested objects, but be mindful of serialization limits:
/** @ODM\EmbeddedDocument */
class Address
{
/** @ODM\Field(type="string") */
private $street;
// ...
}
Connection Pooling
$config = $dm->getConfiguration();
$config->setDriverConfig([
'connect_timeout' => 2,
'reconnect' => true,
]);
Enable SQL Logging
$config = $dm->getConfiguration();
$config->setSQLLogger(new \Doctrine\ODM\OrientDB\Logging\EchoSQLLogger());
Check OrientDB Logs
config/log/orientdb.log (default). Enable debug mode in orientdb.php:
'logging' => [
'level' => \Monolog\Logger::DEBUG,
],
Common Errors
Doctrine\Common\Annotations\AnnotationRegistry.2424 by default).Custom Hydrators Override how documents are hydrated from the database:
$dm->getHydrator()->registerHydrator(
'App\Models\User',
new \Doctrine\ODM\OrientDB\Hydrator\CustomHydrator()
);
Event Subscribers Extend functionality via event listeners (e.g., soft deletes):
$dm->getEventManager()->addEventSubscriber(
new class implements \Doctrine\ODM\OrientDB\Event\LifecycleEventSubscriber {
public function preRemove(\Doctrine\ODM\OrientDB\Event\LifecycleEventArgs $args)
{
$args->getDocument()->setDeleted(true);
$args->getDocument()->setDeletedAt(new \DateTime());
}
}
);
Custom Types Register custom field types (e.g., for JSON):
$config = $dm->getConfiguration();
$config->addCustomType('json', 'App\Doctrine\Types\JsonType');
Bulk Operations Use OrientDB’s native bulk APIs for performance:
$dm->getConnection()->executeBulkQuery(
'UPDATE User SET age = age + 1 WHERE age > 25'
);
How can I help you explore Laravel packages today?