PHP code example of sinsquare / silex-doctrine-orm-provider

1. Go to this page and download the library: Download sinsquare/silex-doctrine-orm-provider library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sinsquare / silex-doctrine-orm-provider example snippets


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

//Register all the providers for the ORM

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

...

$application->register(new ValidatorServiceProvider());
$application->register(new DoctrineOrmValidatorProvider());

//Register all the providers for the ORM

use SinSquare\Doctrine\DoctrineOrmWebProfilerProvider;

...

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

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

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

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

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

$em = $application['doctrine.orm.em'];

$em = $application['doctrine.orm.ems']['named_em'];
//OR
$em = $application['doctrine.orm.em.named_em'];

    $entity = new Entity();
    
    //modify the entity

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

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

    return $manager;
});