mongodb-odm-docs-dash/build.docset/Contents/Resources/Documents/reference/document-repositories.html

337 lines
23 KiB
HTML
Raw Permalink Normal View History

2017-12-02 03:35:11 +00:00
<!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>Document Repositories &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="document-repositories">
<span id="id1"></span><h1>Document Repositories<a class="headerlink" href="#document-repositories" title="Permalink to this headline"></a></h1>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">A repository mediates between the domain and data mapping layers using a
collection-like interface for accessing domain objects.</p>
</div>
<p>In Doctrine, a repository is a class that concentrates code responsible for
querying and filtering your documents. ODM provides you with a default
<code class="docutils literal"><span class="pre">DocumentRepository</span></code> for all of your documents:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="cm">/* @var $repository \Doctrine\ODM\MongoDB\DocumentRepository */</span>
<span class="nv">$repository</span> <span class="o">=</span> <span class="nv">$documentManager</span><span class="o">-&gt;</span><span class="na">getRepository</span><span class="p">(</span><span class="nx">User</span><span class="o">::</span><span class="na">class</span><span class="p">);</span>
<span class="nv">$disabledUsers</span> <span class="o">=</span> <span class="nv">$repository</span><span class="o">-&gt;</span><span class="na">findBy</span><span class="p">([</span><span class="s1">&#39;disabled&#39;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span> <span class="s1">&#39;activated&#39;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">]);</span>
</pre></div>
</div>
<p>The array passed to <code class="docutils literal"><span class="pre">findBy</span></code> specifies the criteria for which documents are matched.
ODM will assist with converting PHP values to equivalent BSON types whenever possible:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$group</span> <span class="o">=</span> <span class="nv">$documentManager</span><span class="o">-&gt;</span><span class="na">find</span><span class="p">(</span><span class="nx">Group</span><span class="o">::</span><span class="na">class</span><span class="p">,</span> <span class="mi">123</span><span class="p">);</span>
<span class="cm">/* @var $repository \Doctrine\ODM\MongoDB\DocumentRepository */</span>
<span class="nv">$repository</span> <span class="o">=</span> <span class="nv">$documentManager</span><span class="o">-&gt;</span><span class="na">getRepository</span><span class="p">(</span><span class="nx">User</span><span class="o">::</span><span class="na">class</span><span class="p">);</span>
<span class="nv">$usersInGroup</span> <span class="o">=</span> <span class="nv">$repository</span><span class="o">-&gt;</span><span class="na">findBy</span><span class="p">([</span><span class="s1">&#39;group&#39;</span> <span class="o">=&gt;</span> <span class="nv">$group</span><span class="p">]);</span>
</pre></div>
</div>
<p>The default repository implementation provides the following methods:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">find()</span></code> - finds one document by its identifier. This may skip a database query</li>
</ul>
<p>if the document is already managed by ODM.
- <code class="docutils literal"><span class="pre">findAll()</span></code> - finds all documents in the collection.
- <code class="docutils literal"><span class="pre">findBy()</span></code> - finds all documents matching the given criteria. Additional query
options may be specified (e.g. sort, limit, skip).
- <code class="docutils literal"><span class="pre">findOneBy()</span></code> - finds one document matching the given criteria.
- <code class="docutils literal"><span class="pre">matching()</span></code> - Finds all documents matching the given criteria, as expressed
with Doctrine's Criteria API.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">All above methods will include additional criteria specified by <a class="reference internal" href="filters.html#filters"><span class="std std-ref">Filters</span></a>.</p>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Magic <code class="docutils literal"><span class="pre">findBy</span></code> and <code class="docutils literal"><span class="pre">findOneBy</span></code> calls described below are deprecated in 1.2 and
will be removed in 2.0.</p>
</div>
<p>Additional methods that are not defined explicitly in the repository class may also be
used if they follow a specific naming convention:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$group</span> <span class="o">=</span> <span class="nv">$documentManager</span><span class="o">-&gt;</span><span class="na">find</span><span class="p">(</span><span class="nx">Group</span><span class="o">::</span><span class="na">class</span><span class="p">,</span> <span class="mi">123</span><span class="p">);</span>
<span class="cm">/* @var $repository \Doctrine\ODM\MongoDB\DocumentRepository */</span>
<span class="nv">$repository</span> <span class="o">=</span> <span class="nv">$documentManager</span><span class="o">-&gt;</span><span class="na">getRepository</span><span class="p">(</span><span class="nx">User</span><span class="o">::</span><span class="na">class</span><span class="p">);</span>
<span class="nv">$usersInGroup</span> <span class="o">=</span> <span class="nv">$repository</span><span class="o">-&gt;</span><span class="na">findByGroup</span><span class="p">(</span><span class="nv">$group</span><span class="p">);</span>
<span class="nv">$randomUser</span> <span class="o">=</span> <span class="nv">$repository</span><span class="o">-&gt;</span><span class="na">findOneByStatus</span><span class="p">(</span><span class="s1">&#39;active&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>In the above example, <code class="docutils literal"><span class="pre">findByGroup()</span></code> and <code class="docutils literal"><span class="pre">findOneByStatus()</span></code> will be handled by
the <code class="docutils literal"><span class="pre">__call</span></code> method, which intercepts calls to undefined methods. If the invoked
method's name starts with &quot;findBy&quot; or &quot;findOneBy&quot;, ODM will attempt to infer mapped
properties from the remainder of the method name (&quot;Group&quot; or &quot;Status&quot; as per example).
The above calls are equivalent to:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$group</span> <span class="o">=</span> <span class="nv">$documentManager</span><span class="o">-&gt;</span><span class="na">find</span><span class="p">(</span><span class="nx">Group</span><span class="o">::</span><span class="na">class</span><span class="p">,</span> <span class="mi">123</span><span class="p">);</span>
<span class="cm">/* @var $repository \Doctrine\ODM\MongoDB\DocumentRepository */</span>
<span class="nv">$repository</span> <span class="o">=</span> <span class="nv">$documentManager</span><span class="o">-&gt;</span><span class="na">getRepository</span><span class="p">(</span><span class="nx">User</span><span class="o">::</span><span class="na">class</span><span class="p">);</span>
<span class="nv">$usersInGroup</span> <span class="o">=</span> <span class="nv">$repository</span><span class="o">-&gt;</span><span class="na">findBy</span><span class="p">([</span><span class="s1">&#39;group&#39;</span> <span class="o">=&gt;</span> <span class="nv">$group</span><span class="p">]);</span>
<span class="nv">$randomUser</span> <span class="o">=</span> <span class="nv">$repository</span><span class="o">-&gt;</span><span class="na">findOneBy</span><span class="p">([</span><span class="s1">&#39;status&#39;</span> <span class="o">=&gt;</span> <span class="s1">&#39;active&#39;</span><span class="p">]);</span>
</pre></div>
</div>
<div class="section" id="custom-repositories">
<h2>Custom Repositories<a class="headerlink" href="#custom-repositories" title="Permalink to this headline"></a></h2>
<p>A custom repository allows filtering logic to be consolidated into a single class instead
of spreading it throughout a project. A custom repository class may be specified for a
document class like so:</p>
<div class="configuration-block">
<ul class="simple">
<li><em>PHP</em><div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">namespace</span> <span class="nx">Documents</span><span class="p">;</span>
<span class="sd">/** @Document(repositoryClass=&quot;Repositories\UserRepository&quot;) */</span>
<span class="k">class</span> <span class="nc">User</span>
<span class="p">{</span>
<span class="cm">/* ... */</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
<li><em>XML</em><div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;document</span> <span class="na">name=</span><span class="s">&quot;Documents\User&quot;</span> <span class="na">repository-class=</span><span class="s">&quot;Repositories\UserRepository&quot;</span><span class="nt">&gt;</span>
<span class="c">&lt;!-- ... --&gt;</span>
<span class="nt">&lt;/document&gt;</span>
</pre></div>
</div>
</li>
<li><em>YAML</em><div class="highlight-yaml"><div class="highlight"><pre><span class="l l-Scalar l-Scalar-Plain">Documents\User</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">repositoryClass</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">Repositories\\UserRepository</span>
<span class="l l-Scalar l-Scalar-Plain">collection</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">user</span>
<span class="c1"># ...</span>
</pre></div>
</div>
</li>
</ul>
</div>
<p>The next step is implementing your repository class. In most cases, ODM's default
<code class="docutils literal"><span class="pre">DocumentRepository</span></code> class may be extended with additional methods that you need.
More complex cases that require passing additional dependencies to a custom repository
class will be discussed in the next section.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">namespace</span> <span class="nx">Repositories</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">UserRepository</span> <span class="k">extends</span> <span class="nx">DocumentRepository</span>
<span class="p">{</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">findDisabled</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">findBy</span><span class="p">([</span><span class="s1">&#39;disabled&#39;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">,</span> <span class="s1">&#39;activated&#39;</span> <span class="o">=&gt;</span> <span class="k">true</span><span class="p">]);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>It is also possible to change ODM's default <code class="docutils literal"><span class="pre">DocumentRepository</span></code> to your own
implementation for all documents (unless overridden by the mapping):</p>
<div class="highlight-php"><div class="highlight"><pre><span class="x">$documentManager-&gt;getConfiguration()</span>
<span class="x"> -&gt;setDefaultRepositoryClassName(MyDefaultRepository::class);</span>
</pre></div>
</div>
</div>
<div class="section" id="repositories-with-additional-dependencies">
<h2>Repositories with Additional Dependencies<a class="headerlink" href="#repositories-with-additional-dependencies" title="Permalink to this headline"></a></h2>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Implementing your own RepositoryFactory is possible since version 1.0, but the
<code class="docutils literal"><span class="pre">AbstractRepositoryFactory</span></code> class used in this example is only available since 1.2.</p>
</div>
<p>By default, Doctrine assumes that it can instantiate your repositories in same manner
as its default one:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">namespace</span> <span class="nx">Repositories</span><span class="p">;</span>
<span class="k">class</span> <span class="nc">UserRepository</span> <span class="k">extends</span> <span class="nx">DocumentRepository</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">DocumentManager</span> <span class="nv">$dm</span><span class="p">,</span> <span class="nx">UnitOfWork</span> <span class="nv">$uow</span><span class="p">,</span> <span class="nx">ClassMetadata</span> <span class="nv">$classMetadata</span><span class="p">)</span>
<span class="p">{</span>
<span class="cm">/* constructor is inherited from DocumentRepository */</span>
<span class="cm">/* ... */</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>In order to change the way Doctrine instantiates repositories, you will need to implement your own
<a class="reference external" href="https://github.com/doctrine/mongodb-odm/blob/master/lib/Doctrine/ODM/MongoDB/Repository/RepositoryFactory.php">RepositoryFactory</a></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\Repository\AbstractRepositoryFactory</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">YourRepositoryFactory</span> <span class="k">extends</span> <span class="nx">AbstractRepositoryFactory</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">instantiateRepository</span><span class="p">(</span><span class="nv">$repositoryClassName</span><span class="p">,</span> <span class="nx">DocumentManager</span> <span class="nv">$documentManager</span><span class="p">,</span> <span class="nx">ClassMetadata</span> <span class="nv">$metadata</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">switch</span> <span class="p">(</span><span class="nv">$repositoryClassName</span><span class="p">)</span> <span class="p">{</span>
<span class="k">case</span> <span class="nx">UserRepository</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="nx">UserRepository</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="nv">$documentManager</span><span class="p">,</span> <span class="nv">$metadata</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">$repositoryClassName</span><span class="p">(</span><span class="nv">$documentManager</span><span class="p">,</span> <span class="nv">$documentManager</span><span class="o">-&gt;</span><span class="na">getUnitOfWork</span><span class="p">(),</span> <span class="nv">$metadata</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">$repoFactory</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">YourRepositoryFactory</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">setRepositoryFactory</span><span class="p">(</span><span class="nv">$repoFactory</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="#">Document Repositories</a><ul>
<li><a class="reference internal" href="#custom-repositories">Custom Repositories</a></li>
<li><a class="reference internal" href="#repositories-with-additional-dependencies">Repositories with Additional Dependencies</a></li>
</ul>
</li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/reference/document-repositories.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>