Provides Doctrine ORM Entity Managers as services to Pimple applications.
Currently requires both dbs and dbs.event_manager services in order to work. These can be provided by a Doctrine Service Provider like the Silex one. If you can or want to fake it, go for it. :)
Through Composer as dflydev/doctrine-orm-service-provider.
To get up and running, register DoctrineOrmServiceProvider
and
manually specify the directory that will contain the proxies along
with at least one mapping.
In each of these examples an Entity Manager that is bound to the default database connection will be provided. It will be accessible via orm.em.
<?php
// Default entity manager.
$em = $app['orm.em'];
<?php
use Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProvider;
use Pimple\Container;
$container = new Container;
$container['db.options'] = array(
'driver' => 'pdo_sqlite',
'path' => '/path/to/sqlite.db',
);
// ensure that $container['dbs'] and $container['dbs.event_manager']
// are available, most likely by way of a core service provider.
$container->register(new DoctrineOrmServiceProvider, array(
'orm.proxies_dir' => '/path/to/proxies',
'orm.em.options' => array(
'mappings' => array(
// Using actual filesystem paths
array(
'type' => 'annotation',
'namespace' => 'Foo\Entities',
'path' => __DIR__.'/src/Foo/Entities',
),
array(
'type' => 'xml',
'namespace' => 'Bat\Entities',
'path' => __DIR__.'/src/Bat/Resources/mappings',
),
// XML/YAML driver (Symfony2 style)
// Mapping files can be named like Foo.orm.yml
// instead of Baz.Entities.Foo.dcm.yml
array(
'type' => 'simple_yml',
'namespace' => 'Baz\Entities',
'path' => __DIR__.'/src/Bat/Resources/config/doctrine',
),
),
),
));
Version 2.x of this service provider is compatible with version 2.x of Silex and version 1.x of this service provider is compatible with version 1.x of Silex. The following is an example of version 2.x of this service provider and version 2.x of Silex.
<?php
use Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProvider;
use Silex\Application;
use Silex\Provider\DoctrineServiceProvider;
$app = new Application;
$app->register(new DoctrineServiceProvider, array(
'db.options' => array(
'driver' => 'pdo_sqlite',
'path' => '/path/to/sqlite.db',
),
));
$app->register(new DoctrineOrmServiceProvider, array(
'orm.proxies_dir' => '/path/to/proxies',
'orm.em.options' => array(
'mappings' => array(
// Using actual filesystem paths
array(
'type' => 'annotation',
'namespace' => 'Foo\Entities',
'path' => __DIR__.'/src/Foo/Entities',
),
array(
'type' => 'xml',
'namespace' => 'Bat\Entities',
'path' => __DIR__.'/src/Bat/Resources/mappings',
),
),
),
));
orm.em.options: Array of Entity Manager options.
These options are available:
mappings: Array of mapping definitions.
Each mapping definition should be an array with the following options:
annotation
, xml
, yml
, simple_xml
, simple_yml
or php
.New: the simple_xml
and simple_yml
driver types were added in v1.1 and provide support for the simplified XML driver and simplified YAML driver of Doctrine.
Additionally, each mapping definition should contain one of the following options:
Path\To\Foo\Resources\mappings
Each mapping definition can have the following optional options:
Each annotation mapping may also specify the following options:
true
, only simple notations like @Entity
will work.
If false
, more advanced notations and aliasing via use
will
work. (Example: use Doctrine\ORM\Mapping AS ORM
, @ORM\Entity
)
Note that if set to false
, the AnnotationRegistry
will probably
need to be configured correctly so that it can load your Annotations
classes. See this FAQ:
Why aren't my Annotations classes being found?orm.ems.options: Array of Entity Manager configuration sets indexed by each Entity Manager's name. Each value should look like orm.em.options.
Example configuration:
<?php
$app['orm.ems.default'] = 'sqlite';
$app['orm.ems.options'] = array(
'mysql' => array(
'connection' => 'mysql',
'mappings' => array(),
),
'sqlite' => array(
'connection' => 'sqlite',
'mappings' => array(),
),
);
Example usage:
<?php
$emMysql = $app['orm.ems']['mysql'];
$emSqlite = $app['orm.ems']['sqlite'];
Doctrine\Common\Persistence\Mapping\ClassMetadataFactory
.Doctrine\Common\Persistence\ObjectRepository
.Doctrine\ORM\Repository\RepositoryFactory
.Doctrine\ORM\Mapping\EntityListenerResolver
.orm.add_mapping_driver: Function providing the ability to add a mapping driver to an Entity Manager.
These params are available:
Doctrine\Common\Persistence\Mapping\Driver\MappingDriver
.$mappingDriver
, string.null
.orm.em_name_from_param: Function providing the ability to retrieve an entity manager's name from a param.
This is useful for being able to optionally allow users to specify which entity manager should be configured for a 3rd party service provider but fallback to the default entity manager if not explitely specified.
For example:
<?php
$emName = $app['orm.em_name_from_param']('3rdparty.provider.em');
$em = $app['orm.ems'][$emName];
This code should be able to be used inside of a 3rd party service provider
safely, whether the user has defined 3rdparty.provider.em
or not.
Doctrine\ORM\Mapping\NamingStrategy
.Doctrine\ORM\Mapping\QuoteStrategy
.Doctrine\ORM\Query\AST\Functions\FunctionNode
.Doctrine\ORM\Internal\Hydration\AbstractHydrator
.Doctrine\ORM\EntityManager
.Doctrine\ORM\EntityManager
indexed by name.When use_simple_annotation_reader is set to False
for an entity,
the AnnotationRegistry
needs to have the project's autoloader added
to it.
Example:
<?php
$loader = require __DIR__ . '/../vendor/autoload.php';
\Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
There is already a thirdparty ManagerRegistry
implementation at saxulum-doctrine-orm-manager-registry-provider.
It support the entity
type known of the form component, adds the UniqueEntity
validator and a command line integration.
MIT, see LICENSE.
If you have questions or want to help out, join us in the #dflydev or #silex-php channels on irc.freenode.net.
This project is based heavily on both the core Doctrine Service Provider and the work done by @docteurklein on the docteurklein/silex-doctrine-service-providers project. Some inspiration was also taken from Doctrine Bundle and Doctrine Bridge.