/usr/share/doc/php-doctrine-orm/html/reference/advanced-configuration.html is in doctrine-orm-doc 2.4.6-1+deb8u1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>32. Advanced Configuration — Doctrine 2 ORM 2 documentation</title>
<link rel="stylesheet" href="../_static/bootstrap/css/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/layout.css" type="text/css" />
<link rel="stylesheet" href="../_static/configurationblock.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '2',
COLLAPSE_MODINDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/configurationblock.js"></script>
<script src="../_static/bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript">
<!--
$(document).ready(function() {
$("#versions").change(function() {
var docsUrl = $(this).val();
window.location.href = docsUrl;
});
});
-->
</script>
<link rel="shortcut icon" href="../_static/doctrine.ico"/>
<link rel="top" title="Doctrine 2 ORM 2 documentation" href="../index.html" />
<link rel="next" title="Aggregate Fields" href="../cookbook/aggregate-fields.html" />
<link rel="prev" title="31. Implementing a NamingStrategy" href="namingstrategy.html" />
</head>
<body>
<div id="wrapper">
<div id="header">
<h1 id="h1title"></h1>
<div id="logo">
<a href="http://www.doctrine-project.org/">Doctrine - PHP Database Libraries</a>
</div>
</div>
<div id="nav" class="cls">
<div class="tl cls">
<ul>
<li><a target="_top" href="/">home</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/about">about</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/projects.html">projects</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/projects/orm">orm</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/projects/dbal">dbal</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/blog">blog</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/jira">development</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/contribute">contribute</a></li>
<li><a class="" target="_top" href="http://www.doctrine-project.org/community">community</a></li>
</ul>
</div>
</div>
<div id="content" class="cls">
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="../cookbook/aggregate-fields.html" title="Aggregate Fields"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="namingstrategy.html" title="31. Implementing a NamingStrategy"
accesskey="P">previous</a> |</li>
<li><a href="/">Doctrine Homepage</a> »</li>
<li><a href="../index.html">Doctrine 2 ORM 2 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" >
<div class="section" id="advanced-configuration">
<h1>32. Advanced Configuration<a class="headerlink" href="#advanced-configuration" title="Permalink to this headline">¶</a></h1>
<p>The configuration of the EntityManager requires a
<tt class="docutils literal"><span class="pre">Doctrine\ORM\Configuration</span></tt> instance as well as some database
connection parameters. This example shows all the potential
steps of configuration.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">use</span> <span class="nx">Doctrine\ORM\EntityManager</span><span class="p">,</span>
<span class="nx">Doctrine\ORM\Configuration</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$applicationMode</span> <span class="o">==</span> <span class="s2">"development"</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$cache</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Doctrine\Common\Cache\ArrayCache</span><span class="p">;</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nv">$cache</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Doctrine\Common\Cache\ApcCache</span><span class="p">;</span>
<span class="p">}</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Configuration</span><span class="p">;</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setMetadataCacheImpl</span><span class="p">(</span><span class="nv">$cache</span><span class="p">);</span>
<span class="nv">$driverImpl</span> <span class="o">=</span> <span class="nv">$config</span><span class="o">-></span><span class="na">newDefaultAnnotationDriver</span><span class="p">(</span><span class="s1">'/path/to/lib/MyProject/Entities'</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setMetadataDriverImpl</span><span class="p">(</span><span class="nv">$driverImpl</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setQueryCacheImpl</span><span class="p">(</span><span class="nv">$cache</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setProxyDir</span><span class="p">(</span><span class="s1">'/path/to/myproject/lib/MyProject/Proxies'</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setProxyNamespace</span><span class="p">(</span><span class="s1">'MyProject\Proxies'</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$applicationMode</span> <span class="o">==</span> <span class="s2">"development"</span><span class="p">)</span> <span class="p">{</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setAutoGenerateProxyClasses</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setAutoGenerateProxyClasses</span><span class="p">(</span><span class="k">false</span><span class="p">);</span>
<span class="p">}</span>
<span class="nv">$connectionOptions</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
<span class="s1">'driver'</span> <span class="o">=></span> <span class="s1">'pdo_sqlite'</span><span class="p">,</span>
<span class="s1">'path'</span> <span class="o">=></span> <span class="s1">'database.sqlite'</span>
<span class="p">);</span>
<span class="nv">$em</span> <span class="o">=</span> <span class="nx">EntityManager</span><span class="o">::</span><span class="na">create</span><span class="p">(</span><span class="nv">$connectionOptions</span><span class="p">,</span> <span class="nv">$config</span><span class="p">);</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Do not use Doctrine without a metadata and query cache!
Doctrine is optimized for working with caches. The main
parts in Doctrine that are optimized for caching are the metadata
mapping information with the metadata cache and the DQL to SQL
conversions with the query cache. These 2 caches require only an
absolute minimum of memory yet they heavily improve the runtime
performance of Doctrine. The recommended cache driver to use with
Doctrine is <a class="reference external" href="http://www.php.net/apc">APC</a>. APC provides you with
an opcode-cache (which is highly recommended anyway) and a very
fast in-memory cache storage that you can use for the metadata and
query caches as seen in the previous code snippet.</p>
</div>
<div class="section" id="configuration-options">
<h2>32.1. Configuration Options<a class="headerlink" href="#configuration-options" title="Permalink to this headline">¶</a></h2>
<p>The following sections describe all the configuration options
available on a <tt class="docutils literal"><span class="pre">Doctrine\ORM\Configuration</span></tt> instance.</p>
<div class="section" id="proxy-directory-required">
<h3>32.1.1. Proxy Directory (<strong>*REQUIRED*</strong>)<a class="headerlink" href="#proxy-directory-required" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setProxyDir</span><span class="p">(</span><span class="nv">$dir</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">getProxyDir</span><span class="p">();</span>
</pre></div>
</div>
<p>Gets or sets the directory where Doctrine generates any proxy
classes. For a detailed explanation on proxy classes and how they
are used in Doctrine, refer to the “Proxy Objects” section further
down.</p>
</div>
<div class="section" id="proxy-namespace-required">
<h3>32.1.2. Proxy Namespace (<strong>*REQUIRED*</strong>)<a class="headerlink" href="#proxy-namespace-required" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setProxyNamespace</span><span class="p">(</span><span class="nv">$namespace</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">getProxyNamespace</span><span class="p">();</span>
</pre></div>
</div>
<p>Gets or sets the namespace to use for generated proxy classes. For
a detailed explanation on proxy classes and how they are used in
Doctrine, refer to the “Proxy Objects” section further down.</p>
</div>
<div class="section" id="metadata-driver-required">
<h3>32.1.3. Metadata Driver (<strong>*REQUIRED*</strong>)<a class="headerlink" href="#metadata-driver-required" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setMetadataDriverImpl</span><span class="p">(</span><span class="nv">$driver</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">getMetadataDriverImpl</span><span class="p">();</span>
</pre></div>
</div>
<p>Gets or sets the metadata driver implementation that is used by
Doctrine to acquire the object-relational metadata for your
classes.</p>
<p>There are currently 4 available implementations:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">Doctrine\ORM\Mapping\Driver\AnnotationDriver</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Doctrine\ORM\Mapping\Driver\XmlDriver</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Doctrine\ORM\Mapping\Driver\YamlDriver</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Doctrine\ORM\Mapping\Driver\DriverChain</span></tt></li>
</ul>
<p>Throughout the most part of this manual the AnnotationDriver is
used in the examples. For information on the usage of the XmlDriver
or YamlDriver please refer to the dedicated chapters
<tt class="docutils literal"><span class="pre">XML</span> <span class="pre">Mapping</span></tt> and <tt class="docutils literal"><span class="pre">YAML</span> <span class="pre">Mapping</span></tt>.</p>
<p>The annotation driver can be configured with a factory method on
the <tt class="docutils literal"><span class="pre">Doctrine\ORM\Configuration</span></tt>:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$driverImpl</span> <span class="o">=</span> <span class="nv">$config</span><span class="o">-></span><span class="na">newDefaultAnnotationDriver</span><span class="p">(</span><span class="s1">'/path/to/lib/MyProject/Entities'</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setMetadataDriverImpl</span><span class="p">(</span><span class="nv">$driverImpl</span><span class="p">);</span>
</pre></div>
</div>
<p>The path information to the entities is required for the annotation
driver, because otherwise mass-operations on all entities through
the console could not work correctly. All of metadata drivers
accept either a single directory as a string or an array of
directories. With this feature a single driver can support multiple
directories of Entities.</p>
</div>
<div class="section" id="metadata-cache-recommended">
<h3>32.1.4. Metadata Cache (<strong>*RECOMMENDED*</strong>)<a class="headerlink" href="#metadata-cache-recommended" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setMetadataCacheImpl</span><span class="p">(</span><span class="nv">$cache</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">getMetadataCacheImpl</span><span class="p">();</span>
</pre></div>
</div>
<p>Gets or sets the cache implementation to use for caching metadata
information, that is, all the information you supply via
annotations, xml or yaml, so that they do not need to be parsed and
loaded from scratch on every single request which is a waste of
resources. The cache implementation must implement the
<tt class="docutils literal"><span class="pre">Doctrine\Common\Cache\Cache</span></tt> interface.</p>
<p>Usage of a metadata cache is highly recommended.</p>
<p>The recommended implementations for production are:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">Doctrine\Common\Cache\ApcCache</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Doctrine\Common\Cache\MemcacheCache</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Doctrine\Common\Cache\XcacheCache</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Doctrine\Common\Cache\RedisCache</span></tt></li>
</ul>
<p>For development you should use the
<tt class="docutils literal"><span class="pre">Doctrine\Common\Cache\ArrayCache</span></tt> which only caches data on a
per-request basis.</p>
</div>
<div class="section" id="query-cache-recommended">
<h3>32.1.5. Query Cache (<strong>*RECOMMENDED*</strong>)<a class="headerlink" href="#query-cache-recommended" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setQueryCacheImpl</span><span class="p">(</span><span class="nv">$cache</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">getQueryCacheImpl</span><span class="p">();</span>
</pre></div>
</div>
<p>Gets or sets the cache implementation to use for caching DQL
queries, that is, the result of a DQL parsing process that includes
the final SQL as well as meta information about how to process the
SQL result set of a query. Note that the query cache does not
affect query results. You do not get stale data. This is a pure
optimization cache without any negative side-effects (except some
minimal memory usage in your cache).</p>
<p>Usage of a query cache is highly recommended.</p>
<p>The recommended implementations for production are:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">Doctrine\Common\Cache\ApcCache</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Doctrine\Common\Cache\MemcacheCache</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Doctrine\Common\Cache\XcacheCache</span></tt></li>
<li><tt class="docutils literal"><span class="pre">Doctrine\Common\Cache\RedisCache</span></tt></li>
</ul>
<p>For development you should use the
<tt class="docutils literal"><span class="pre">Doctrine\Common\Cache\ArrayCache</span></tt> which only caches data on a
per-request basis.</p>
</div>
<div class="section" id="sql-logger-optional">
<h3>32.1.6. SQL Logger (<strong>*Optional*</strong>)<a class="headerlink" href="#sql-logger-optional" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setSQLLogger</span><span class="p">(</span><span class="nv">$logger</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">getSQLLogger</span><span class="p">();</span>
</pre></div>
</div>
<p>Gets or sets the logger to use for logging all SQL statements
executed by Doctrine. The logger class must implement the
<tt class="docutils literal"><span class="pre">Doctrine\DBAL\Logging\SQLLogger</span></tt> interface. A simple default
implementation that logs to the standard output using <tt class="docutils literal"><span class="pre">echo</span></tt> and
<tt class="docutils literal"><span class="pre">var_dump</span></tt> can be found at
<tt class="docutils literal"><span class="pre">Doctrine\DBAL\Logging\EchoSQLLogger</span></tt>.</p>
</div>
<div class="section" id="auto-generating-proxy-classes-optional">
<h3>32.1.7. Auto-generating Proxy Classes (<strong>*OPTIONAL*</strong>)<a class="headerlink" href="#auto-generating-proxy-classes-optional" title="Permalink to this headline">¶</a></h3>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setAutoGenerateProxyClasses</span><span class="p">(</span><span class="nv">$bool</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">getAutoGenerateProxyClasses</span><span class="p">();</span>
</pre></div>
</div>
<p>Gets or sets whether proxy classes should be generated
automatically at runtime by Doctrine. If set to <tt class="docutils literal"><span class="pre">FALSE</span></tt>, proxy
classes must be generated manually through the doctrine command
line task <tt class="docutils literal"><span class="pre">generate-proxies</span></tt>. The strongly recommended value for
a production environment is <tt class="docutils literal"><span class="pre">FALSE</span></tt>.</p>
</div>
</div>
<div class="section" id="development-vs-production-configuration">
<h2>32.2. Development vs Production Configuration<a class="headerlink" href="#development-vs-production-configuration" title="Permalink to this headline">¶</a></h2>
<p>You should code your Doctrine2 bootstrapping with two different
runtime models in mind. There are some serious benefits of using
APC or Memcache in production. In development however this will
frequently give you fatal errors, when you change your entities and
the cache still keeps the outdated metadata. That is why we
recommend the <tt class="docutils literal"><span class="pre">ArrayCache</span></tt> for development.</p>
<p>Furthermore you should have the Auto-generating Proxy Classes
option to true in development and to false in production. If this
option is set to <tt class="docutils literal"><span class="pre">TRUE</span></tt> it can seriously hurt your script
performance if several proxy classes are re-generated during script
execution. Filesystem calls of that magnitude can even slower than
all the database queries Doctrine issues. Additionally writing a
proxy sets an exclusive file lock which can cause serious
performance bottlenecks in systems with regular concurrent
requests.</p>
</div>
<div class="section" id="connection-options">
<h2>32.3. Connection Options<a class="headerlink" href="#connection-options" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">$connectionOptions</span></tt> passed as the first argument to
<tt class="docutils literal"><span class="pre">EntityManager::create()</span></tt> has to be either an array or an
instance of <tt class="docutils literal"><span class="pre">Doctrine\DBAL\Connection</span></tt>. If an array is passed it
is directly passed along to the DBAL Factory
<tt class="docutils literal"><span class="pre">Doctrine\DBAL\DriverManager::getConnection()</span></tt>. The DBAL
configuration is explained in the
<a class="reference external" href="./../../../../../dbal/2.0/docs/reference/configuration/en">DBAL section</a>.</p>
</div>
<div class="section" id="proxy-objects">
<h2>32.4. Proxy Objects<a class="headerlink" href="#proxy-objects" title="Permalink to this headline">¶</a></h2>
<p>A proxy object is an object that is put in place or used instead of
the “real” object. A proxy object can add behavior to the object
being proxied without that object being aware of it. In Doctrine 2,
proxy objects are used to realize several features but mainly for
transparent lazy-loading.</p>
<p>Proxy objects with their lazy-loading facilities help to keep the
subset of objects that are already in memory connected to the rest
of the objects. This is an essential property as without it there
would always be fragile partial objects at the outer edges of your
object graph.</p>
<p>Doctrine 2 implements a variant of the proxy pattern where it
generates classes that extend your entity classes and adds
lazy-loading capabilities to them. Doctrine can then give you an
instance of such a proxy class whenever you request an object of
the class being proxied. This happens in two situations:</p>
<div class="section" id="reference-proxies">
<h3>32.4.1. Reference Proxies<a class="headerlink" href="#reference-proxies" title="Permalink to this headline">¶</a></h3>
<p>The method <tt class="docutils literal"><span class="pre">EntityManager#getReference($entityName,</span> <span class="pre">$identifier)</span></tt>
lets you obtain a reference to an entity for which the identifier
is known, without loading that entity from the database. This is
useful, for example, as a performance enhancement, when you want to
establish an association to an entity for which you have the
identifier. You could simply do this:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="c1">// $em instanceof EntityManager, $cart instanceof MyProject\Model\Cart</span>
<span class="c1">// $itemId comes from somewhere, probably a request parameter</span>
<span class="nv">$item</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">getReference</span><span class="p">(</span><span class="s1">'MyProject\Model\Item'</span><span class="p">,</span> <span class="nv">$itemId</span><span class="p">);</span>
<span class="nv">$cart</span><span class="o">-></span><span class="na">addItem</span><span class="p">(</span><span class="nv">$item</span><span class="p">);</span>
</pre></div>
</div>
<p>Here, we added an Item to a Cart without loading the Item from the
database. If you invoke any method on the Item instance, it would
fully initialize its state transparently from the database. Here
$item is actually an instance of the proxy class that was generated
for the Item class but your code does not need to care. In fact it
<strong>should not care</strong>. Proxy objects should be transparent to your
code.</p>
</div>
<div class="section" id="association-proxies">
<h3>32.4.2. Association proxies<a class="headerlink" href="#association-proxies" title="Permalink to this headline">¶</a></h3>
<p>The second most important situation where Doctrine uses proxy
objects is when querying for objects. Whenever you query for an
object that has a single-valued association to another object that
is configured LAZY, without joining that association in the same
query, Doctrine puts proxy objects in place where normally the
associated object would be. Just like other proxies it will
transparently initialize itself on first access.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Joining an association in a DQL or native query
essentially means eager loading of that association in that query.
This will override the ‘fetch’ option specified in the mapping for
that association, but only for that query.</p>
</div>
</div>
<div class="section" id="generating-proxy-classes">
<h3>32.4.3. Generating Proxy classes<a class="headerlink" href="#generating-proxy-classes" title="Permalink to this headline">¶</a></h3>
<p>Proxy classes can either be generated manually through the Doctrine
Console or automatically by Doctrine. The configuration option that
controls this behavior is:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setAutoGenerateProxyClasses</span><span class="p">(</span><span class="nv">$bool</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">getAutoGenerateProxyClasses</span><span class="p">();</span>
</pre></div>
</div>
<p>The default value is <tt class="docutils literal"><span class="pre">TRUE</span></tt> for convenient development. However,
this setting is not optimal for performance and therefore not
recommended for a production environment. To eliminate the overhead
of proxy class generation during runtime, set this configuration
option to <tt class="docutils literal"><span class="pre">FALSE</span></tt>. When you do this in a development environment,
note that you may get class/file not found errors if certain proxy
classes are not available or failing lazy-loads if new methods were
added to the entity class that are not yet in the proxy class. In
such a case, simply use the Doctrine Console to (re)generate the
proxy classes like so:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="x">$ ./doctrine orm:generate-proxies</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="autoloading-proxies">
<h2>32.5. Autoloading Proxies<a class="headerlink" href="#autoloading-proxies" title="Permalink to this headline">¶</a></h2>
<p>When you deserialize proxy objects from the session or any other storage
it is necessary to have an autoloading mechanism in place for these classes.
For implementation reasons Proxy class names are not PSR-0 compliant. This
means that you have to register a special autoloader for these classes:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">use</span> <span class="nx">Doctrine\ORM\Proxy\Autoloader</span><span class="p">;</span>
<span class="nv">$proxyDir</span> <span class="o">=</span> <span class="s2">"/path/to/proxies"</span><span class="p">;</span>
<span class="nv">$proxyNamespace</span> <span class="o">=</span> <span class="s2">"MyProxies"</span><span class="p">;</span>
<span class="nx">Autoloader</span><span class="o">::</span><span class="na">register</span><span class="p">(</span><span class="nv">$proxyDir</span><span class="p">,</span> <span class="nv">$proxyNamespace</span><span class="p">);</span>
</pre></div>
</div>
<p>If you want to execute additional logic to intercept the proxy file not found
state you can pass a closure as the third argument. It will be called with
the arguments proxydir, namespace and className when the proxy file could not
be found.</p>
</div>
<div class="section" id="multiple-metadata-sources">
<h2>32.6. Multiple Metadata Sources<a class="headerlink" href="#multiple-metadata-sources" title="Permalink to this headline">¶</a></h2>
<p>When using different components using Doctrine 2 you may end up
with them using two different metadata drivers, for example XML and
YAML. You can use the DriverChain Metadata implementations to
aggregate these drivers based on namespaces:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">use</span> <span class="nx">Doctrine\ORM\Mapping\Driver\DriverChain</span><span class="p">;</span>
<span class="nv">$chain</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">DriverChain</span><span class="p">();</span>
<span class="nv">$chain</span><span class="o">-></span><span class="na">addDriver</span><span class="p">(</span><span class="nv">$xmlDriver</span><span class="p">,</span> <span class="s1">'Doctrine\Tests\Models\Company'</span><span class="p">);</span>
<span class="nv">$chain</span><span class="o">-></span><span class="na">addDriver</span><span class="p">(</span><span class="nv">$yamlDriver</span><span class="p">,</span> <span class="s1">'Doctrine\Tests\ORM\Mapping'</span><span class="p">);</span>
</pre></div>
</div>
<p>Based on the namespace of the entity the loading of entities is
delegated to the appropriate driver. The chain semantics come from
the fact that the driver loops through all namespaces and matches
the entity class name against the namespace using a
<tt class="docutils literal"><span class="pre">strpos()</span> <span class="pre">===</span> <span class="pre">0</span></tt> call. This means you need to order the drivers
correctly if sub-namespaces use different metadata driver
implementations.</p>
</div>
<div class="section" id="default-repository-optional">
<h2>32.7. Default Repository (<strong>*OPTIONAL*</strong>)<a class="headerlink" href="#default-repository-optional" title="Permalink to this headline">¶</a></h2>
<p>Specifies the FQCN of a subclass of the EntityRepository.
That will be available for all entities without a custom repository class.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">setDefaultRepositoryClassName</span><span class="p">(</span><span class="nv">$fqcn</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-></span><span class="na">getDefaultRepositoryClassName</span><span class="p">();</span>
</pre></div>
</div>
<p>The default value is <tt class="docutils literal"><span class="pre">Doctrine\ORM\EntityRepository</span></tt>.
Any repository class must be a subclass of EntityRepository otherwise you got an ORMException</p>
</div>
<div class="section" id="setting-up-the-console">
<h2>32.8. Setting up the Console<a class="headerlink" href="#setting-up-the-console" title="Permalink to this headline">¶</a></h2>
<p>Doctrine uses the Symfony Console component for generating the command
line interface. You can take a look at the <tt class="docutils literal"><span class="pre">vendor/bin/doctrine.php</span></tt>
script and the <tt class="docutils literal"><span class="pre">Doctrine\ORM\Tools\Console\ConsoleRunner</span></tt> command
for inspiration how to setup the cli.</p>
<p>In general the required code looks like this:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="nv">$cli</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Application</span><span class="p">(</span><span class="s1">'Doctrine Command Line Interface'</span><span class="p">,</span> <span class="nx">\Doctrine\ORM\Version</span><span class="o">::</span><span class="na">VERSION</span><span class="p">);</span>
<span class="nv">$cli</span><span class="o">-></span><span class="na">setCatchExceptions</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
<span class="nv">$cli</span><span class="o">-></span><span class="na">setHelperSet</span><span class="p">(</span><span class="nv">$helperSet</span><span class="p">);</span>
<span class="nx">Doctrine\ORM\Tools\Console\ConsoleRunner</span><span class="o">::</span><span class="na">addCommands</span><span class="p">(</span><span class="nv">$cli</span><span class="p">);</span>
<span class="nv">$cli</span><span class="o">-></span><span class="na">run</span><span class="p">();</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<div id="searchbox" style="">
<h3>Search</h3>
<form class="search" action="http://readthedocs.org/search/project/" method="get">
<input type="text" name="q" size="18">
<input type="submit" value="Go">
<input type="hidden" name="selected_facets" value="project:">
</form>
</div>
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">32. Advanced Configuration</a><ul>
<li><a class="reference internal" href="#configuration-options">32.1. Configuration Options</a><ul>
<li><a class="reference internal" href="#proxy-directory-required">32.1.1. Proxy Directory (<strong>*REQUIRED*</strong>)</a></li>
<li><a class="reference internal" href="#proxy-namespace-required">32.1.2. Proxy Namespace (<strong>*REQUIRED*</strong>)</a></li>
<li><a class="reference internal" href="#metadata-driver-required">32.1.3. Metadata Driver (<strong>*REQUIRED*</strong>)</a></li>
<li><a class="reference internal" href="#metadata-cache-recommended">32.1.4. Metadata Cache (<strong>*RECOMMENDED*</strong>)</a></li>
<li><a class="reference internal" href="#query-cache-recommended">32.1.5. Query Cache (<strong>*RECOMMENDED*</strong>)</a></li>
<li><a class="reference internal" href="#sql-logger-optional">32.1.6. SQL Logger (<strong>*Optional*</strong>)</a></li>
<li><a class="reference internal" href="#auto-generating-proxy-classes-optional">32.1.7. Auto-generating Proxy Classes (<strong>*OPTIONAL*</strong>)</a></li>
</ul>
</li>
<li><a class="reference internal" href="#development-vs-production-configuration">32.2. Development vs Production Configuration</a></li>
<li><a class="reference internal" href="#connection-options">32.3. Connection Options</a></li>
<li><a class="reference internal" href="#proxy-objects">32.4. Proxy Objects</a><ul>
<li><a class="reference internal" href="#reference-proxies">32.4.1. Reference Proxies</a></li>
<li><a class="reference internal" href="#association-proxies">32.4.2. Association proxies</a></li>
<li><a class="reference internal" href="#generating-proxy-classes">32.4.3. Generating Proxy classes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#autoloading-proxies">32.5. Autoloading Proxies</a></li>
<li><a class="reference internal" href="#multiple-metadata-sources">32.6. Multiple Metadata Sources</a></li>
<li><a class="reference internal" href="#default-repository-optional">32.7. Default Repository (<strong>*OPTIONAL*</strong>)</a></li>
<li><a class="reference internal" href="#setting-up-the-console">32.8. Setting up the Console</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="namingstrategy.html"
title="previous chapter">31. Implementing a NamingStrategy</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="../cookbook/aggregate-fields.html"
title="next chapter">Aggregate Fields</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/reference/advanced-configuration.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2010-12, Doctrine Project Team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
<br/>
<a target="_BLANK" href="http://www.servergrove.com"><img src="../_static/servergrove.jpg" /></a> <br/><br/>
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick" />
<input type="hidden" name="hosted_button_id" value="BAE2E3XANQ77Y" />
<input type="Submit" value="Donate via PayPal" />
</form>
</div>
</div>
<div id="bot-rcnr">
<div class="tl"><!-- corner --></div>
</div>
</div>
<a class="githublink" href="http://github.com/doctrine">Fork me on GitHub</a>
</body>
</html>
|