doctrine/mongodb-odm
Doctrine MongoDB ODM is an object document mapper for PHP that brings Doctrine-style persistence to MongoDB. Define documents with metadata, map fields and relations, run queries, and handle unit of work, identity map, and migrations for MongoDB apps.
DocumentManager vs. Laravel’s DB facade). Potential for dual-database architectures (e.g., SQL for transactions, MongoDB for analytics).DocumentManager as a singleton.public function register()
{
$this->app->singleton(DocumentManager::class, function ($app) {
$config = $app['config']['doctrine_mongodb'];
$client = new Client($config['connection']);
return ODM\DocumentManager::create($client, $config['config']);
});
}
doctrine/dbal (if using both SQL and MongoDB).mongodb/mongodb (version alignment required).replace or conflict directives in composer.json.DocumentRepository, PersistentCollection, EmbeddedDocuments).MongoModel extending Document).BulkWriter, unlike Eloquent’s fluent methods.$match, $lookup), bypassing ODM’s QueryBuilder.SearchIndex, VectorSearch) be handled?mongodb/mock) be required?DocumentManager can be registered as a Laravel service.config/doctrine_mongodb.php) to define:
'connection' => [
'server' => 'mongodb://user:pass@host:port',
'options' => ['connectTimeoutMS' => 5000],
],
'config' => [
'document_root' => app_path('Models/MongoDB'),
'proxy_dir' => storage_path('framework/proxies'),
'useNativeLazyObjects' => true, // PHP 8.4+ optimization
],
ModelSaved, ModelDeleted) via Doctrine lifecycle callbacks.doctrine/cache).// Before (Eloquent)
class User extends Model { ... }
// After (ODM)
#[ODM\Document]
class UserDocument extends Document { ... }
EventListeners to trigger MongoDB writes on Eloquent events.QueryBuilder for complex MongoDB aggregations.Resource classes for ODM documents.DocumentManager and DocumentRepository.SchemaManager).SearchIndex, VectorSearchIndex).SchemaManager for safe migrations:
$schemaManager = $documentManager->getSchemaManager();
$schemaManager->ensureIndexesAreCreated();
onFlush, postPersist).How can I help you explore Laravel packages today?