213 lines
12 KiB
HTML
213 lines
12 KiB
HTML
|
<!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>Implementing the Notify ChangeTracking Policy — Doctrine MongoDB ODM 1.1.5 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: '1.1.5',
|
||
|
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="search" title="Search" href="../search.html" />
|
||
|
<link rel="top" title="Doctrine MongoDB ODM 1.1.5 documentation" href="../index.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="http://www.doctrine-project.org/">Home</a></li>
|
||
|
<li><a target="_top" href="http://www.doctrine-project.org/about.html">About</a></li>
|
||
|
<li><a target="_top" href="http://www.doctrine-project.org/projects.html">Projects</a></li>
|
||
|
<li><a target="_top" href="http://www.doctrine-project.org/contribute.html">Contribute</a></li>
|
||
|
<li><a target="_top" href="http://www.doctrine-project.org/community.html">Community</a></li>
|
||
|
<li><a target="_top" href="http://www.doctrine-project.org/archive.html">Blog</a></li>
|
||
|
<li><a target="_top" href="http://www.doctrine-project.org/jira">Development</a></li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div id="content" class="cls">
|
||
|
<div class="related">
|
||
|
<h3>Navigation</h3>
|
||
|
<ul>
|
||
|
<li><a href="/">Doctrine Homepage</a> »</li>
|
||
|
<li><a href="../index.html">Doctrine MongoDB ODM 1.1.5 documentation</a> »</li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
|
||
|
<div class="document">
|
||
|
<div class="documentwrapper">
|
||
|
<div class="bodywrapper">
|
||
|
|
||
|
<div class="body" >
|
||
|
|
||
|
<div class="section" id="implementing-the-notify-changetracking-policy">
|
||
|
<h1>Implementing the Notify ChangeTracking Policy<a class="headerlink" href="#implementing-the-notify-changetracking-policy" title="Permalink to this headline">¶</a></h1>
|
||
|
<p><em>Section author: Roman Borschel (<a class="reference external" href="mailto:roman%40code-factory.org">roman<span>@</span>code-factory<span>.</span>org</a>)</em></p>
|
||
|
<p>The NOTIFY change-tracking policy is the most effective
|
||
|
change-tracking policy provided by Doctrine but it requires some
|
||
|
boilerplate code. This recipe will show you how this boilerplate
|
||
|
code should look like. We will implement it on a
|
||
|
<a class="reference external" href="http://martinfowler.com/eaaCatalog/layerSupertype.html">Layer Supertype</a>
|
||
|
for all our domain objects.</p>
|
||
|
<div class="section" id="implementing-notifypropertychanged">
|
||
|
<h2>Implementing NotifyPropertyChanged<a class="headerlink" href="#implementing-notifypropertychanged" title="Permalink to this headline">¶</a></h2>
|
||
|
<p>The NOTIFY policy is based on the assumption that the entities
|
||
|
notify interested listeners of changes to their properties. For
|
||
|
that purpose, a class that wants to use this policy needs to
|
||
|
implement the <code class="docutils literal"><span class="pre">NotifyPropertyChanged</span></code> interface from the
|
||
|
<code class="docutils literal"><span class="pre">Doctrine\Common</span></code> namespace.</p>
|
||
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
||
|
|
||
|
<span class="k">use</span> <span class="nx">Doctrine\Common\NotifyPropertyChanged</span><span class="p">,</span>
|
||
|
<span class="nx">Doctrine\Common\PropertyChangedListener</span><span class="p">;</span>
|
||
|
|
||
|
<span class="k">abstract</span> <span class="k">class</span> <span class="nc">DomainObject</span> <span class="k">implements</span> <span class="nx">NotifyPropertyChanged</span>
|
||
|
<span class="p">{</span>
|
||
|
<span class="k">private</span> <span class="nv">$_listeners</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span>
|
||
|
|
||
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">addPropertyChangedListener</span><span class="p">(</span><span class="nx">PropertyChangedListener</span> <span class="nv">$listener</span><span class="p">)</span>
|
||
|
<span class="p">{</span>
|
||
|
<span class="nv">$this</span><span class="o">-></span><span class="na">_listeners</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$listener</span><span class="p">;</span>
|
||
|
<span class="p">}</span>
|
||
|
|
||
|
<span class="sd">/** Notifies listeners of a change. */</span>
|
||
|
<span class="k">protected</span> <span class="k">function</span> <span class="nf">_onPropertyChanged</span><span class="p">(</span><span class="nv">$propName</span><span class="p">,</span> <span class="nv">$oldValue</span><span class="p">,</span> <span class="nv">$newValue</span><span class="p">)</span>
|
||
|
<span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">_listeners</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-></span><span class="na">_listeners</span> <span class="k">as</span> <span class="nv">$listener</span><span class="p">)</span> <span class="p">{</span>
|
||
|
<span class="nv">$listener</span><span class="o">-></span><span class="na">propertyChanged</span><span class="p">(</span><span class="nv">$this</span><span class="p">,</span> <span class="nv">$propName</span><span class="p">,</span> <span class="nv">$oldValue</span><span class="p">,</span> <span class="nv">$newValue</span><span class="p">);</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="p">}</span>
|
||
|
</pre></div>
|
||
|
</div>
|
||
|
<p>Then, in each property setter of concrete, derived domain classes,
|
||
|
you need to invoke _onPropertyChanged as follows to notify
|
||
|
listeners:</p>
|
||
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
||
|
|
||
|
<span class="c1">// Mapping not shown, either in annotations, xml or yaml as usual</span>
|
||
|
<span class="k">class</span> <span class="nc">MyEntity</span> <span class="k">extends</span> <span class="nx">DomainObject</span>
|
||
|
<span class="p">{</span>
|
||
|
<span class="k">private</span> <span class="nv">$data</span><span class="p">;</span>
|
||
|
<span class="c1">// ... other fields as usual</span>
|
||
|
|
||
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">setData</span><span class="p">(</span><span class="nv">$data</span><span class="p">)</span>
|
||
|
<span class="p">{</span>
|
||
|
<span class="k">if</span> <span class="p">(</span><span class="nv">$data</span> <span class="o">!=</span> <span class="nv">$this</span><span class="o">-></span><span class="na">data</span><span class="p">)</span> <span class="p">{</span> <span class="c1">// check: is it actually modified?</span>
|
||
|
<span class="nv">$this</span><span class="o">-></span><span class="na">_onPropertyChanged</span><span class="p">(</span><span class="s1">'data'</span><span class="p">,</span> <span class="nv">$this</span><span class="o">-></span><span class="na">data</span><span class="p">,</span> <span class="nv">$data</span><span class="p">);</span>
|
||
|
<span class="nv">$this</span><span class="o">-></span><span class="na">data</span> <span class="o">=</span> <span class="nv">$data</span><span class="p">;</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="p">}</span>
|
||
|
<span class="p">}</span>
|
||
|
</pre></div>
|
||
|
</div>
|
||
|
<p>The check whether the new value is different from the old one is
|
||
|
not mandatory but recommended. That way you can avoid unnecessary
|
||
|
updates and also have full control over when you consider a
|
||
|
property changed.</p>
|
||
|
</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="#">Implementing the Notify ChangeTracking Policy</a><ul>
|
||
|
<li><a class="reference internal" href="#implementing-notifypropertychanged">Implementing NotifyPropertyChanged</a></li>
|
||
|
</ul>
|
||
|
</li>
|
||
|
</ul>
|
||
|
|
||
|
<h3>This Page</h3>
|
||
|
<ul class="this-page-menu">
|
||
|
<li><a href="../_sources/cookbook/implementing-the-notify-changetracking-policy.rst.txt"
|
||
|
rel="nofollow">Show Source</a></li>
|
||
|
</ul>
|
||
|
</div>
|
||
|
</div>
|
||
|
<div class="clearer"></div>
|
||
|
</div>
|
||
|
<div class="footer">
|
||
|
© Copyright 2013, Doctrine Project Team.
|
||
|
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.6.2.
|
||
|
<br/>
|
||
|
<a target="_BLANK" href="http://www.servergrove.com"><img src="http://www.doctrine-project.org/_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="image" src="https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!" />
|
||
|
<img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1" />
|
||
|
</form>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<div id="bot-rcnr">
|
||
|
<div class="tl"><!-- corner --></div>
|
||
|
</div>
|
||
|
</div>
|
||
|
|
||
|
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
|
||
|
</script>
|
||
|
<script type="text/javascript">
|
||
|
_uacct = "UA-288343-7";
|
||
|
urchinTracker();
|
||
|
</script>
|
||
|
<a class="githublink" href="http://github.com/doctrine"><img src="https://s3.amazonaws.com/github/ribbons/forkme_right_orange_ff7600.png" alt="Fork me on GitHub"></a>
|
||
|
</body>
|
||
|
</html>
|