配置参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
doctrine:
dbal:
default_connection: default
# A collection of custom types
types:
# example
some_custom_type:
class: Acme\HelloBundle\MyCustomType
connections:
# A collection of different named connections (e.g. default, conn2, etc)
default:
dbname: ~
host: localhost
port: ~
user: root
password: ~
# RDBMS specific; Refer to the manual of your RDBMS for more information
charset: ~
dbname_suffix: ~
# SQLite specific
path: ~
# SQLite specific
memory: ~
# MySQL specific. The unix socket to use for MySQL
unix_socket: ~
# IBM DB2 specific. True to use as persistent connection for the ibm_db2 driver
persistent: ~
# IBM DB2 specific. The protocol to use for the ibm_db2 driver (default to TCPIP if omitted)
protocol: ~
# Oracle specific. True to use SERVICE_NAME as connection parameter instead of SID for Oracle
service: ~
# Oracle specific. Overrules dbname parameter if given and used as SERVICE_NAME or SID connection
# parameter for Oracle depending on the service parameter.
servicename: ~
# oci8 driver specific. The session mode to use for the oci8 driver.
sessionMode: ~
# SQL Anywhere specific (ServerName). The name of a running database server to connect to for SQL Anywhere.
server: ~
# PostgreSQL specific (default_dbname).
# Override the default database (postgres) to connect to.
default_dbname: ~
# PostgreSQL specific (LIBPQ-CONNECT-SSLMODE).
# Determines whether or with what priority a SSL TCP/IP connection will be negotiated with the server for PostgreSQL.
sslmode: ~
# PostgreSQL specific (LIBPQ-CONNECT-SSLROOTCERT).
# The name of a file containing SSL certificate authority (CA) certificate(s).
# If the file exists, the server's certificate will be verified to be signed by one of these authorities.
sslrootcert: ~
# PostgreSQL specific (LIBPQ-CONNECT-SSLCERT).
# The name of a file containing the client SSL certificate.
sslcert: ~
# PostgreSQL specific (LIBPQ-CONNECT-SSLKEY).
# The name of a file containing the private key for the client SSL certificate.
sslkey: ~
# PostgreSQL specific (LIBPQ-CONNECT-SSLCRL).
# The name of a file containing the SSL certificate revocation list (CRL).
sslcrl: ~
# Oracle specific (SERVER=POOLED). True to use a pooled server with the oci8/pdo_oracle driver
pooled: ~
# pdo_sqlsrv driver specific. Configuring MultipleActiveResultSets for the pdo_sqlsrv driver
MultipleActiveResultSets: ~
# Enable savepoints for nested transactions
use_savepoints: true
driver: pdo_mysql
platform_service: ~
auto_commit: ~
# If set to "/^sf2_/" all tables, and any named objects such as sequences
# not prefixed with "sf2_" will be ignored by the schema tool.
# This is for custom tables which should not be altered automatically.
schema_filter: ~
# When true, queries are logged to a "doctrine" monolog channel
logging: "%kernel.debug%"
profiling: "%kernel.debug%"
# When true, profiling also collects a backtrace for each query
profiling_collect_backtrace: false
# When true, profiling also collects schema errors for each query
profiling_collect_schema_errors: true
# When true, type comments are skipped in the database schema, matching the behavior of DBAL 4.
# This requires using the non-deprecated schema comparison APIs of DBAL.
disable_type_comments: false
server_version: ~
driver_class: ~
# Allows to specify a custom wrapper implementation to use.
# Must be a subclass of Doctrine\DBAL\Connection
wrapper_class: ~
keep_replica: ~
# An array of options
options:
# example
# key: value
# An array of mapping types
mapping_types:
# example
# enum: string
default_table_options:
# Affects schema-tool. If absent, DBAL chooses defaults
# based on the platform. Examples here are for MySQL.
# charset: utf8mb4
# collate: utf8mb4_unicode_ci # When using doctrine/dbal 2.x
# collation: utf8mb4_unicode_ci # When using doctrine/dbal 3.x
# engine: InnoDB
# Service identifier of a Psr\Cache\CacheItemPoolInterface implementation
# to use as the cache driver for dbal result sets.
result_cache: ~
replicas:
# A collection of named replica connections (e.g. replica1, replica2)
replica1:
dbname: ~
host: localhost
port: ~
user: root
password: ~
charset: ~
dbname_suffix: ~
path: ~
memory: ~
# MySQL specific. The unix socket to use for MySQL
unix_socket: ~
# IBM DB2 specific. True to use as persistent connection for the ibm_db2 driver
persistent: ~
# IBM DB2 specific. The protocol to use for the ibm_db2 driver (default to TCPIP if omitted)
protocol: ~
# Oracle specific. True to use SERVICE_NAME as connection parameter instead of SID for Oracle
service: ~
# Oracle specific. Overrules dbname parameter if given and used as SERVICE_NAME or SID connection
# parameter for Oracle depending on the service parameter.
servicename: ~
# oci8 driver specific. The session mode to use for the oci8 driver.
sessionMode: ~
# SQL Anywhere specific (ServerName). The name of a running database server to connect to for SQL Anywhere.
server: ~
# PostgreSQL specific (default_dbname).
# Override the default database (postgres) to connect to.
default_dbname: ~
# PostgreSQL specific (LIBPQ-CONNECT-SSLMODE).
# Determines whether or with what priority a SSL TCP/IP connection will be negotiated with the server for PostgreSQL.
sslmode: ~
# PostgreSQL specific (LIBPQ-CONNECT-SSLROOTCERT).
# The name of a file containing SSL certificate authority (CA) certificate(s).
# If the file exists, the server's certificate will be verified to be signed by one of these authorities.
sslrootcert: ~
# PostgreSQL specific (LIBPQ-CONNECT-SSLCERT).
# The name of a file containing the client SSL certificate.
sslcert: ~
# PostgreSQL specific (LIBPQ-CONNECT-SSLKEY).
# The name of a file containing the private key for the client SSL certificate.
sslkey: ~
# PostgreSQL specific (LIBPQ-CONNECT-SSLCRL).
# The name of a file containing the SSL certificate revocation list (CRL).
sslcrl: ~
# Oracle specific (SERVER=POOLED). True to use a pooled server with the oci8/pdo_oracle driver
pooled: ~
# pdo_sqlsrv driver specific. Configuring MultipleActiveResultSets for the pdo_sqlsrv driver
MultipleActiveResultSets: ~
orm:
default_entity_manager: ~ # The first defined is used if not set
# Auto generate mode possible values are: "NEVER", "ALWAYS", "FILE_NOT_EXISTS", "EVAL", "FILE_NOT_EXISTS_OR_CHANGED"
auto_generate_proxy_classes: false
proxy_dir: "%kernel.cache_dir%/doctrine/orm/Proxies"
proxy_namespace: Proxies
# Enables the new implementation of proxies based on lazy ghosts instead of using the legacy implementation
enable_lazy_ghost_objects: false
entity_managers:
# A collection of different named entity managers (e.g. some_em, another_em)
some_em:
query_cache_driver:
type: ~
id: ~
pool: ~
metadata_cache_driver:
type: ~
id: ~
pool: ~
result_cache_driver:
type: ~
id: ~
pool: ~
entity_listeners:
entities:
# example
Acme\HelloBundle\Entity\Author:
listeners:
# example
Acme\HelloBundle\EventListener\ExampleListener:
events:
type: preUpdate
method: preUpdate
# The name of a DBAL connection (the one marked as default is used if not set)
connection: ~
class_metadata_factory_name: Doctrine\ORM\Mapping\ClassMetadataFactory
default_repository_class: Doctrine\ORM\EntityRepository
auto_mapping: false
# Opt-in to new mapping driver mode as of Doctrine ORM 2.16, https://github.com/doctrine/orm/pull/10455
report_fields_where_declared: false
# 0pt-in to the new mapping driver mode as of Doctrine ORM 2.14. See https://github.com/doctrine/orm/pull/6728.
validate_xml_mapping: false
naming_strategy: doctrine.orm.naming_strategy.default
quote_strategy: doctrine.orm.quote_strategy.default
entity_listener_resolver: ~
repository_factory: ~
second_level_cache:
region_cache_driver:
type: ~
id: ~
pool: ~
region_lock_lifetime: 60
log_enabled: true
region_lifetime: 0
enabled: true
factory: ~
regions:
# Prototype
name:
cache_driver:
type: ~
id: ~
pool: ~
lock_path: '%kernel.cache_dir%/doctrine/orm/slc/filelock'
lock_lifetime: 60
type: default
lifetime: 0
service: ~
name: ~
loggers:
# Prototype
name:
name: ~
service: ~
# An array of hydrator names
hydrators:
# example
ListHydrator: Acme\HelloBundle\Hydrators\ListHydrator
mappings:
# An array of mappings, which may be a bundle name or something else
mapping_name:
mapping: true
type: ~
dir: ~
alias: ~
prefix: ~
is_bundle: ~
dql:
# A collection of string functions
string_functions:
# example
# test_string: Acme\HelloBundle\DQL\StringFunction
# A collection of numeric functions
numeric_functions:
# example
# test_numeric: Acme\HelloBundle\DQL\NumericFunction
# A collection of datetime functions
datetime_functions:
# example
# test_datetime: Acme\HelloBundle\DQL\DatetimeFunction
# Register SQL Filters in the entity manager
filters:
# An array of filters
some_filter:
class: Acme\HelloBundle\Filter\SomeFilter # Required
enabled: false
# An array of parameters
parameters:
# example
foo_param: bar_value
schema_ignore_classes:
- Acme\AppBundle\Entity\Order
- Acme\AppBundle\Entity\PhoneNumber
# Search for the "ResolveTargetEntityListener" class for a cookbook about this
resolve_target_entities:
# Prototype
Acme\InvoiceBundle\Model\InvoiceSubjectInterface: Acme\AppBundle\Entity\Customer
配置概览
以下配置示例展示了 ORM 解析的所有默认配置
1 2 3 4 5 6 7 8 9 10 11 12
doctrine:
orm:
auto_mapping: true
# the standard distribution overrides this to be true in debug, false otherwise
auto_generate_proxy_classes: false
proxy_namespace: Proxies
proxy_dir: "%kernel.cache_dir%/doctrine/orm/Proxies"
default_entity_manager: default
metadata_cache_driver: ~
query_cache_driver: ~
result_cache_driver: ~
report_fields_where_declared: false
还有许多其他配置选项可用于覆盖某些类,但这些选项仅适用于非常高级的用例。
Oracle 数据库
如果 oracle 中配置的环境格式不符合 doctrine 的要求,你需要使用 OracleSessionInit 监听器,以便 doctrine 能够识别 Oracle 数据库使用的格式。
你可以通过以下方式轻松实现:
1 2 3 4 5
services:
oracle.listener:
class: Doctrine\DBAL\Event\Listeners\OracleSessionInit
tags:
- { name: doctrine.event_listener, event: postConnect }
doctrine 将在 Oracle 数据库会话中更改的环境变量是
1 2 3 4
NLS_TIME_FORMAT="HH24:MI:SS"
NLS_DATE_FORMAT="YYYY-MM-DD HH24:MI:SS"
NLS_TIMESTAMP_FORMAT="YYYY-MM-DD HH24:MI:SS"
NLS_TIMESTAMP_TZ_FORMAT="YYYY-MM-DD HH24:MI:SS TZH:TZM"
缓存驱动
你可以使用 Symfony 缓存池,方法是使用 pool
类型并通过 FrameworkBundle 配置创建一个缓存池。service
类型允许你定义你自己的缓存服务的 ID
。
以下示例展示了缓存配置的概览
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
doctrine:
orm:
auto_mapping: true
# With no cache set, this defaults to a sane 'pool' configuration
metadata_cache_driver: ~
# the 'pool' type requires to define the 'pool' option and configure a cache pool using the FrameworkBundle
result_cache_driver:
type: pool
pool: doctrine.result_cache_pool
# the 'service' type requires to define the 'id' option too
query_cache_driver:
type: service
id: App\ORM\MyCacheService
framework:
cache:
pools:
doctrine.result_cache_pool:
adapter: cache.app
映射配置
显式定义所有映射实体是 ORM 唯一必要的配置,并且有几个你可以控制的配置选项。以下是映射存在的配置选项
类型
- 可以是
attribute
、xml
、yml
、php
或staticphp
之一。这指定了你的映射使用的元数据类型。 目录
- 映射或实体文件的路径(取决于驱动)。如果此路径是相对路径,则假定它是相对于 bundle 根目录的。这仅在你映射的名称是 bundle 名称时才有效。如果你想使用此选项指定绝对路径,你应该在路径前加上 DIC 中存在的内核参数(例如
%kernel.root_dir%
)。 前缀
- 此映射的所有实体共享的公共命名空间前缀。此前缀绝不应与其他已定义映射的前缀冲突,否则 Doctrine 将无法找到你的某些实体。此选项默认为 bundle 命名空间 +
Entity
,例如,对于名为AcmeHelloBundle
的应用程序 bundle,前缀将是Acme\HelloBundle\Entity
。 别名
- Doctrine 提供了一种将实体命名空间别名为更简单、更短的名称的方法,以便在 DQL 查询或 Repository 访问中使用。当使用 bundle 时,别名默认为 bundle 名称。
是否为 bundle
- 此选项是从
dir
派生的值,默认情况下,如果 dir 是相对路径,并且file_exists()
检查返回 false,则设置为 true。如果存在性检查返回 true,则为 false。在这种情况下,指定了绝对路径,并且元数据文件很可能位于 bundle 之外的目录中。 - .. 索引:
- single: Configuration; Doctrine DBAL single: Doctrine; DBAL 配置
过滤器配置
你可以轻松地在你的配置文件中定义 doctrine 过滤器
1 2 3 4 5 6 7 8 9
doctrine:
orm:
filters:
myFilter:
class: MyVendor\MyBundle\Filters\MyFilter
enabled: true
parameters:
myParameter: myValue
mySecondParameter: mySecondValue
myFilter
- 过滤器标识符(必需)
类
- 过滤器目标类(必需)
已启用
- 默认启用/禁用过滤器(可选 - 默认禁用)
参数
- 设置默认参数(可选)
myParameter: myValue
- 将值
myValue
绑定到参数myParameter
(可选)
自动装配多个实体管理器
你可以通过类型提示你的服务参数,使用以下语法自动装配不同的实体管理器:Doctrine\ORM\EntityManagerInterface $<entity manager>EntityManager
。例如,要注入一个 purchase_logs
实体管理器,请使用此方法
1 2 3 4 5
- public function __construct(EntityManagerInterface $entityManager)
+ public function __construct(EntityManagerInterface $purchaseLogsEntityManager)
{
$this->entityManager = $purchaseLogsEntityManager;
}
Doctrine DBAL 配置
注意
DoctrineBundle 支持默认 Doctrine 驱动程序接受的所有参数,并已转换为 Symfony 强制执行的 XML 或 YAML 命名标准。有关更多信息,请参阅 Doctrine DBAL 文档。
注意
当指定 url
参数时,除非 override_url
设置为 true
,否则从该 URL 提取的任何信息都将覆盖显式设置的参数。数据库 URL 的一个示例是 mysql://snoopy:redbaron@localhost/baseball
,任何显式设置的 driver、user、password 和 dbname 参数都将被此 URL 覆盖。有关更多信息,请参阅 Doctrine DBAL 文档。
除了默认的 Doctrine 选项外,还有一些与 Symfony 相关的选项可以配置。以下代码块显示了所有可能的配置键
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
doctrine:
dbal:
url: mysql://user:secret@localhost:1234/otherdatabase # this would override the values below
dbname: database
host: localhost
port: 1234
user: user
password: secret
dbname_suffix: _test
driver: pdo_mysql
driver_class: MyNamespace\MyDriverImpl
options:
foo: bar
path: "%kernel.project_dir%/var/data.db" # SQLite specific
memory: true # SQLite specific
unix_socket: /tmp/mysql.sock
persistent: true
MultipleActiveResultSets: true # pdo_sqlsrv driver specific
pooled: true # Oracle specific (SERVER=POOLED)
protocol: TCPIP # IBM DB2 specific (PROTOCOL)
server: my_database_server # SQL Anywhere specific (ServerName)
service: true # Oracle specific (SERVICE_NAME instead of SID)
servicename: MyOracleServiceName # Oracle specific (SERVICE_NAME)
sessionMode: 2 # oci8 driver specific (session_mode)
default_dbname: database # PostgreSQL specific (default_dbname)
sslmode: require # PostgreSQL specific (LIBPQ-CONNECT-SSLMODE)
sslrootcert: postgresql-ca.pem # PostgreSQL specific (LIBPQ-CONNECT-SSLROOTCERT)
sslcert: postgresql-cert.pem # PostgreSQL specific (LIBPQ-CONNECT-SSLCERT)
sslkey: postgresql-key.pem # PostgreSQL specific (LIBPQ-CONNECT-SSLKEY)
sslcrl: postgresql.crl # PostgreSQL specific (LIBPQ-CONNECT-SSLCRL)
wrapper_class: MyDoctrineDbalConnectionWrapper
charset: ~ # RDBMS-specific. Refer to the manual of your RDBMS for more information.
logging: "%kernel.debug%"
platform_service: MyOwnDatabasePlatformService
auto_commit: false
schema_filter: /^sf2_/
mapping_types:
enum: string
types:
custom: Acme\HelloBundle\MyCustomType
default_table_options:
# Affects schema-tool. If absent, DBAL chooses defaults
# based on the platform. These defaults might be
# sub-optimal for backward compatibility reasons.
charset: utf8mb4
collate: utf8mb4_unicode_ci # when using doctrine/dbal 2.x
collation: utf8mb4_unicode_ci # when using doctrine/dbal 3.x
engine: InnoDB
如果你想在 YAML 中配置多个连接,请将它们放在 connections
键下,并为它们指定一个唯一的名称
1 2 3 4 5 6 7 8 9 10 11 12 13 14
doctrine:
dbal:
default_connection: default
connections:
default:
dbname: Symfony2
user: root
password: null
host: localhost
customer:
dbname: customer
user: root
password: null
host: localhost
database_connection
服务始终指向默认连接,即定义的第一个连接或通过 default_connection
参数配置的连接。
每个连接也可以通过 doctrine.dbal.[name]_connection
服务访问,其中 [name]
是连接的名称。
自动装配多个连接
你可以通过类型提示你的服务参数,使用以下语法自动装配不同的连接:Doctrine\DBAL\Connection $<connection name>Connection
。例如,要注入一个名为 purchase_logs
的连接,请使用此方法
1 2 3 4 5
- public function __construct(Connection $connection)
+ public function __construct(Connection $purchaseLogsConnection)
{
$this->connection = $purchaseLogsConnection;
}