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

389 lines
22 KiB
HTML
Raw 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>Trees &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="trees">
<h1>Trees<a class="headerlink" href="#trees" title="Permalink to this headline"></a></h1>
<p>MongoDB lends itself quite well to storing hierarchical data. This
chapter will demonstrate some examples!</p>
<div class="section" id="full-tree-in-single-document">
<h2>Full Tree in Single Document<a class="headerlink" href="#full-tree-in-single-document" title="Permalink to this headline"></a></h2>
<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="sd">/** @Id */</span>
<span class="k">private</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$title</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$body</span><span class="p">;</span>
<span class="sd">/** @EmbedMany(targetDocument=&quot;Comment&quot;) */</span>
<span class="k">private</span> <span class="nv">$comments</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span>
<span class="c1">// ...</span>
<span class="p">}</span>
<span class="sd">/** @EmbeddedDocument */</span>
<span class="k">class</span> <span class="nc">Comment</span>
<span class="p">{</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$by</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$text</span><span class="p">;</span>
<span class="sd">/** @EmbedMany(targetDocument=&quot;Comment&quot;) */</span>
<span class="k">private</span> <span class="nv">$replies</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Retrieve a blog post and only select the first 10 comments:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$post</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">&#39;BlogPost&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">selectSlice</span><span class="p">(</span><span class="s1">&#39;replies&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">10</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">getSingleResult</span><span class="p">();</span>
<span class="nv">$replies</span> <span class="o">=</span> <span class="nv">$post</span><span class="o">-&gt;</span><span class="na">getReplies</span><span class="p">();</span>
</pre></div>
</div>
<p>You can read more about this pattern on the MongoDB documentation page &quot;Trees in MongoDB&quot; in the
<a class="reference external" href="http://www.mongodb.org/display/DOCS/Trees+in+MongoDB#TreesinMongoDB-FullTreeinSingleDocument">Full Tree in Single Document</a> section.</p>
</div>
<div class="section" id="parent-reference">
<h2>Parent Reference<a class="headerlink" href="#parent-reference" title="Permalink to this headline"></a></h2>
<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">Category</span>
<span class="p">{</span>
<span class="sd">/** @Id */</span>
<span class="k">private</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$name</span><span class="p">;</span>
<span class="sd">/**</span>
<span class="sd"> * @ReferenceOne(targetDocument=&quot;Category&quot;)</span>
<span class="sd"> * @Index</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$parent</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Query for children by a specific parent id:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$children</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">&#39;Category&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">field</span><span class="p">(</span><span class="s1">&#39;parent.id&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">equals</span><span class="p">(</span><span class="s1">&#39;theid&#39;</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>
</pre></div>
</div>
<p>You can read more about this pattern on the MongoDB documentation page &quot;Trees in MongoDB&quot; in the
<a class="reference external" href="https://docs.mongodb.com/manual/tutorial/model-tree-structures/#model-tree-structures-with-parent-references">Parent Links</a> section.</p>
</div>
<div class="section" id="child-reference">
<h2>Child Reference<a class="headerlink" href="#child-reference" title="Permalink to this headline"></a></h2>
<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">Category</span>
<span class="p">{</span>
<span class="sd">/** @Id */</span>
<span class="k">private</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$name</span><span class="p">;</span>
<span class="sd">/**</span>
<span class="sd"> * @ReferenceMany(targetDocument=&quot;Category&quot;)</span>
<span class="sd"> * @Index</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$children</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Query for immediate children of a category:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$category</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">&#39;Category&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">field</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">equals</span><span class="p">(</span><span class="s1">&#39;theid&#39;</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">getSingleResult</span><span class="p">();</span>
<span class="nv">$children</span> <span class="o">=</span> <span class="nv">$category</span><span class="o">-&gt;</span><span class="na">getChildren</span><span class="p">();</span>
</pre></div>
</div>
<p>Query for immediate parent of a category:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$parent</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">&#39;Category&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">field</span><span class="p">(</span><span class="s1">&#39;children.id&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">equals</span><span class="p">(</span><span class="s1">&#39;theid&#39;</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">getSingleResult</span><span class="p">();</span>
</pre></div>
</div>
<p>You can read more about this pattern on the MongoDB documentation page &quot;Trees in MongoDB&quot; in the
<a class="reference external" href="https://docs.mongodb.com/manual/tutorial/model-tree-structures/#model-tree-structures-with-child-references">Child Links</a> section.</p>
</div>
<div class="section" id="array-of-ancestors">
<h2>Array of Ancestors<a class="headerlink" href="#array-of-ancestors" title="Permalink to this headline"></a></h2>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/** @MappedSuperclass */</span>
<span class="k">class</span> <span class="nc">BaseCategory</span>
<span class="p">{</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$name</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
<span class="sd">/** @Document */</span>
<span class="k">class</span> <span class="nc">Category</span> <span class="k">extends</span> <span class="nx">BaseCategory</span>
<span class="p">{</span>
<span class="sd">/** @Id */</span>
<span class="k">private</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/**</span>
<span class="sd"> * @ReferenceMany(targetDocument=&quot;Category&quot;)</span>
<span class="sd"> * @Index</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$ancestors</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span>
<span class="sd">/**</span>
<span class="sd"> * @ReferenceOne(targetDocument=&quot;Category&quot;)</span>
<span class="sd"> * @Index</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$parent</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
<span class="sd">/** @EmbeddedDocument */</span>
<span class="k">class</span> <span class="nc">SubCategory</span> <span class="k">extends</span> <span class="nx">BaseCategory</span>
<span class="p">{</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Query for all descendants of a category:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$categories</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">createQueryBuilder</span><span class="p">(</span><span class="s1">&#39;Category&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">field</span><span class="p">(</span><span class="s1">&#39;ancestors.id&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">equals</span><span class="p">(</span><span class="s1">&#39;theid&#39;</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>
</pre></div>
</div>
<p>Query for all ancestors of a category:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$category</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">createQuery</span><span class="p">(</span><span class="s1">&#39;Category&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">field</span><span class="p">(</span><span class="s1">&#39;id&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">equals</span><span class="p">(</span><span class="s1">&#39;theid&#39;</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">getSingleResult</span><span class="p">();</span>
<span class="nv">$ancestors</span> <span class="o">=</span> <span class="nv">$category</span><span class="o">-&gt;</span><span class="na">getAncestors</span><span class="p">();</span>
</pre></div>
</div>
<p>You can read more about this pattern on the MongoDB documentation page &quot;Trees in MongoDB&quot; in the
<a class="reference external" href="https://docs.mongodb.com/manual/tutorial/model-tree-structures/#model-tree-structures-with-an-array-of-ancestors">Array of Ancestors</a> section.</p>
</div>
<div class="section" id="materialized-paths">
<h2>Materialized Paths<a class="headerlink" href="#materialized-paths" title="Permalink to this headline"></a></h2>
<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">Category</span>
<span class="p">{</span>
<span class="sd">/** @Id */</span>
<span class="k">private</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$name</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$path</span><span class="p">;</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Query for the entire tree:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$categories</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">createQuery</span><span class="p">(</span><span class="s1">&#39;Category&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">sort</span><span class="p">(</span><span class="s1">&#39;path&#39;</span><span class="p">,</span> <span class="s1">&#39;asc&#39;</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>
</pre></div>
</div>
<p>Query for the node 'b' and all its descendants:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$categories</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">createQuery</span><span class="p">(</span><span class="s1">&#39;Category&#39;</span><span class="p">)</span>
<span class="o">-&gt;</span><span class="na">field</span><span class="p">(</span><span class="s1">&#39;path&#39;</span><span class="p">)</span><span class="o">-&gt;</span><span class="na">equals</span><span class="p">(</span><span class="s1">&#39;/^a,b,/&#39;</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>
</pre></div>
</div>
<p>You can read more about this pattern on the MongoDB documentation page &quot;Trees in MongoDB&quot; in the
<a class="reference external" href="https://docs.mongodb.com/manual/tutorial/model-tree-structures/#model-tree-structures-with-materialized-paths">Materialized Paths (Full Path in Each Node)</a> section.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<div id="searchbox" style="">
<h3>Search</h3>
<form class="search" action="http://readthedocs.org/search/project/" method="get">
<input type="text" name="q" size="18">
<input type="submit" value="Go">
<input type="hidden" name="selected_facets" value="project:">
</form>
</div>
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Trees</a><ul>
<li><a class="reference internal" href="#full-tree-in-single-document">Full Tree in Single Document</a></li>
<li><a class="reference internal" href="#parent-reference">Parent Reference</a></li>
<li><a class="reference internal" href="#child-reference">Child Reference</a></li>
<li><a class="reference internal" href="#array-of-ancestors">Array of Ancestors</a></li>
<li><a class="reference internal" href="#materialized-paths">Materialized Paths</a></li>
</ul>
</li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/reference/trees.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>