796 lines
57 KiB
HTML
796 lines
57 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>Events — 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="events">
|
|
<h1>Events<a class="headerlink" href="#events" title="Permalink to this headline">¶</a></h1>
|
|
<p>Doctrine features a lightweight event system that is part of the
|
|
Common package.</p>
|
|
<div class="section" id="the-event-system">
|
|
<h2>The Event System<a class="headerlink" href="#the-event-system" title="Permalink to this headline">¶</a></h2>
|
|
<p>The event system is controlled by the <code class="docutils literal"><span class="pre">EventManager</span></code>. It is the
|
|
central point of Doctrine's event listener system. Listeners are
|
|
registered on the manager and events are dispatched through the
|
|
manager.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventManager</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Now we can add some event listeners to the <code class="docutils literal"><span class="pre">$evm</span></code>. Let's create a
|
|
<code class="docutils literal"><span class="pre">EventTest</span></code> class to play around with.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">const</span> <span class="no">preFoo</span> <span class="o">=</span> <span class="s1">'preFoo'</span><span class="p">;</span>
|
|
<span class="k">const</span> <span class="no">postFoo</span> <span class="o">=</span> <span class="s1">'postFoo'</span><span class="p">;</span>
|
|
|
|
<span class="k">private</span> <span class="nv">$_evm</span><span class="p">;</span>
|
|
|
|
<span class="k">public</span> <span class="nv">$preFooInvoked</span> <span class="o">=</span> <span class="k">false</span><span class="p">;</span>
|
|
<span class="k">public</span> <span class="nv">$postFooInvoked</span> <span class="o">=</span> <span class="k">false</span><span class="p">;</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">__construct</span><span class="p">(</span><span class="nv">$evm</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nx">self</span><span class="o">::</span><span class="na">preFoo</span><span class="p">,</span> <span class="nx">self</span><span class="o">::</span><span class="na">postFoo</span><span class="p">),</span> <span class="nv">$this</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">preFoo</span><span class="p">(</span><span class="nx">EventArgs</span> <span class="nv">$e</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">preFooInvoked</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">postFoo</span><span class="p">(</span><span class="nx">EventArgs</span> <span class="nv">$e</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">postFooInvoked</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="c1">// Create a new instance</span>
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">(</span><span class="nv">$evm</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Events can be dispatched by using the <code class="docutils literal"><span class="pre">dispatchEvent()</span></code> method.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">dispatchEvent</span><span class="p">(</span><span class="nx">EventTest</span><span class="o">::</span><span class="na">preFoo</span><span class="p">);</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">dispatchEvent</span><span class="p">(</span><span class="nx">EventTest</span><span class="o">::</span><span class="na">postFoo</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>You can easily remove a listener with the <code class="docutils literal"><span class="pre">removeEventListener()</span></code>
|
|
method.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">removeEventListener</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nx">self</span><span class="o">::</span><span class="na">preFoo</span><span class="p">,</span> <span class="nx">self</span><span class="o">::</span><span class="na">postFoo</span><span class="p">),</span> <span class="nv">$this</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The Doctrine event system also has a simple concept of event
|
|
subscribers. We can define a simple <code class="docutils literal"><span class="pre">TestEventSubscriber</span></code> class
|
|
which implements the <code class="docutils literal"><span class="pre">\Doctrine\Common\EventSubscriber</span></code> interface
|
|
and implements a <code class="docutils literal"><span class="pre">getSubscribedEvents()</span></code> method which returns an
|
|
array of events it should be subscribed to.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">TestEventSubscriber</span> <span class="k">implements</span> <span class="nx">\Doctrine\Common\EventSubscriber</span>
|
|
<span class="p">{</span>
|
|
<span class="k">const</span> <span class="no">preFoo</span> <span class="o">=</span> <span class="s1">'preFoo'</span><span class="p">;</span>
|
|
|
|
<span class="k">public</span> <span class="nv">$preFooInvoked</span> <span class="o">=</span> <span class="k">false</span><span class="p">;</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">preFoo</span><span class="p">()</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">preFooInvoked</span> <span class="o">=</span> <span class="k">true</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">getSubscribedEvents</span><span class="p">()</span>
|
|
<span class="p">{</span>
|
|
<span class="k">return</span> <span class="k">array</span><span class="p">(</span><span class="nx">self</span><span class="o">::</span><span class="na">preFoo</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="nv">$eventSubscriber</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">TestEventSubscriber</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventSubscriber</span><span class="p">(</span><span class="nv">$eventSubscriber</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Now when you dispatch an event any event subscribers will be
|
|
notified for that event.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">dispatchEvent</span><span class="p">(</span><span class="nx">TestEventSubscriber</span><span class="o">::</span><span class="na">preFoo</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Now test the <code class="docutils literal"><span class="pre">$eventSubscriber</span></code> instance to see if the
|
|
<code class="docutils literal"><span class="pre">preFoo()</span></code> method was invoked.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">if</span> <span class="p">(</span><span class="nv">$eventSubscriber</span><span class="o">-></span><span class="na">preFooInvoked</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="k">echo</span> <span class="s1">'pre foo invoked!'</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="lifecycle-events">
|
|
<span id="id1"></span><h2>Lifecycle Events<a class="headerlink" href="#lifecycle-events" title="Permalink to this headline">¶</a></h2>
|
|
<p>The DocumentManager and UnitOfWork trigger several events during
|
|
the life-time of their registered documents.</p>
|
|
<ul class="simple">
|
|
<li>preRemove - The preRemove event occurs for a given document before
|
|
the respective DocumentManager remove operation for that document
|
|
is executed.</li>
|
|
<li>postRemove - The postRemove event occurs for a document after the
|
|
document has been removed. It will be invoked after the database
|
|
delete operations.</li>
|
|
<li>prePersist - The prePersist event occurs for a given document
|
|
before the respective DocumentManager persist operation for that
|
|
document is executed.</li>
|
|
<li>postPersist - The postPersist event occurs for a document after
|
|
the document has been made persistent. It will be invoked after the
|
|
database insert operations. Generated primary key values are
|
|
available in the postPersist event.</li>
|
|
<li>preUpdate - The preUpdate event occurs before the database update
|
|
operations to document data.</li>
|
|
<li>postUpdate - The postUpdate event occurs after the database update
|
|
operations to document data.</li>
|
|
<li>preLoad - The preLoad event occurs for a document before the
|
|
document has been loaded into the current DocumentManager from the
|
|
database or after the refresh operation has been applied to it.</li>
|
|
<li>postLoad - The postLoad event occurs for a document after the
|
|
document has been loaded into the current DocumentManager from the
|
|
database or after the refresh operation has been applied to it.</li>
|
|
<li>loadClassMetadata - The loadClassMetadata event occurs after the
|
|
mapping metadata for a class has been loaded from a mapping source
|
|
(annotations/xml/yaml).</li>
|
|
<li>preFlush - The preFlush event occurs before the change-sets of all
|
|
managed documents are computed. This both a lifecycle call back and
|
|
and listener.</li>
|
|
<li>postFlush - The postFlush event occurs after the change-sets of all
|
|
managed documents are computed.</li>
|
|
<li>onFlush - The onFlush event occurs after the change-sets of all
|
|
managed documents are computed. This event is not a lifecycle
|
|
callback.</li>
|
|
<li>onClear - The onClear event occurs after the UnitOfWork has had
|
|
its state cleared.</li>
|
|
<li>documentNotFound - The documentNotFound event occurs when a proxy object
|
|
could not be initialized. This event is not a lifecycle callback.</li>
|
|
<li>postCollectionLoad - The postCollectionLoad event occurs just after
|
|
collection has been initialized (loaded) and before new elements
|
|
are re-added to it.</li>
|
|
</ul>
|
|
<p>You can access the Event constants from the <code class="docutils literal"><span class="pre">Events</span></code> class in the
|
|
ODM package.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">use</span> <span class="nx">Doctrine\ODM\MongoDB\Events</span><span class="p">;</span>
|
|
|
|
<span class="k">echo</span> <span class="nx">Events</span><span class="o">::</span><span class="na">preUpdate</span><span class="p">;</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>These can be hooked into by two different types of event
|
|
listeners:</p>
|
|
<ul class="simple">
|
|
<li>Lifecycle Callbacks are methods on the document classes that are
|
|
called when the event is triggered. They receive instances
|
|
of <code class="docutils literal"><span class="pre">Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span></code> (see relevant
|
|
examples below) as arguments and are specifically designed to allow
|
|
changes inside the document classes state.</li>
|
|
<li>Lifecycle Event Listeners are classes with specific callback
|
|
methods that receives some kind of <code class="docutils literal"><span class="pre">EventArgs</span></code> instance which
|
|
give access to the document, DocumentManager or other relevant
|
|
data.</li>
|
|
</ul>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">All Lifecycle events that happen during the <code class="docutils literal"><span class="pre">flush()</span></code> of
|
|
a DocumentManager have very specific constraints on the allowed
|
|
operations that can be executed. Please read the
|
|
<em>Implementing Event Listeners</em> section very carefully to understand
|
|
which operations are allowed in which lifecycle event.</p>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="lifecycle-callbacks">
|
|
<h2>Lifecycle Callbacks<a class="headerlink" href="#lifecycle-callbacks" title="Permalink to this headline">¶</a></h2>
|
|
<p>A lifecycle event is a regular event with the additional feature of
|
|
providing a mechanism to register direct callbacks inside the
|
|
corresponding document classes that are executed when the lifecycle
|
|
event occurs.</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="sd">/** @Document @HasLifecycleCallbacks */</span>
|
|
<span class="k">class</span> <span class="nc">User</span>
|
|
<span class="p">{</span>
|
|
<span class="c1">// ...</span>
|
|
|
|
<span class="sd">/**</span>
|
|
<span class="sd"> * @Field</span>
|
|
<span class="sd"> */</span>
|
|
<span class="k">public</span> <span class="nv">$value</span><span class="p">;</span>
|
|
|
|
<span class="sd">/** @Field */</span>
|
|
<span class="k">private</span> <span class="nv">$createdAt</span><span class="p">;</span>
|
|
|
|
<span class="sd">/** @PrePersist */</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">doStuffOnPrePersist</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">createdAt</span> <span class="o">=</span> <span class="nb">date</span><span class="p">(</span><span class="s1">'Y-m-d H:i:s'</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="sd">/** @PrePersist */</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">doOtherStuffOnPrePersist</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">value</span> <span class="o">=</span> <span class="s1">'changed from prePersist callback!'</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="sd">/** @PostPersist */</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">doStuffOnPostPersist</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">value</span> <span class="o">=</span> <span class="s1">'changed from postPersist callback!'</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="sd">/** @PreLoad */</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">doStuffOnPreLoad</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\PreLoadEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$data</span> <span class="o">=&</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getData</span><span class="p">();</span>
|
|
<span class="nv">$data</span><span class="p">[</span><span class="s1">'value'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'changed from preLoad callback'</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="sd">/** @PostLoad */</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">doStuffOnPostLoad</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">value</span> <span class="o">=</span> <span class="s1">'changed from postLoad callback!'</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="sd">/** @PreUpdate */</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">doStuffOnPreUpdate</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\PreUpdateEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">value</span> <span class="o">=</span> <span class="s1">'changed from preUpdate callback!'</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="sd">/** @PreFlush */</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">preFlush</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\PreFlushEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">value</span> <span class="o">=</span> <span class="s1">'changed from preFlush callback!'</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Note that when using annotations you have to apply the
|
|
@HasLifecycleCallbacks marker annotation on the document class.</p>
|
|
</div>
|
|
<div class="section" id="listening-to-lifecycle-events">
|
|
<h2>Listening to Lifecycle Events<a class="headerlink" href="#listening-to-lifecycle-events" title="Permalink to this headline">¶</a></h2>
|
|
<p>Lifecycle event listeners are much more powerful than the simple
|
|
lifecycle callbacks that are defined on the document classes. They
|
|
allow to implement re-usable behaviours between different document
|
|
classes, yet require much more detailed knowledge about the inner
|
|
workings of the DocumentManager and UnitOfWork. Please read the
|
|
<em>Implementing Event Listeners</em> section carefully if you are trying
|
|
to write your own listener.</p>
|
|
<p>To register an event listener you have to hook it into the
|
|
EventManager that is passed to the DocumentManager factory:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$eventManager</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventManager</span><span class="p">();</span>
|
|
<span class="nv">$eventManager</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">preUpdate</span><span class="p">),</span> <span class="k">new</span> <span class="nx">MyEventListener</span><span class="p">());</span>
|
|
<span class="nv">$eventManager</span><span class="o">-></span><span class="na">addEventSubscriber</span><span class="p">(</span><span class="k">new</span> <span class="nx">MyEventSubscriber</span><span class="p">());</span>
|
|
|
|
<span class="nv">$documentManager</span> <span class="o">=</span> <span class="nx">DocumentManager</span><span class="o">::</span><span class="na">create</span><span class="p">(</span><span class="nv">$mongo</span><span class="p">,</span> <span class="nv">$config</span><span class="p">,</span> <span class="nv">$eventManager</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>You can also retrieve the event manager instance after the
|
|
DocumentManager was created:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$documentManager</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">()</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="k">array</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">preUpdate</span><span class="p">),</span> <span class="k">new</span> <span class="nx">MyEventListener</span><span class="p">());</span>
|
|
<span class="nv">$documentManager</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">()</span><span class="o">-></span><span class="na">addEventSubscriber</span><span class="p">(</span><span class="k">new</span> <span class="nx">MyEventSubscriber</span><span class="p">());</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="implementing-event-listeners">
|
|
<h2>Implementing Event Listeners<a class="headerlink" href="#implementing-event-listeners" title="Permalink to this headline">¶</a></h2>
|
|
<p>This section explains what is and what is not allowed during
|
|
specific lifecycle events of the UnitOfWork. Although you get
|
|
passed the DocumentManager in all of these events, you have to
|
|
follow this restrictions very carefully since operations in the
|
|
wrong event may produce lots of different errors, such as
|
|
inconsistent data and lost updates/persists/removes.</p>
|
|
<div class="section" id="prepersist">
|
|
<h3>prePersist<a class="headerlink" href="#prepersist" title="Permalink to this headline">¶</a></h3>
|
|
<p>Listen to the <code class="docutils literal"><span class="pre">prePersist</span></code> event:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">prePersist</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">prePersist</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$document</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getDocument</span><span class="p">();</span>
|
|
<span class="nv">$document</span><span class="o">-></span><span class="na">setSomething</span><span class="p">();</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="preload">
|
|
<h3>preLoad<a class="headerlink" href="#preload" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">preLoad</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class with a <code class="docutils literal"><span class="pre">preLoad()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">preLoad</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\PreLoadEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$data</span> <span class="o">=&</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getData</span><span class="p">();</span>
|
|
<span class="c1">// do something</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="postload">
|
|
<h3>postLoad<a class="headerlink" href="#postload" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">postLoad</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class with a <code class="docutils literal"><span class="pre">postLoad()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">postLoad</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$document</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getDocument</span><span class="p">();</span>
|
|
<span class="c1">// do something</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="preremove">
|
|
<h3>preRemove<a class="headerlink" href="#preremove" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">preRemove</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class with a <code class="docutils literal"><span class="pre">preRemove()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">preRemove</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$document</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getDocument</span><span class="p">();</span>
|
|
<span class="c1">// do something</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="preflush">
|
|
<h3>preFlush<a class="headerlink" href="#preflush" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">preFlush</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class with a <code class="docutils literal"><span class="pre">preFlush()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">preFlush</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\PreFlushEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$dm</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getDocumentManager</span><span class="p">();</span>
|
|
<span class="nv">$uow</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getUnitOfWork</span><span class="p">();</span>
|
|
<span class="c1">// do something</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="onflush">
|
|
<h3>onFlush<a class="headerlink" href="#onflush" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">onFlush</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class with a <code class="docutils literal"><span class="pre">onFlush()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">onFlush</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\OnFlushEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$dm</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getDocumentManager</span><span class="p">();</span>
|
|
<span class="nv">$uow</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getUnitOfWork</span><span class="p">();</span>
|
|
<span class="c1">// do something</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="postflush">
|
|
<h3>postFlush<a class="headerlink" href="#postflush" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">postFlush</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class with a <code class="docutils literal"><span class="pre">postFlush()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">postFlush</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\PostFlushEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$dm</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getDocumentManager</span><span class="p">();</span>
|
|
<span class="nv">$uow</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getUnitOfWork</span><span class="p">();</span>
|
|
<span class="c1">// do something</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="preupdate">
|
|
<h3>preUpdate<a class="headerlink" href="#preupdate" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">preUpdate</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class with a <code class="docutils literal"><span class="pre">preUpdate()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">preUpdate</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$document</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getDocument</span><span class="p">();</span>
|
|
<span class="nv">$document</span><span class="o">-></span><span class="na">setSomething</span><span class="p">();</span>
|
|
<span class="nv">$dm</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getDocumentManager</span><span class="p">();</span>
|
|
<span class="nv">$class</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getClassMetadata</span><span class="p">(</span><span class="nb">get_class</span><span class="p">(</span><span class="nv">$document</span><span class="p">));</span>
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">getUnitOfWork</span><span class="p">()</span><span class="o">-></span><span class="na">recomputeSingleDocumentChangeSet</span><span class="p">(</span><span class="nv">$class</span><span class="p">,</span> <span class="nv">$document</span><span class="p">);</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">If you modify a document in the preUpdate event you must call <code class="docutils literal"><span class="pre">recomputeSingleDocumentChangeSet</span></code>
|
|
for the modified document in order for the changes to be persisted.</p>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="onclear">
|
|
<h3>onClear<a class="headerlink" href="#onclear" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">onClear</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class with a <code class="docutils literal"><span class="pre">onClear()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">onClear</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\OnClearEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$class</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getDocumentClass</span><span class="p">();</span>
|
|
<span class="nv">$dm</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getDocumentManager</span><span class="p">();</span>
|
|
<span class="nv">$uow</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getUnitOfWork</span><span class="p">();</span>
|
|
|
|
<span class="c1">// Check if event clears all documents.</span>
|
|
<span class="k">if</span> <span class="p">(</span><span class="nv">$eventArgs</span><span class="o">-></span><span class="na">clearsAllDocuments</span><span class="p">())</span> <span class="p">{</span>
|
|
<span class="c1">// do something</span>
|
|
<span class="p">}</span>
|
|
<span class="c1">// do something</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="documentnotfound">
|
|
<h3>documentNotFound<a class="headerlink" href="#documentnotfound" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">documentNotFound</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class with a <code class="docutils literal"><span class="pre">documentNotFound()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">documentNotFound</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\DocumentNotFoundEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$proxy</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getObject</span><span class="p">();</span>
|
|
<span class="nv">$identifier</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getIdentifier</span><span class="p">();</span>
|
|
<span class="c1">// do something</span>
|
|
<span class="c1">// To prevent the documentNotFound exception from being thrown, call the disableException() method:</span>
|
|
<span class="nv">$eventArgs</span><span class="o">-></span><span class="na">disableException</span><span class="p">();</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="postupdate-postremove-postpersist">
|
|
<h3>postUpdate, postRemove, postPersist<a class="headerlink" href="#postupdate-postremove-postpersist" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">postUpdate</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">postRemove</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">postPersist</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class with a <code class="docutils literal"><span class="pre">postUpdate()</span></code>, <code class="docutils literal"><span class="pre">postRemove()</span></code> and <code class="docutils literal"><span class="pre">postPersist()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">postUpdate</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">postRemove</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">postPersist</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="postcollectionload">
|
|
<h3>postCollectionLoad<a class="headerlink" href="#postcollectionload" title="Permalink to this headline">¶</a></h3>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">This event was introduced in version 1.1</p>
|
|
</div>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">postCollectionLoad</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Define the <code class="docutils literal"><span class="pre">EventTest</span></code> class with a <code class="docutils literal"><span class="pre">postCollectionLoad()</span></code> method:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">postCollectionLoad</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\PostCollectionLoadEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$collection</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getCollection</span><span class="p">();</span>
|
|
<span class="k">if</span> <span class="p">(</span><span class="nv">$collection</span> <span class="nx">instanceof</span> <span class="nx">\Malarzm\Collections\DiffableCollection</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="nv">$collection</span><span class="o">-></span><span class="na">snapshot</span><span class="p">();</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="load-classmetadata-event">
|
|
<h2>Load ClassMetadata Event<a class="headerlink" href="#load-classmetadata-event" title="Permalink to this headline">¶</a></h2>
|
|
<p>When the mapping information for a document is read, it is
|
|
populated in to a <code class="docutils literal"><span class="pre">ClassMetadata</span></code> instance. You can hook in to
|
|
this process and manipulate the instance with the <code class="docutils literal"><span class="pre">loadClassMetadata</span></code> event:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$test</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">EventTest</span><span class="p">();</span>
|
|
<span class="nv">$metadataFactory</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getMetadataFactory</span><span class="p">();</span>
|
|
<span class="nv">$evm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$evm</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span><span class="nx">Events</span><span class="o">::</span><span class="na">loadClassMetadata</span><span class="p">,</span> <span class="nv">$test</span><span class="p">);</span>
|
|
|
|
<span class="k">class</span> <span class="nc">EventTest</span>
|
|
<span class="p">{</span>
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">loadClassMetadata</span><span class="p">(</span><span class="nx">\Doctrine\ODM\MongoDB\Event\LoadClassMetadataEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$classMetadata</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getClassMetadata</span><span class="p">();</span>
|
|
<span class="nv">$fieldMapping</span> <span class="o">=</span> <span class="k">array</span><span class="p">(</span>
|
|
<span class="s1">'fieldName'</span> <span class="o">=></span> <span class="s1">'about'</span><span class="p">,</span>
|
|
<span class="s1">'type'</span> <span class="o">=></span> <span class="s1">'string'</span>
|
|
<span class="p">);</span>
|
|
<span class="nv">$classMetadata</span><span class="o">-></span><span class="na">mapField</span><span class="p">(</span><span class="nv">$fieldMapping</span><span class="p">);</span>
|
|
<span class="p">}</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="#">Events</a><ul>
|
|
<li><a class="reference internal" href="#the-event-system">The Event System</a></li>
|
|
<li><a class="reference internal" href="#lifecycle-events">Lifecycle Events</a></li>
|
|
<li><a class="reference internal" href="#lifecycle-callbacks">Lifecycle Callbacks</a></li>
|
|
<li><a class="reference internal" href="#listening-to-lifecycle-events">Listening to Lifecycle Events</a></li>
|
|
<li><a class="reference internal" href="#implementing-event-listeners">Implementing Event Listeners</a><ul>
|
|
<li><a class="reference internal" href="#prepersist">prePersist</a></li>
|
|
<li><a class="reference internal" href="#preload">preLoad</a></li>
|
|
<li><a class="reference internal" href="#postload">postLoad</a></li>
|
|
<li><a class="reference internal" href="#preremove">preRemove</a></li>
|
|
<li><a class="reference internal" href="#preflush">preFlush</a></li>
|
|
<li><a class="reference internal" href="#onflush">onFlush</a></li>
|
|
<li><a class="reference internal" href="#postflush">postFlush</a></li>
|
|
<li><a class="reference internal" href="#preupdate">preUpdate</a></li>
|
|
<li><a class="reference internal" href="#onclear">onClear</a></li>
|
|
<li><a class="reference internal" href="#documentnotfound">documentNotFound</a></li>
|
|
<li><a class="reference internal" href="#postupdate-postremove-postpersist">postUpdate, postRemove, postPersist</a></li>
|
|
<li><a class="reference internal" href="#postcollectionload">postCollectionLoad</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a class="reference internal" href="#load-classmetadata-event">Load ClassMetadata Event</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../_sources/reference/events.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> |