mongodb-odm-docs-dash/build.docset/Contents/Resources/Documents/reference/complex-references.html

283 lines
15 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>Complex References &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="complex-references">
<h1>Complex References<a class="headerlink" href="#complex-references" title="Permalink to this headline"></a></h1>
<p>Sometimes you may want to access related documents using custom criteria or from
the inverse side of a relationship.</p>
<p>You can create an <a class="reference external" href="http://en.wikipedia.org/wiki/Immutable">immutable</a> reference to one or many documents and specify
how that reference is to be loaded. The reference is immutable in that it is
defined only in the mapping, unlike a typical reference where a <a class="reference external" href="http://php.net/manual/en/class.mongodbref.php">MongoDBRef</a> or
identifier (see <a class="reference internal" href="reference-mapping.html#storing-references"><span class="std std-ref">Storing References</span></a>) is stored on the document itself.</p>
<p>The following options may be used for <a class="reference internal" href="reference-mapping.html#reference-one"><span class="std std-ref">one</span></a> and
<a class="reference internal" href="reference-mapping.html#reference-many"><span class="std std-ref">many</span></a> reference mappings:</p>
<blockquote>
<div><ul class="simple">
<li><code class="docutils literal"><span class="pre">criteria</span></code> - Query criteria to apply to the cursor.</li>
<li><code class="docutils literal"><span class="pre">repositoryMethod</span></code> - The repository method used to create the cursor.</li>
<li><code class="docutils literal"><span class="pre">sort</span></code> - Sort criteria for the cursor.</li>
<li><code class="docutils literal"><span class="pre">skip</span></code> - Skip offset to apply to the cursor.</li>
<li><code class="docutils literal"><span class="pre">limit</span></code> - Limit to apply to the cursor.</li>
</ul>
</div></blockquote>
<div class="section" id="basic-example">
<h2>Basic Example<a class="headerlink" href="#basic-example" title="Permalink to this headline"></a></h2>
<p>In the following example, <code class="docutils literal"><span class="pre">$comments</span></code> will refer to all Comments for the
BlogPost and <code class="docutils literal"><span class="pre">$last5Comments</span></code> will refer to only the last five Comments. The
<code class="docutils literal"><span class="pre">mappedBy</span></code> field is used to determine which Comment field should be used for
querying by the BlogPost's ID.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/** @Document */</span>
<span class="k">class</span> <span class="nc">BlogPost</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="sd">/** @ReferenceMany(targetDocument=&quot;Comment&quot;, mappedBy=&quot;blogPost&quot;) */</span>
<span class="k">private</span> <span class="nv">$comments</span><span class="p">;</span>
<span class="sd">/**</span>
<span class="sd"> * @ReferenceMany(</span>
<span class="sd"> * targetDocument=&quot;Comment&quot;,</span>
<span class="sd"> * mappedBy=&quot;blogPost&quot;,</span>
<span class="sd"> * sort={&quot;date&quot;=&quot;desc&quot;},</span>
<span class="sd"> * limit=5</span>
<span class="sd"> * )</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$last5Comments</span><span class="p">;</span>
<span class="p">}</span>
<span class="sd">/** @Document */</span>
<span class="k">class</span> <span class="nc">Comment</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="sd">/** @ReferenceOne(targetDocument=&quot;BlogPost&quot;, inversedBy=&quot;comments&quot;) */</span>
<span class="k">private</span> <span class="nv">$blogPost</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>You can also use <code class="docutils literal"><span class="pre">mappedBy</span></code> for referencing a single document, as in the
following example:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/**</span>
<span class="sd"> * @ReferenceOne(</span>
<span class="sd"> * targetDocument=&quot;Comment&quot;,</span>
<span class="sd"> * mappedBy=&quot;blogPost&quot;,</span>
<span class="sd"> * sort={&quot;date&quot;=&quot;desc&quot;}</span>
<span class="sd"> * )</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$lastComment</span><span class="p">;</span>
</pre></div>
</div>
</div>
<div class="section" id="criteria-example">
<h2><code class="docutils literal"><span class="pre">criteria</span></code> Example<a class="headerlink" href="#criteria-example" title="Permalink to this headline"></a></h2>
<p>Use <code class="docutils literal"><span class="pre">criteria</span></code> to further match referenced documents. In the following
example, <code class="docutils literal"><span class="pre">$commentsByAdmin</span></code> will refer only comments created by
administrators:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/**</span>
<span class="sd"> * @ReferenceMany(</span>
<span class="sd"> * targetDocument=&quot;Comment&quot;,</span>
<span class="sd"> * mappedBy=&quot;blogPost&quot;,</span>
<span class="sd"> * criteria={&quot;isByAdmin&quot; : true}</span>
<span class="sd"> * )</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$commentsByAdmin</span><span class="p">;</span>
</pre></div>
</div>
</div>
<div class="section" id="repositorymethod-example">
<h2><code class="docutils literal"><span class="pre">repositoryMethod</span></code> Example<a class="headerlink" href="#repositorymethod-example" title="Permalink to this headline"></a></h2>
<p>Alternatively, you can use <code class="docutils literal"><span class="pre">repositoryMethod</span></code> to specify a custom method to
call on the Comment repository class to populate the reference.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/**</span>
<span class="sd"> * @ReferenceMany(</span>
<span class="sd"> * targetDocument=&quot;Comment&quot;,</span>
<span class="sd"> * mappedBy=&quot;blogPost&quot;,</span>
<span class="sd"> * repositoryMethod=&quot;findSomeComments&quot;</span>
<span class="sd"> * )</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$someComments</span><span class="p">;</span>
</pre></div>
</div>
<p>The <code class="docutils literal"><span class="pre">Comment</span></code> class will need to have a custom repository class configured:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/** @Document(repositoryClass=&quot;CommentRepository&quot;) */</span>
<span class="k">class</span> <span class="nc">Comment</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Lastly, the <code class="docutils literal"><span class="pre">CommentRepository</span></code> class will need a <code class="docutils literal"><span class="pre">findSomeComments()</span></code>
method which shall return <code class="docutils literal"><span class="pre">Doctrine\MongoDB\CursorInterface</span></code>. When this method
is called to populate the reference, Doctrine will provide the Blogpost instance
(i.e. owning document) as the first argument:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">class</span> <span class="nc">CommentRepository</span> <span class="k">extends</span> <span class="nx">\Doctrine\ODM\MongoDB\DocumentRepository</span>
<span class="p">{</span>
<span class="sd">/**</span>
<span class="sd"> * @return \Doctrine\ODM\MongoDB\Cursor</span>
<span class="sd"> */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">findSomeComments</span><span class="p">(</span><span class="nx">BlogPost</span> <span class="nv">$blogPost</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">createQueryBuilder</span><span class="p">()</span>
<span class="o">-&gt;</span><span class="na">field</span><span class="p">(</span><span class="s1">&#39;blogPost&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">references</span><span class="p">(</span><span class="nv">$blogPost</span><span class="p">);</span>
<span class="o">-&gt;</span><span class="na">getQuery</span><span class="p">()</span><span class="o">-&gt;</span><span class="na">execute</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="#">Complex References</a><ul>
<li><a class="reference internal" href="#basic-example">Basic Example</a></li>
<li><a class="reference internal" href="#criteria-example"><code class="docutils literal"><span class="pre">criteria</span></code> Example</a></li>
<li><a class="reference internal" href="#repositorymethod-example"><code class="docutils literal"><span class="pre">repositoryMethod</span></code> Example</a></li>
</ul>
</li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/reference/complex-references.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>