sinsquare/silex-doctrine-orm-provider

Silex 框架的 Doctrine ORM 提供器

v1.0.7 2017-07-31 12:40 UTC

This package is not auto-updated.

Last update: 2024-09-21 08:56:02 UTC


README

安装

使用 composer

{
    "require": {
        "sinsquare/silex-doctrine-orm-provider": "1.*"
    }
}

注册提供器

  • 如果只需要 ORM 而不需要验证和 Web 分析器
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());
  • 如果只需要 ORM 验证 (UniqueEntity)
//Register all the providers for the ORM

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

...

$application->register(new ValidatorServiceProvider());
$application->register(new DoctrineOrmValidatorProvider());
  • 如果只需要 ORM Web 分析器
//Register all the providers for the ORM

use SinSquare\Doctrine\DoctrineOrmWebProfilerProvider;

...

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

配置

ORM 的配置必须在 $application['doctrine.orm.options'] 中设置,并且必须在注册提供器之前存在。

基本配置方案

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

配置方案类似于在 Smyfony 中使用的配置方案 (了解更多信息).

  • default_entity_manager: 默认实体管理器的名称。如果未设置,则第一个将是默认值。

  • connection: 要使用的 DBAL 连接的名称。更多信息请参阅 DoctrineServiceProvider 帮助 这里.

  • mapping: 目前默认只支持注解类型,但您可以扩展其功能。在 DoctrineOrmServiceProvider 中查找 $app['doctrine.orm.mappingdriver.locator']。

  • cache: 该项目使用 SinSquare/silex-doctrine-orm-provider 进行缓存,它是 Doctrine Cache 的包装器。

  • 使用匿名缓存
'query_cache_driver' => array(
    'type' => 'array',
),

您可以根据更多信息检查 Doctrine Cache 组件来更改缓存类型。

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

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

您可以根据需要创建新的缓存类型,请参阅 这里 了解如何操作。

获取 EntityManager

  • 默认实体管理器
$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
    }

向 EntityManager 添加自定义订阅者

如果您需要将订阅者附加到 EntityManager,应使用 $application['doctrine.orm.em_factory.postinit'],因为它仅在第一次调用管理器后运行一次。

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

    return $manager;
});