The DoctrineORMModule module intends to integrate Doctrine 2 ORM with Zend Framework 2 quickly and easily. The following features are intended to work out of the box:
- Doctrine ORM support
- Multiple ORM entity managers
- Multiple DBAL connections
- Support reuse existing PDO connections in DBAL
Installation of this module uses composer. For composer documentation, please refer to getcomposer.org.
-
cd my/project/directory
-
create a
composer.json
file with following contents (minimum stability is required since the module still has frequent updates):{ "minimum-stability": "dev", "require": { "doctrine/doctrine-orm-module": "dev-master" } }
-
run
php composer.phar install
-
open
my/project/directory/config/application.config.php
and addDoctrineModule
andDoctrineORMModule
to yourmodules
-
create directory
my/project/directory/data/DoctrineORMModule/Proxy
and make sure your application has write access to it. This directory can be changed using the module options.
-
install DoctrineModule
-
clone this module to
vendor/DoctrineORMModule
-
setup PSR-0 autoloading for namespace
DoctrineORMModule
(the directory where the classes in this namespace live isvendor/DoctrineORMModule/src/DoctrineORMModule
. -
The module depends on Doctrine ORM 2.3.*, Doctrine DBAL 2.3.*, Doctrine Migrations. You have to download/install those packages and have them autoloaded.
-
open
my/project/directory/configs/application.config.php
and add the following key to yourmodules
:'DoctrineModule', 'DoctrineORMModule',
To register drivers with Doctrine module simply add the drivers to the doctrine.driver key in your configuration.
<?php
return array(
'doctrine' => array(
'driver' => array(
'my_annotation_driver' => array(
'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver',
'cache' => 'array',
'paths' => array('path/to/my/entities', 'another/path/if/i/want')
)
)
)
);
By default, this module ships with a DriverChain so that modules can add their entities to the chain. Once you have setup your driver you should add it to the chain as follows:
<?php
return array(
'doctrine' => array(
'driver' => array(
'orm_default' => array(
'drivers' => array(
'My\Namespace' => 'my_annotation_driver'
)
)
)
)
);
You also have access to the chain directly via the doctrine.driver.orm_default
service and you can manipulate the
chain however you wish and/or add drivers to it directly without using the driver factory and configuration array. A
good place to do this is the onBootstrap()
method of your Module.php
file or in another service.
Setup your connection by adding the module configuration to any valid ZF2 config file. This can be any file in autoload/ or a module configuration (such as the Application/config/module.config.php file).
<?php
return array(
'doctrine' => array(
'connection' => array(
'orm_default' => array(
'driverClass' => 'Doctrine\DBAL\Driver\PDOMySql\Driver',
'params' => array(
'host' => 'localhost',
'port' => '3306',
'user' => 'username',
'password' => 'password',
'dbname' => 'database',
)
)
)
),
);
You can add more connections by adding additional keys to the connection
and specifying your parameters.
An exhaustive list of configuration options can be found directly in the Options classes of each module.
- doctrine.connection.orm_default
- doctrine.configuration.orm_default
- doctrine.driver.orm_default
- doctrine.entitymanager.orm_default
- doctrine.eventmanager.orm_default
Access the Doctrine command line as following
./vendor/bin/doctrine-module
Access the entity manager using the following alias:
<?php
$em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
You can also inject the EntityManager
directly in your controllers/services by using a controller factory. Please
refer to the official ServiceManager documentation for more information.
Example implementation using Boostrap
event that you can add into you module class:
<?php
namespace MyModule;
class Module
{
//..
public function onBootstrap(\Zend\EventManager\EventInterface $e)
{
$application = $e->getApplication();
$serviceManager = $application->getServiceManager();
$controllerLoader = $serviceManager->get('ControllerLoader');
// Add initializer to Controller Service Manager that check if controllers needs entity manager injection
$controllerLoader->addInitializer(function ($instance) use ($serviceManager) {
if (method_exists($instance, 'setEntityManager')) {
$instance->setEntityManager($serviceManager->get('doctrine.entitymanager.orm_default'));
}
});
}
}
Coming soon!