baohx2000 / doc
Doctrine 提供商 for Silex
Requires
Requires (Dev)
- phpunit/phpunit: 4.6.4
- silex/silex: ~1
This package is auto-updated.
Last update: 2024-09-19 21:09:01 UTC
README
Doc 是多个现有 Doctrine 相关服务提供者的组合,为 Silex 应用程序提供全面功能的 Doctrine 集成,包括
- DBAL
- ORM(包括多个实体管理器)
- 迁移
Doc 安装设置
在您的应用程序服务提供者之一中设置迁移目录:$app['migrations.directory'] = '/some/directory'; $app->register(new DocServiceProvider);
Doctrine ORM 服务提供者
高度基于(并利用)dflydev/doctrine-orm-service-provider,因此只需复制以下说明。
为 Pimple 应用程序提供 Doctrine ORM 实体管理器作为服务。
特性
- 利用核心 Doctrine 服务提供者,适用于 Silex 或 Cilex。
- 默认实体管理器可以绑定到任何数据库连接
- 可以定义多个实体管理器
- 允许服务提供者注册自己的映射的机制
要求
- PHP 5.3+
- Doctrine ~2.3
目前需要 dbs 和 dbs.event_manager 两个服务才能运行。这些可以由像 Silex 或 Cilex 服务提供者一样提供。如果您能够或想要伪造它,那就去做吧。 :)
可选依赖
PSR-0 资源定位服务提供者
如果使用命名空间化资源映射,则需要实现 dflydev/psr0-resource-locator-service-provider。有关 orm.generate_psr0_mapping 的更多信息,请参阅文档。
安装
通过 Composer 作为 dflydev/doctrine-orm-service-provider 安装。
用法
要启动,请注册 DoctrineOrmServiceProvider
并手动指定包含代理和至少一个映射的目录。
在这些示例中,将提供一个绑定到默认数据库连接的实体管理器。它将通过 orm.em 访问。
<?php // Default entity manager. $em = $app['orm.em'];
Pimple
<?php use Dflydev\Pimple\Provider\DoctrineOrm\DoctrineOrmServiceProvider; $container = new \Pimple; $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["orm.proxies_dir"] = "/path/to/proxies"; $container["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", ), // Using PSR-0 namespaceish embedded resources // (requires registering a PSR-0 Resource Locator // Service Provider) array( "type" => "annotation", "namespace" => "Baz\Entities", "resources_namespace" => "Baz\Entities", ), array( "type" => "xml", "namespace" => "Bar\Entities", "resources_namespace" => "Bar\Resources\mappings", ), ), ); $doctrineOrmServiceProvider = new DoctrineOrmServiceProvider; $doctrineormServiceProvider->register($container);
Silex
<?php use Dflydev\Silex\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", ), // As of 1.1, you can also use the simplified // 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", ), // Using PSR-0 namespaceish embedded resources // (requires registering a PSR-0 Resource Locator // Service Provider) array( "type" => "annotation", "namespace" => "Baz\Entities", "resources_namespace" => "Baz\Entities", ), array( "type" => "xml", "namespace" => "Bar\Entities", "resources_namespace" => "Bar\Resources\mappings", ), ), ), ));
Cilex
<?php use Dflydev\Cilex\Provider\DoctrineOrm\DoctrineOrmServiceProvider; use Cilex\Application; use Cilex\Provider\DoctrineServiceProvider; $app = new Application('My Application'); $app->register(new DoctrineServiceProvider, array( /** same as the Silex example **/ )); $app->register(new DoctrineOrmServiceProvider, array( /** same as the Silex example **/ ));
配置
参数
-
orm.em.options:实体管理器选项数组。
这些选项包括
-
connection(默认:default):定义要使用的数据库连接的字符串。当通过 dbs 使用命名数据库时使用。
-
mappings:映射定义数组。
每个映射定义都应该是一个具有以下选项的数组
- type:映射驱动程序类型,可以是
annotation
、xml
、yml
、simple_xml
、simple_yml
或php
。 - namespace:实体所在的命名空间。
新增:在 v1.1 中添加了
simple_xml
和simple_yml
驱动程序类型,以支持 Doctrine 的 简化 XML 驱动程序 和 简化 YAML 驱动程序。此外,每个映射定义还应包含以下选项之一
- 路径:映射文件所在的路径。这应该是一个实际的文件系统路径。对于PHP驱动程序,它可以是路径的数组。
- 资源命名空间:映射文件所在的类似于命名空间的路径。例如:
Path\To\Foo\Resources\mappings
每个映射定义都可以有以下可选选项
- 别名(默认:null):为实体命名空间设置别名。
每个注解映射也可以指定以下选项
- use_simple_annotation_reader(默认:true):如果设置为
true
,则只有简单的注解,如@Entity
将起作用。如果设置为false
,则更高级的注解和通过use
实现的别名将起作用。(示例:use Doctrine\ORM\Mapping AS ORM
,@ORM\Entity
)请注意,如果设置为false
,则可能需要正确配置AnnotationRegistry
,以便它可以加载您的注解类。请参阅此常见问题解答:[为什么我的注解类没有被找到?](#user-content-why-arent-my-annotations-classes-being-found)
- type:映射驱动程序类型,可以是
-
查询缓存(默认:由orm.default_cache指定的设置):描述查询缓存实现的字符串或数组。
-
元数据缓存(默认:由orm.default_cache指定的设置):描述元数据缓存实现的字符串或数组。
-
结果缓存(默认:由orm.default_cache指定的设置):描述结果缓存实现的字符串或数组。
-
加湿缓存(默认:由orm.default_cache指定的设置):描述加湿缓存实现的字符串或数组。
-
类型:格式为'typeName' => 'Namespace\To\Type\Class'的自定义类型数组。
-
-
orm.ems.options:按每个实体管理器的名称索引的实体管理器配置集数组。每个值应类似于orm.em.options。
示例配置
<?php $app['orm.ems.default'] = 'sqlite'; $app['orm.ems.options'] = array( 'mysql' => array( 'connection' => 'mysql', 'mappings' => array(), ), 'sqlite' => array( 'connection' => 'sqlite', 'mappings' => array(), ), );
示例用法
<?php $emMysql = $app['orm.ems']['mysql']; $emSqlite = $app['orm.ems']['sqlite'];
-
orm.ems.default(默认:第一个处理的实体管理器):定义默认实体管理器名称的字符串。
-
orm.proxies_dir:定义Doctrine生成的代理所在的路径的字符串。
-
orm.proxies_namespace(默认:DoctrineProxy):定义Doctrine生成的代理应驻留的命名空间的字符串。
-
orm.auto_generate_proxies:定义是否自动生成代理的布尔值。
-
orm.class_metadata_factory_name:类元数据工厂的类名。类实现
Doctrine\Common\Persistence\Mapping\ClassMetadataFactory
。 -
orm.default_repository_class:默认存储库的类名。类实现
Doctrine\Common\Persistence\ObjectRepository
。 -
orm.repository_factory:存储库工厂,实例
Doctrine\ORM\Repository\RepositoryFactory
。 -
orm.entity_listener_resolver:实体监听器解析器,实例
Doctrine\ORM\Mapping\EntityListenerResolver
。 -
orm.default_cache:描述默认缓存实现的字符串或数组。
-
orm.add_mapping_driver:提供将映射驱动程序添加到实体管理器的功能。
这些参数是可用的
- $mappingDriver:要添加的映射驱动程序,实例
Doctrine\Common\Persistence\Mapping\Driver\MappingDriver
。 - $namespace:由
$mappingDriver
映射的命名空间,字符串。 - $name:要添加映射的实体管理器的名称,字符串,默认
null
。
- $mappingDriver:要添加的映射驱动程序,实例
-
orm.em_name_from_param:提供从参数检索实体管理器名称的功能。
这对于能够可选地允许用户指定为第三方服务提供商配置哪个实体管理器,但在未显式指定的情况下回退到默认实体管理器非常有用。
例如
<?php $emName = $app['orm.em_name_from_param']('3rdparty.provider.em'); $em = $app['orm.ems'][$emName];
此代码应能够在第三方服务提供商中安全使用,无论用户是否已定义
3rdparty.provider.em
。 -
orm.generate_psr0_mapping:利用dflydev/psr0-resource-locator-service-provider处理命名空间资源目录到其映射实体的映射。
示例用法
<?php $app['orm.ems.config'] = $app->share($app->extend('orm.ems.config', function ($config, $app) { $mapping = $app['orm.generate_psr0_mapping'](array( 'Foo\Resources\mappings' => 'Foo\Entities', 'Bar\Resources\mappings' => 'Bar\Entities', )); $chain = $app['orm.mapping_driver_chain.locator'](); foreach ($mapping as $directory => $namespace) { $driver = new XmlDriver($directory); $chain->addDriver($driver, $namespace); } return $config; }));
-
orm.strategy:
- naming:命名策略,实例
Doctrine\ORM\Mapping\NamingStrategy
。 - quote:引用策略,实例
Doctrine\ORM\Mapping\QuoteStrategy
。
- naming:命名策略,实例
-
orm.custom.functions:
- 字符串,数字,日期时间:自定义DQL函数,根据DQL函数名称索引的类名数组。这些类是
Doctrine\ORM\Query\AST\Functions\FunctionNode
的子类。 - hydration_modes:注入器类名,根据注入模式名称索引。这些类是
Doctrine\ORM\Internal\Hydration\AbstractHydrator
的子类。
- 字符串,数字,日期时间:自定义DQL函数,根据DQL函数名称索引的类名数组。这些类是
服务
- orm.em:实体管理器,
Doctrine\ORM\EntityManager
实例。 - orm.ems:实体管理器,
Doctrine\ORM\EntityManager
的数组,按名称索引。
常见问题解答
为什么找不到我的注解类?
当为实体设置use_simple_annotation_reader
为False
时,需要将项目的自动加载器添加到AnnotationRegistry
中。
示例
<?php $loader = require __DIR__ . '/../vendor/autoload.php'; \Doctrine\Common\Annotations\AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
为什么没有管理注册实现?
已经有一个第三方ManagerRegistry
实现位于saxulum-doctrine-orm-manager-registry-provider。它支持表单组件所知的entity
类型,添加了UniqueEntity
验证器以及命令行集成。
许可
MIT,见LICENSE。
社区
如果您有问题或想帮忙,请加入irc.freenode.net上的#dflydev或#silex-php频道。
非原创
该项目在核心Doctrine服务提供者和@docteurklein在docteurklein/silex-doctrine-service-providers项目上的工作中使用了大量。也从Doctrine Bundle和Doctrine Bridge中获得了灵感。