/usr/share/doc/php-doctrine-orm/html/cookbook/decorator-pattern.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 | <!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>Persisting the Decorator Pattern — 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="Extending DQL in Doctrine 2: Custom AST Walkers" href="dql-custom-walkers.html" />
<link rel="prev" title="Aggregate Fields" href="aggregate-fields.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="dql-custom-walkers.html" title="Extending DQL in Doctrine 2: Custom AST Walkers"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="aggregate-fields.html" title="Aggregate Fields"
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="persisting-the-decorator-pattern">
<h1>Persisting the Decorator Pattern<a class="headerlink" href="#persisting-the-decorator-pattern" title="Permalink to this headline">¶</a></h1>
<p><em>Section author: Chris Woodford <<a class="reference external" href="mailto:chris.woodford%40gmail.com">chris<span>.</span>woodford<span>@</span>gmail<span>.</span>com</a>></em></p>
<p>This recipe will show you a simple example of how you can use
Doctrine 2 to persist an implementation of the
<a class="reference external" href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator Pattern</a></p>
<div class="section" id="component">
<h2>Component<a class="headerlink" href="#component" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">Component</span></tt> class needs to be persisted, so it’s going to
be an <tt class="docutils literal"><span class="pre">Entity</span></tt>. As the top of the inheritance hierarchy, it’s going
to have to define the persistent inheritance. For this example, we
will use Single Table Inheritance, but Class Table Inheritance
would work as well. In the discriminator map, we will define two
concrete subclasses, <tt class="docutils literal"><span class="pre">ConcreteComponent</span></tt> and <tt class="docutils literal"><span class="pre">ConcreteDecorator</span></tt>.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">namespace</span> <span class="nx">Test</span><span class="p">;</span>
<span class="sd">/**</span>
<span class="sd"> * @Entity</span>
<span class="sd"> * @InheritanceType("SINGLE_TABLE")</span>
<span class="sd"> * @DiscriminatorColumn(name="discr", type="string")</span>
<span class="sd"> * @DiscriminatorMap({"cc" = "Test\Component\ConcreteComponent",</span>
<span class="sd"> "cd" = "Test\Decorator\ConcreteDecorator"})</span>
<span class="sd"> */</span>
<span class="k">abstract</span> <span class="k">class</span> <span class="nc">Component</span>
<span class="p">{</span>
<span class="sd">/**</span>
<span class="sd"> * @Id @Column(type="integer")</span>
<span class="sd"> * @GeneratedValue(strategy="AUTO")</span>
<span class="sd"> */</span>
<span class="k">protected</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @Column(type="string", nullable=true) */</span>
<span class="k">protected</span> <span class="nv">$name</span><span class="p">;</span>
<span class="sd">/**</span>
<span class="sd"> * Get id</span>
<span class="sd"> * @return integer $id</span>
<span class="sd"> */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getId</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">id</span><span class="p">;</span>
<span class="p">}</span>
<span class="sd">/**</span>
<span class="sd"> * Set name</span>
<span class="sd"> * @param string $name</span>
<span class="sd"> */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setName</span><span class="p">(</span><span class="nv">$name</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">name</span> <span class="o">=</span> <span class="nv">$name</span><span class="p">;</span>
<span class="p">}</span>
<span class="sd">/**</span>
<span class="sd"> * Get name</span>
<span class="sd"> * @return string $name</span>
<span class="sd"> */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getName</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">name</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="concretecomponent">
<h2>ConcreteComponent<a class="headerlink" href="#concretecomponent" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">ConcreteComponent</span></tt> class is pretty simple and doesn’t do much
more than extend the abstract <tt class="docutils literal"><span class="pre">Component</span></tt> class (only for the
purpose of keeping this example simple).</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">namespace</span> <span class="nx">Test\Component</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Test\Component</span><span class="p">;</span>
<span class="sd">/** @Entity */</span>
<span class="k">class</span> <span class="nc">ConcreteComponent</span> <span class="k">extends</span> <span class="nx">Component</span>
<span class="p">{}</span>
</pre></div>
</div>
</div>
<div class="section" id="decorator">
<h2>Decorator<a class="headerlink" href="#decorator" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">Decorator</span></tt> class doesn’t need to be persisted, but it does
need to define an association with a persisted <tt class="docutils literal"><span class="pre">Entity</span></tt>. We can
use a <tt class="docutils literal"><span class="pre">MappedSuperclass</span></tt> for this.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">namespace</span> <span class="nx">Test</span><span class="p">;</span>
<span class="sd">/** @MappedSuperclass */</span>
<span class="k">abstract</span> <span class="k">class</span> <span class="nc">Decorator</span> <span class="k">extends</span> <span class="nx">Component</span>
<span class="p">{</span>
<span class="sd">/**</span>
<span class="sd"> * @OneToOne(targetEntity="Test\Component", cascade={"all"})</span>
<span class="sd"> * @JoinColumn(name="decorates", referencedColumnName="id")</span>
<span class="sd"> */</span>
<span class="k">protected</span> <span class="nv">$decorates</span><span class="p">;</span>
<span class="sd">/**</span>
<span class="sd"> * initialize the decorator</span>
<span class="sd"> * @param Component $c</span>
<span class="sd"> */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">__construct</span><span class="p">(</span><span class="nx">Component</span> <span class="nv">$c</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">setDecorates</span><span class="p">(</span><span class="nv">$c</span><span class="p">);</span>
<span class="p">}</span>
<span class="sd">/**</span>
<span class="sd"> * (non-PHPdoc)</span>
<span class="sd"> * @see Test.Component::getName()</span>
<span class="sd"> */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getName</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="s1">'Decorated '</span> <span class="o">.</span> <span class="nv">$this</span><span class="o">-></span><span class="na">getDecorates</span><span class="p">()</span><span class="o">-></span><span class="na">getName</span><span class="p">();</span>
<span class="p">}</span>
<span class="sd">/**</span>
<span class="sd"> * the component being decorated</span>
<span class="sd"> * @return Component</span>
<span class="sd"> */</span>
<span class="k">protected</span> <span class="k">function</span> <span class="nf">getDecorates</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">decorates</span><span class="p">;</span>
<span class="p">}</span>
<span class="sd">/**</span>
<span class="sd"> * sets the component being decorated</span>
<span class="sd"> * @param Component $c</span>
<span class="sd"> */</span>
<span class="k">protected</span> <span class="k">function</span> <span class="nf">setDecorates</span><span class="p">(</span><span class="nx">Component</span> <span class="nv">$c</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">decorates</span> <span class="o">=</span> <span class="nv">$c</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>All operations on the <tt class="docutils literal"><span class="pre">Decorator</span></tt> (i.e. persist, remove, etc) will
cascade from the <tt class="docutils literal"><span class="pre">Decorator</span></tt> to the <tt class="docutils literal"><span class="pre">Component</span></tt>. This means that
when we persist a <tt class="docutils literal"><span class="pre">Decorator</span></tt>, Doctrine will take care of
persisting the chain of decorated objects for us. A <tt class="docutils literal"><span class="pre">Decorator</span></tt> can
be treated exactly as a <tt class="docutils literal"><span class="pre">Component</span></tt> when it comes time to
persisting it.</p>
<p>The <tt class="docutils literal"><span class="pre">Decorator's</span></tt> constructor accepts an instance of a
<tt class="docutils literal"><span class="pre">Component</span></tt>, as defined by the <tt class="docutils literal"><span class="pre">Decorator</span></tt> pattern. The
setDecorates/getDecorates methods have been defined as protected to
hide the fact that a <tt class="docutils literal"><span class="pre">Decorator</span></tt> is decorating a <tt class="docutils literal"><span class="pre">Component</span></tt> and
keeps the <tt class="docutils literal"><span class="pre">Component</span></tt> interface and the <tt class="docutils literal"><span class="pre">Decorator</span></tt> interface
identical.</p>
<p>To illustrate the intended result of the <tt class="docutils literal"><span class="pre">Decorator</span></tt> pattern, the
getName() method has been overridden to append a string to the
<tt class="docutils literal"><span class="pre">Component's</span></tt> getName() method.</p>
</div>
<div class="section" id="concretedecorator">
<h2>ConcreteDecorator<a class="headerlink" href="#concretedecorator" title="Permalink to this headline">¶</a></h2>
<p>The final class required to complete a simple implementation of the
Decorator pattern is the <tt class="docutils literal"><span class="pre">ConcreteDecorator</span></tt>. In order to further
illustrate how the <tt class="docutils literal"><span class="pre">Decorator</span></tt> can alter data as it moves through
the chain of decoration, a new field, “special”, has been added to
this class. The getName() has been overridden and appends the value
of the getSpecial() method to its return value.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">namespace</span> <span class="nx">Test\Decorator</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Test\Decorator</span><span class="p">;</span>
<span class="sd">/** @Entity */</span>
<span class="k">class</span> <span class="nc">ConcreteDecorator</span> <span class="k">extends</span> <span class="nx">Decorator</span>
<span class="p">{</span>
<span class="sd">/** @Column(type="string", nullable=true) */</span>
<span class="k">protected</span> <span class="nv">$special</span><span class="p">;</span>
<span class="sd">/**</span>
<span class="sd"> * Set special</span>
<span class="sd"> * @param string $special</span>
<span class="sd"> */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setSpecial</span><span class="p">(</span><span class="nv">$special</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-></span><span class="na">special</span> <span class="o">=</span> <span class="nv">$special</span><span class="p">;</span>
<span class="p">}</span>
<span class="sd">/**</span>
<span class="sd"> * Get special</span>
<span class="sd"> * @return string $special</span>
<span class="sd"> */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getSpecial</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">special</span><span class="p">;</span>
<span class="p">}</span>
<span class="sd">/**</span>
<span class="sd"> * (non-PHPdoc)</span>
<span class="sd"> * @see Test.Component::getName()</span>
<span class="sd"> */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getName</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="s1">'['</span> <span class="o">.</span> <span class="nv">$this</span><span class="o">-></span><span class="na">getSpecial</span><span class="p">()</span>
<span class="o">.</span> <span class="s1">'] '</span> <span class="o">.</span> <span class="k">parent</span><span class="o">::</span><span class="na">getName</span><span class="p">();</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="examples">
<h2>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h2>
<p>Here is an example of how to persist and retrieve your decorated
objects</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
<span class="k">use</span> <span class="nx">Test\Component\ConcreteComponent</span><span class="p">,</span>
<span class="nx">Test\Decorator\ConcreteDecorator</span><span class="p">;</span>
<span class="c1">// assumes Doctrine 2 is configured and an instance of</span>
<span class="c1">// an EntityManager is available as $em</span>
<span class="c1">// create a new concrete component</span>
<span class="nv">$c</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ConcreteComponent</span><span class="p">();</span>
<span class="nv">$c</span><span class="o">-></span><span class="na">setName</span><span class="p">(</span><span class="s1">'Test Component 1'</span><span class="p">);</span>
<span class="nv">$em</span><span class="o">-></span><span class="na">persist</span><span class="p">(</span><span class="nv">$c</span><span class="p">);</span> <span class="c1">// assigned unique ID = 1</span>
<span class="c1">// create a new concrete decorator</span>
<span class="nv">$c</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ConcreteComponent</span><span class="p">();</span>
<span class="nv">$c</span><span class="o">-></span><span class="na">setName</span><span class="p">(</span><span class="s1">'Test Component 2'</span><span class="p">);</span>
<span class="nv">$d</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ConcreteDecorator</span><span class="p">(</span><span class="nv">$c</span><span class="p">);</span>
<span class="nv">$d</span><span class="o">-></span><span class="na">setSpecial</span><span class="p">(</span><span class="s1">'Really'</span><span class="p">);</span>
<span class="nv">$em</span><span class="o">-></span><span class="na">persist</span><span class="p">(</span><span class="nv">$d</span><span class="p">);</span>
<span class="c1">// assigns c as unique ID = 2, and d as unique ID = 3</span>
<span class="nv">$em</span><span class="o">-></span><span class="na">flush</span><span class="p">();</span>
<span class="nv">$c</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">find</span><span class="p">(</span><span class="s1">'Test\Component'</span><span class="p">,</span> <span class="mi">1</span><span class="p">);</span>
<span class="nv">$d</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">find</span><span class="p">(</span><span class="s1">'Test\Component'</span><span class="p">,</span> <span class="mi">3</span><span class="p">);</span>
<span class="k">echo</span> <span class="nb">get_class</span><span class="p">(</span><span class="nv">$c</span><span class="p">);</span>
<span class="c1">// prints: Test\Component\ConcreteComponent</span>
<span class="k">echo</span> <span class="nv">$c</span><span class="o">-></span><span class="na">getName</span><span class="p">();</span>
<span class="c1">// prints: Test Component 1</span>
<span class="k">echo</span> <span class="nb">get_class</span><span class="p">(</span><span class="nv">$d</span><span class="p">)</span>
<span class="c1">// prints: Test\Component\ConcreteDecorator</span>
<span class="k">echo</span> <span class="nv">$d</span><span class="o">-></span><span class="na">getName</span><span class="p">();</span>
<span class="c1">// prints: [Really] Decorated Test Component 2</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="#">Persisting the Decorator Pattern</a><ul>
<li><a class="reference internal" href="#component">Component</a></li>
<li><a class="reference internal" href="#concretecomponent">ConcreteComponent</a></li>
<li><a class="reference internal" href="#decorator">Decorator</a></li>
<li><a class="reference internal" href="#concretedecorator">ConcreteDecorator</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="aggregate-fields.html"
title="previous chapter">Aggregate Fields</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="dql-custom-walkers.html"
title="next chapter">Extending DQL in Doctrine 2: Custom AST Walkers</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/cookbook/decorator-pattern.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>
|