Installation Add the bundle to your Symfony project via Composer:
composer require alguilla/arangodb-ogm-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Alguilla\ArangoDbOgmBundle\AlguillaArangoDbOgmBundle::class => ['all' => true],
];
Configuration
Define your ArangoDB connection in config/packages/alguilla_arangodb_ogm.yaml:
alguilla_arangodb_ogm:
clients:
default:
host: '%env(ARANGO_HOST)%'
port: '%env(int:ARANGO_PORT)%'
username: '%env(ARANGO_USER)%'
password: '%env(ARANGO_PASSWORD)%'
First Model
Create a document model (e.g., src/Document/User.php):
namespace App\Document;
use Alguilla\ArangoDbOgmBundle\Model\Document;
class User extends Document
{
/** @var string */
public $name;
/** @var string */
public $email;
}
First Query Use the repository to interact with ArangoDB:
use App\Document\User;
use Alguilla\ArangoDbOgmBundle\Repository\DocumentRepository;
$repository = $this->get(DocumentRepository::class);
$user = $repository->findOneBy(['email' => 'user@example.com']);
CRUD Operations
save() on a new document instance.
$user = new User();
$user->name = 'John Doe';
$user->email = 'john@example.com';
$repository->save($user);
save() again.remove() or delete().
$repository->remove($user);
Querying
find(), findOneBy(), or findBy().
$users = $repository->findBy(['active' => true]);
executeQuery().
$result = $repository->executeQuery('FOR u IN users FILTER u.active RETURN u');
Relationships
@var annotations or properties.
class Post extends Document
{
/** @var User */
public $author;
}
getRelation() to fetch related documents.
$post = $repository->findOneBy(['id' => 123]);
$author = $post->getRelation('author');
Collections
setCollection().
$repository->setCollection('custom_users');
Transactions
beginTransaction() and commit() for atomic operations.
$client = $this->get('alguilla_arangodb_ogm.client.default');
$client->beginTransaction();
try {
$repository->save($user);
$client->commit();
} catch (\Exception $e) {
$client->rollback();
}
Symfony Events
kernel.request).$this->get('alguilla_arangodb_ogm.client.default')->executeQuery('...');
Doctrine Bridge
Caching
$cache = $this->get('cache.app');
$key = 'users_active';
$users = $cache->get($key, function() use ($repository) {
return $repository->findBy(['active' => true]);
});
Migrations
$schema = $this->get('alguilla_arangodb_ogm.schema');
$schema->createCollection('users', ['numberOfShards' => 3]);
Collection Naming
User → user). Override with @Collection annotation:
/** @Collection("custom_users") */
class User extends Document {}
Primary Key Conflicts
_key (ArangoDB’s primary key) is unique. The bundle auto-generates it if not provided, but manual assignments must be unique.Transactions and Connections
commit() or rollback() will leave transactions open.AQL vs. OGM Queries
Edge Cases in Relationships
$post->setAuthor($user);
$user->addPost($post); // Assuming a method like this exists
Enable Logging Configure Monolog to log ArangoDB queries:
monolog:
handlers:
arango:
type: stream
path: "%kernel.logs_dir%/arangodb.log"
level: debug
channels: ["arangodb"]
Then enable the channel in the bundle config:
alguilla_arangodb_ogm:
logging: true
Query Profiling Use ArangoDB’s built-in profiler or enable slow query logging in the bundle config:
alguilla_arangodb_ogm:
clients:
default:
slow_query_threshold: 100 # Log queries slower than 100ms
Connection Issues
ping() to test connections:
$client->ping();
Custom Hydrators
Alguilla\ArangoDbOgmBundle\Hydrator\HydratorInterface.Event Subscribers
prePersist, postLoad) via Symfony’s event dispatcher:
$dispatcher->addListener(DocumentEvents::PRE_PERSIST, function($event) {
$document = $event->getDocument();
// Pre-save logic
});
Custom Repositories
DocumentRepository to add domain-specific methods:
class UserRepository extends DocumentRepository
{
public function findActiveUsers()
{
return $this->findBy(['active' => true]);
}
}
services.yaml:
App\Repository\UserRepository:
arguments:
$class: App\Document\User
tags: ['alguilla_arangodb_ogm.repository']
Schema Customization
$schema->ensureIndex('users', ['email'], ['unique' => true]);
How can I help you explore Laravel packages today?