mongodb-odm-docs-dash/Doctrine ODM.docset/Contents/Resources/Documents/reference/custom-collections.html

303 lines
18 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>Custom Collections &mdash; 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> &raquo;</li>
<li><a href="../index.html">Doctrine MongoDB ODM 1.1.5 documentation</a> &raquo;</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" >
<div class="section" id="custom-collections">
<span id="custom-collection"></span><h1>Custom Collections<a class="headerlink" href="#custom-collections" title="Permalink to this headline"></a></h1>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This feature was introduced in version 1.1</p>
</div>
<p>By default, Doctrine uses <code class="docutils literal"><span class="pre">ArrayCollection</span></code> implementation of its <code class="docutils literal"><span class="pre">Collection</span></code>
interface to hold both embedded and referenced documents. That collection may then
be wrapped by a <code class="docutils literal"><span class="pre">PersistentCollection</span></code> to allow for change tracking and other
persistence-related features.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">use</span> <span class="nx">Doctrine\Common\Collections\ArrayCollection</span><span class="p">;</span>
<span class="sd">/** @Document */</span>
<span class="k">class</span> <span class="nc">Application</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="sd">/**</span>
<span class="sd"> * @EmbedMany(targetDocument=&quot;Section&quot;)</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$sections</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="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">sections</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ArrayCollection</span><span class="p">();</span>
<span class="p">}</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>For most cases this solution is sufficient but more sophisticated domains could use
their own collections (e.g. a collection that ensures its contained objects are sorted)
or to simply add common filtering methods that otherwise would otherwise be added to
owning document's class.</p>
<div class="section" id="custom-collection-classes">
<h2>Custom Collection Classes<a class="headerlink" href="#custom-collection-classes" title="Permalink to this headline"></a></h2>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">You may want to check <a class="reference external" href="https://github.com/malarzm/collections">malarzm/collections</a>
which provides alternative implementations of Doctrine's <code class="docutils literal"><span class="pre">Collection</span></code> interface and
aims to kickstart development of your own collections.</p>
</div>
<p>Using your own <code class="docutils literal"><span class="pre">Collection</span></code> implementation is as simple as specifying the
<code class="docutils literal"><span class="pre">collectionClass</span></code> parameter in the <code class="docutils literal"><span class="pre">&#64;EmbedMany</span></code> or <code class="docutils literal"><span class="pre">&#64;ReferenceMany</span></code> mapping
and ensuring that your custom class is initialized in the owning class' constructor:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">use</span> <span class="nx">Doctrine\Common\Collections\ArrayCollection</span><span class="p">;</span>
<span class="sd">/** @Document */</span>
<span class="k">class</span> <span class="nc">Application</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="sd">/**</span>
<span class="sd"> * @EmbedMany(</span>
<span class="sd"> * collectionClass=&quot;SectionCollection&quot;</span>
<span class="sd"> * targetDocument=&quot;Section&quot;</span>
<span class="sd"> * )</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$sections</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="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">sections</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">SectionCollection</span><span class="p">();</span>
<span class="p">}</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>If you are satisfied with <code class="docutils literal"><span class="pre">ArrayCollection</span></code> and only want
to sprinkle it with some filtering methods, you may just extend it:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">use</span> <span class="nx">Doctrine\Common\Collections\ArrayCollection</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">SectionCollection</span> <span class="k">extends</span> <span class="nx">ArrayCollection</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getEnabled</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">return</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">filter</span><span class="p">(</span><span class="k">function</span><span class="p">(</span><span class="nx">Section</span> <span class="nv">$s</span><span class="p">)</span> <span class="p">{</span>
<span class="k">return</span> <span class="nv">$s</span><span class="o">-&gt;</span><span class="na">isEnabled</span><span class="p">();</span>
<span class="p">});</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Alternatively, you may want to implement the whole class from scratch:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">use</span> <span class="nx">Doctrine\Common\Collections\Collection</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">SectionCollection</span> <span class="k">implements</span> <span class="nx">Collection</span>
<span class="p">{</span>
<span class="k">private</span> <span class="nv">$elements</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">__construct</span><span class="p">(</span><span class="k">array</span> <span class="nv">$elements</span> <span class="o">=</span> <span class="k">array</span><span class="p">())</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">elements</span> <span class="o">=</span> <span class="nv">$elements</span><span class="p">;</span>
<span class="p">}</span>
<span class="c1">// your implementation of all methods interface requires</span>
<span class="p">}</span>
</pre></div>
</div>
</div>
<div class="section" id="taking-control-of-the-collection-s-constructor">
<h2>Taking Control of the Collection's Constructor<a class="headerlink" href="#taking-control-of-the-collection-s-constructor" title="Permalink to this headline"></a></h2>
<p>By default, Doctrine assumes that it can instantiate your collections in same
manner as an <code class="docutils literal"><span class="pre">ArrayCollection</span></code> (i.e. the only parameter is an optional PHP
array); however, you may want to inject additional dependencies into your
custom collection class(es). This will require you to create a
<a class="reference external" href="https://github.com/doctrine/mongodb-odm/blob/master/lib/Doctrine/ODM/MongoDB/PersistentCollection/PersistentCollectionFactory.php">PersistentCollectionFactory implementation</a>,
which Doctrine will then use to construct its persistent collections.
You may decide to implement this class from scratch or extend our
<code class="docutils literal"><span class="pre">AbstractPersistentCollectionFactory</span></code>:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">use</span> <span class="nx">Doctrine\ODM\MongoDB\PersistentCollection\AbstractPersistentCollectionFactory</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Symfony\Component\EventDispatcher\EventDispatcherInterface</span><span class="p">;</span>
<span class="k">final</span> <span class="k">class</span> <span class="nc">YourPersistentCollectionFactory</span> <span class="k">extends</span> <span class="nx">AbstractPersistentCollectionFactory</span>
<span class="p">{</span>
<span class="k">private</span> <span class="nv">$eventDispatcher</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="nx">EventDispatcherInterface</span> <span class="nv">$eventDispatcher</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">eventDispatcher</span> <span class="o">=</span> <span class="nv">$eventDispatcher</span><span class="p">;</span>
<span class="p">}</span>
<span class="k">protected</span> <span class="k">function</span> <span class="nf">createCollectionClass</span><span class="p">(</span><span class="nv">$collectionClass</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">switch</span> <span class="p">(</span><span class="nv">$collectionClass</span><span class="p">)</span> <span class="p">{</span>
<span class="k">case</span> <span class="nx">SectionCollection</span><span class="o">::</span><span class="na">class</span><span class="o">:</span>
<span class="k">return</span> <span class="k">new</span> <span class="nv">$collectionClass</span><span class="p">(</span><span class="k">array</span><span class="p">(),</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">eventDispatcher</span><span class="p">);</span>
<span class="k">default</span><span class="o">:</span>
<span class="k">return</span> <span class="k">new</span> <span class="nv">$collectionClass</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The factory class must then be registered in the <code class="docutils literal"><span class="pre">Configuration</span></code>:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$eventDispatcher</span> <span class="o">=</span> <span class="nv">$container</span><span class="o">-&gt;</span><span class="na">get</span><span class="p">(</span><span class="s1">&#39;event_dispatcher&#39;</span><span class="p">);</span>
<span class="nv">$collFactory</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">YourPersistentCollectionFactory</span><span class="p">(</span><span class="nv">$eventDispatcher</span><span class="p">);</span>
<span class="nv">$configuration</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Configuration</span><span class="p">();</span>
<span class="c1">// your other config here</span>
<span class="nv">$configuration</span><span class="o">-&gt;</span><span class="na">setPersistentCollectionFactory</span><span class="p">(</span><span class="nv">$collFactory</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="#">Custom Collections</a><ul>
<li><a class="reference internal" href="#custom-collection-classes">Custom Collection Classes</a></li>
<li><a class="reference internal" href="#taking-control-of-the-collection-s-constructor">Taking Control of the Collection's Constructor</a></li>
</ul>
</li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/reference/custom-collections.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy; 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>