Skip to content

helderjs/doctrine-mongo-odm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DoctrineMongoODM Component

Build Status

It's a component based on DoctrineMongoODMModule that provides DoctrineMongoDbODM integration for several (Micro-)frameworks. The goal is be light and easy to configure, for that the library rely just on the PSR-11 and MongoBD ODM.

Requirements

ps.: if using mongo-php-adapter you should add and install it first in you project. After this the library will declare to composer that it provides the ext-mongo.

We recommend using a dependency injection container, and typehint against PSR-11.

Installation

Install this library using composer:

$ composer require helderjs/doctrine-mongo-odm

Configuration

How to create the config file (What you should/can put in the config).

return [
    'config' => [
        ...
        'doctrine' => [
            'default' => 'odm_default',
            'connection' => [
                'odm_default' => [
                    'server'           => 'localhost',
                    'port'             => '27017',
                    'user'             => 'myUser',
                    'password'         => 'myHardPassword',
                    'dbname'           => 'dbName',
                    'options'          => []
                ],
                'odm_secondary' => [
                    'connectionString' => 'mongodb://username:password@server2:27017/mydb',
                    'options'          => []
                ],
            ],
            'driver' => [
                'odm_default' => [
                    \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::class => [
                        'documents_dir' => ['./src/myApp/Documents']
                    ], 
                    \Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver::class => [
                        'simplified' => false,
                        'xml_dir' => [
                            '/path/to/files1',
                            '/path/to/files2',
                        ]
                     ],
                    \Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver::class => [
                        'simplified' => false,
                        'yml_dir' => [
                            '/path/to/files1',
                            '/path/to/files2',
                        ]
                    ],
                    \Doctrine\ODM\MongoDB\Mapping\Driver\MappingDriverChain::class => [
                        'Driver\Annotation' => \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::class,
                        'Driver\Xml' => \Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver::class,
                        'Driver\Yaml' => \Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver::class,
                    ],
                ],
            ],
            'configuration' => [
                'odm_default' => [
                    'metadata_cache'     => \Doctrine\Common\Cache\ArrayCache::class, // optional
                    'driver'             => \Doctrine\ODM\MongoDB\Mapping\Driver\MappingDriverChain::class,
                    'generate_proxies'   => true,
                    'proxy_dir'          => 'data/DoctrineMongoODMModule/Proxy',
                    'proxy_namespace'    => 'DoctrineMongoODMModule\Proxy',
                    'generate_hydrators' => true,
                    'hydrator_dir'       => 'data/DoctrineMongoODMModule/Hydrator',
                    'hydrator_namespace' => 'DoctrineMongoODMModule\Hydrator',
                    'default_db'         => 'MyDBName',
                    'filters'            => [], // custom filters (optional)
                    'types'              => [], // custom types (optional)
                    'retry_connect'      => 0 // optional
                    'retry_query'        => 0 // optional
                    'logger'             => \MyLogger::calss \\ Logger implementation (optional)
                    'classMetadataFactoryName' => 'stdClass' \\ optional
                ]
            ],
            'documentmanager' => [
                'odm_default' => [
                    'connection'    => \Doctrine\ODM\MongoDB\Connection::class,
                    'configuration' => \Doctrine\ODM\MongoDB\Configuration::class,
                    'eventmanager'  => \Doctrine\ODM\MongoDB\EventManager::class, \\ optional
                ],
                'odm_secondary' => [
                    'connection'    => 'doctrine.connection.secondary',
                    'configuration' => \Doctrine\ODM\MongoDB\Configuration::class,
                    'eventmanager'  => 'doctrine.eventmanager.secondary', \\ optional
                ]
            ],
            'eventmanager' => [ \\ optional
                'odm_default' => [
                    'subscribers' => [
                        \MySubscriberImpl1::class,
                    ],
                ],
                'odm_secondary' => [
                    'subscribers' => [
                        new \MySubscriberImpl2(),
                    ],
                ],
            ],
        ],
        ...
    ],
];

Configuring DI at Zend Expressive

...
'dependencies' => [
    'invokables' => [
        \Doctrine\Common\Cache\ArrayCache::class => \Doctrine\Common\Cache\ArrayCache::class,
        \MyLogger::class  => \MyLogger::class,
    ],
    'factories' => [
        \Doctrine\ODM\MongoDB\Configuration::class   => ConfigurationFactory::class,
        \Doctrine\ODM\MongoDB\Connection::class      => ConnectionFactory::class,
        \Doctrine\ODM\MongoDB\EventManager::class    => EventManagerFactory::class,
        \Doctrine\ODM\MongoDB\DocumentManager::class => DocumentManagerFactory::class,
        'doctrine.connection.secondary'              => new ConnectionFactory('odm_secondary'),
        'doctrine.eventmanager.secondary'            => new EventManagerFactory('odm_secondary'),
        'doctrine.documentmandager.secondary'        => new DocumentManagerFactory('odm_secondary'),
        \Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver::class   => \Helderjs\Component\DoctrineMongoODM\AnnotationDriverFactory::class,
        \Doctrine\ODM\MongoDB\Mapping\Driver\XmlDriver::class          => \Helderjs\Component\DoctrineMongoODM\AnnotationDriverFactory::class,
        \Doctrine\ODM\MongoDB\Mapping\Driver\YamlDriver::class         => \Helderjs\Component\DoctrineMongoODM\AnnotationDriverFactory::class,
        \Doctrine\ODM\MongoDB\Mapping\Driver\MappingDriverChain::class => \Helderjs\Component\DoctrineMongoODM\AnnotationDriverFactory::class,
    ],
 ],
 ...

SlimPHP

$container['doctrine-connection'] = function ($container) {
    $factory = new ConnectionFactory();
    
    return $factory($container);
};

$container['doctrine-configuration'] = function ($container) {
    $factory = new ConfigurationFactory();
    
    return $factory($container);
};

$container['doctrine-eventmanager'] = function ($container) {
    $factory = new EventManagerFactory();
    
    return $factory($container);
};

$container['doctrine-driver'] = function ($container) {
    $factory = new MappingDriverChainFactory();
    
    return $factory($container);
};

Goals

  • Improve unit tests
  • Improve documentation
  • Implementing real examples
  • ?introduce new features?

If you want to help: install, test it, report an issue, fork, open a pull request

License

The MIT License (MIT). Please see License File for more information.

About

DoctrineMongoODM component for (Micro-)Framework

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages