azam/odm

Doctrine MongoDB ODM 与 Laravel 集成。旨在与其他框架兼容。

v0.2.10 2017-05-05 09:16 UTC

This package is auto-updated.

Last update: 2024-09-14 23:51:05 UTC


README

这是一个帮助将 Doctrine MongoDb ODM 集成到您选择的任何框架中的包。目前唯一的限制是,通过定义缓存提供者,您声明正在使用 Laravel。如果您使用其他框架,您需要实现自己的缓存解决方案。

Laravel 配置

转到 config/app.php 并将服务提供者添加到 'providers' 部分: CImrie\Odm\OdmServiceProvider::class

发布配置文件: php artisan vendor:publish --tag=odm

现在您可以像这样使用默认的文档管理器

 <?php
 
 use App\Http\Controllers\Controller;
 use Doctrine\ODM\MongoDB\DocumentManager;
 
 class IndexController extends Controller {
     
     public function index(DocumentManager $documentManager) {
         // code as normal ...
         
         $documentManager->flush(); 
         // etc.
     }
     
 }

通用设置

此包主要针对 Laravel,但为其他框架设置并非不可能,并且该包仍然使手动配置 ODM 更加容易。

要生成文档管理器,您可以执行以下操作

<?php

use CImrie\ODM\DocumentManagerFactory;
use CImrie\ODM\Configuration\OdmConfigurationFactory;
use CImrie\ODM\Configuration\Connections\ConnectionResolver;
use CImrie\ODM\Configuration\MetaData\MetaDataRegistry;
use \CImrie\ODM\Common\Registries\ListenerRegistry;
use CImrie\ODM\Configuration\MetaData\Annotations;

$connectionFactories = [
    new \CImrie\ODM\Configuration\Connections\MongodbConnectionFactory()
];

$config = [
              'mongodb' => [
                    'driver'   => 'mongodb',
                    'host'     => 'localhost',
                    'port'     => 27017,
                    'database' => 'odm',
                    'username' => 'secretuser',
                    'password' => 'secretpass',
                    'options'  => [
                        'database' => 'admin',
                    ],
              ]
          ];

$metaDataDrivers = [
    new Annotations()
];  

$dmFactory = new DocumentManagerFactory(
      new OdmConfigurationFactory(),
      new ConnectionResolver($connectionFactories, $config),
      new MetaDataRegistry([Annotations::class]),
      null, //cache manager
      new ListenerRegistry(),
      null //logger, which can be created by extending CImrie\ODM\Logging\Logger
);

/*
 *  <!------------- WARNING -------------!>
 */
//TODO - THIS PART OF THE DOCS IS NOT COMPLETE

$dmFactory->create(new \CImrie\ODM\Common\Config(
    [
        
    ],
    [
        
    ],
    [
        'database' => [
            'connections' => [
            ]     
        ]
    ]
));