2017 © Pedro Peláez
 

library silex-doctrine-orm-provider

Doctrine ORM provider for Silex framework

image

sinsquare/silex-doctrine-orm-provider

Doctrine ORM provider for Silex framework

  • Thursday, October 5, 2017
  • by SinSquare
  • Repository
  • 2 Watchers
  • 1 Stars
  • 818 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 9 Versions
  • 38 % Grown

The README.md

Doctrine ORM provider for Silex 2.x framework

Code quality Tests Issues
SensioLabsInsight Build Status Issue Count
Codacy Badge Test Coverage
Code Climate

Installation

With composer :, (*1)

``` json { "require": { "sinsquare/silex-doctrine-orm-provider": "1.*" } }, (*2)

Registering the providers
====

- If you only need the ORM without validation and web profiler

```php
use Silex\Provider\DoctrineServiceProvider;
use SinSquare\Cache\DoctrineCacheServiceProvider;
use SinSquare\Doctrine\DoctrineOrmServiceProvider;

...

$application['doctrine.orm.options'] = array(<config>);

$application->register(new DoctrineServiceProvider());
$application->register(new DoctrineCacheServiceProvider());
$application->register(new DoctrineOrmServiceProvider());
  • If you only need the ORM validation (UniqueEntity)
//Register all the providers for the ORM

use Silex\Provider\ValidatorServiceProvider;
use SinSquare\Doctrine\DoctrineOrmValidatorProvider;

...

$application->register(new ValidatorServiceProvider());
$application->register(new DoctrineOrmValidatorProvider());
  • If you only need the the ORM web profiler
//Register all the providers for the ORM

use SinSquare\Doctrine\DoctrineOrmWebProfilerProvider;

...

$application->register(new DoctrineOrmWebProfilerProvider());

Configuration

The configuration of the ORM must be set in $application['doctrine.orm.options'], and it must exist before registering the provider., (*3)

A basic configuration scheme:, (*4)

$application['doctrine.orm.options'] = array(
    'default_entity_manager' => 'default',
    'auto_generate_proxy_classes' => true,
    'proxy_dir' => __DIR__.'/Resources/Proxy',
    'proxy_namespace' => 'Proxies',

    'entity_managers' => array(
        'default' => array(
            'query_cache_driver' => array(
                'type' => 'array',
            ),
            'metadata_cache_driver' => array(
                'type' => 'array',
            ),
            'result_cache_driver' => array(
                'type' => 'array',
            ),
            'connection' => 'db1',
            'mappings' => array(
                array(
                    'type' => 'annotation',
                    'namespace' => 'SinSquare\\Doctrine\\Tests\\Resources\\Entity',
                    'alias' => 'TestBundle',
                    'path' => __DIR__.'/Resources/Entity',
                    'use_simple_annotation_reader' => false,
                ),
            ),
        ),
    ),
);

The configuration scheme is similar to the one used in Smyfony (read more here)., (*5)

  • default_entity_manager: Name of the default entity manager. If not set the first one will be the default., (*6)

  • connection: Name of the DBAL connection to use. Read more at the DoctrineServiceProvider help here., (*7)

  • mapping: Currently only the annotation type is supported by default, but you can extend the functionality. Look for $app['doctrine.orm.mappingdriver.locator'] in the DoctrineOrmServiceProvider., (*8)

  • cache: The project uses SinSquare/silex-doctrine-orm-provider for cacheing, which is a wrapper for Doctrine Cache., (*9)

  • Using anonym cache:, (*10)

'query_cache_driver' => array(
    'type' => 'array',
),

You can change the cache type, for more info check the Doctrine Cache component. *Using named cache:, (*11)

$application['doctrine.cache.options'] = array(
    'providers' => array(
        'cache_1' => array(
            'type' => 'void',
        )
    ),
);

$application['doctrine.orm.options'] = array(
    ...
    'result_cache_driver' => array(
        'name' => 'cache_1',
    ),
    ...
);

You can create new types of caches, please read how to here;, (*12)

Retrieving the EntityManager

  • The default entity manager:
$em = $application['doctrine.orm.em'];
  • A named entity manager:
$em = $application['doctrine.orm.ems']['named_em'];
//OR
$em = $application['doctrine.orm.em.named_em'];

Using the validator

    $entity = new Entity();

    //modify the entity

    $validator = $application['validator'];
    $errors = $validator->validate($entity);
    if(count($errors)) {
        //there was an error
    }

Adding custom subscribers to the EntityManager

If you need to attach subscriber to the EntityManager you should use the $application['doctrine.orm.em_factory.postinit'] as it runs only once after the fist call on the manager., (*13)

$application['doctrine.orm.em_factory.postinit'] = $this->application->protect(function ($name, $options, $manager) use ($application) {

    $eventManager = $manager->getEventManager();
    $eventManager->addEventSubscriber($subscriber);

    return $manager;
});

The Versions