dflydev / dflydev-doctrine-orm-service-provider

Doctrine ORM Service Provider
MIT License
209 stars 59 forks source link

Doctrine ORM Service Provider

Provides Doctrine ORM Entity Managers as services to Pimple applications.

Features

Requirements

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. :)

Installation

Through Composer as dflydev/doctrine-orm-service-provider.

Usage

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'];

Pimple

<?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',
            ),
        ),
    ),
));

Silex

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',
            ),
        ),
    ),
));

Configuration

Parameters

Services

Frequently Asked Questions

Why aren't my Annotations classes being found?

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'));

Why is there no manager registry implementation?

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.

License

MIT, see LICENSE.

Community

If you have questions or want to help out, join us in the #dflydev or #silex-php channels on irc.freenode.net.

Not Invented Here

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.