348 lines
20 KiB
HTML
348 lines
20 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>Bi-Directional References — 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="bi-directional-references">
|
|
<h1>Bi-Directional References<a class="headerlink" href="#bi-directional-references" title="Permalink to this headline">¶</a></h1>
|
|
<p>By default when you map a bi-directional reference, the reference is maintained on both sides
|
|
of the relationship and there is not a single "owning side". Both sides are considered owning
|
|
and changes are tracked and persisted separately. Here is an example:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?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">/** @ReferenceOne(targetDocument="User") */</span>
|
|
<span class="k">private</span> <span class="nv">$user</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="sd">/** @Document */</span>
|
|
<span class="k">class</span> <span class="nc">User</span>
|
|
<span class="p">{</span>
|
|
<span class="c1">// ...</span>
|
|
|
|
<span class="sd">/** @ReferenceMany(targetDocument="BlogPost") */</span>
|
|
<span class="k">private</span> <span class="nv">$posts</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>When I persist some instances of the above classes the references would exist on both sides! The
|
|
<code class="docutils literal"><span class="pre">BlogPost</span></code> collection would have a <a class="reference external" href="https://docs.mongodb.com/manual/reference/database-references/#dbrefs">DBRef</a> stored on the <code class="docutils literal"><span class="pre">$user</span></code> property and the <code class="docutils literal"><span class="pre">User</span></code>
|
|
collection would have a <a class="reference external" href="https://docs.mongodb.com/manual/reference/database-references/#dbrefs">DBRef</a> stored in the <code class="docutils literal"><span class="pre">$posts</span></code> property.</p>
|
|
<div class="section" id="owning-and-inverse-sides">
|
|
<h2>Owning and Inverse Sides<a class="headerlink" href="#owning-and-inverse-sides" title="Permalink to this headline">¶</a></h2>
|
|
<p>A user may have lots of posts and we don't need to store a reference to each post on the user, we
|
|
can get the users post by running a query like the following:</p>
|
|
<div class="highlight-javascript"><div class="highlight"><pre><span class="nx">db</span><span class="p">.</span><span class="nx">BlogPost</span><span class="p">.</span><span class="nx">find</span><span class="p">({</span> <span class="s1">'user.$id'</span> <span class="o">:</span> <span class="nx">user</span><span class="p">.</span><span class="nx">id</span> <span class="p">})</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>In order to map this you can use the <code class="docutils literal"><span class="pre">inversedBy</span></code> and <code class="docutils literal"><span class="pre">mappedBy</span></code> options. Here is the same
|
|
example above where we implement this:</p>
|
|
<div class="section" id="one-to-many">
|
|
<h3>One to Many<a class="headerlink" href="#one-to-many" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?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">/** @ReferenceOne(targetDocument="User", inversedBy="posts") */</span>
|
|
<span class="k">private</span> <span class="nv">$user</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="sd">/** @Document */</span>
|
|
<span class="k">class</span> <span class="nc">User</span>
|
|
<span class="p">{</span>
|
|
<span class="c1">// ...</span>
|
|
|
|
<span class="sd">/** @ReferenceMany(targetDocument="BlogPost", mappedBy="user") */</span>
|
|
<span class="k">private</span> <span class="nv">$posts</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>So now when we persist a <code class="docutils literal"><span class="pre">User</span></code> and multiple <code class="docutils literal"><span class="pre">BlogPost</span></code> instances for that <code class="docutils literal"><span class="pre">User</span></code>:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$user</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">User</span><span class="p">();</span>
|
|
|
|
<span class="nv">$post1</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">BlogPost</span><span class="p">();</span>
|
|
<span class="nv">$post1</span><span class="o">-></span><span class="na">setUser</span><span class="p">(</span><span class="nv">$user</span><span class="p">);</span>
|
|
|
|
<span class="nv">$post2</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">BlogPost</span><span class="p">();</span>
|
|
<span class="nv">$post2</span><span class="o">-></span><span class="na">setUser</span><span class="p">(</span><span class="nv">$user</span><span class="p">);</span>
|
|
|
|
<span class="nv">$post3</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">BlogPost</span><span class="p">();</span>
|
|
<span class="nv">$post3</span><span class="o">-></span><span class="na">setUser</span><span class="p">(</span><span class="nv">$user</span><span class="p">);</span>
|
|
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">persist</span><span class="p">(</span><span class="nv">$post1</span><span class="p">);</span>
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">persist</span><span class="p">(</span><span class="nv">$post2</span><span class="p">);</span>
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">persist</span><span class="p">(</span><span class="nv">$post3</span><span class="p">);</span>
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">flush</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>And we retrieve the <code class="docutils literal"><span class="pre">User</span></code> later to access the posts for that user:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$user</span> <span class="o">=</span> <span class="nv">$dm</span><span class="o">-></span><span class="na">find</span><span class="p">(</span><span class="s1">'User'</span><span class="p">,</span> <span class="nv">$user</span><span class="o">-></span><span class="na">id</span><span class="p">);</span>
|
|
|
|
<span class="nv">$posts</span> <span class="o">=</span> <span class="nv">$user</span><span class="o">-></span><span class="na">getPosts</span><span class="p">();</span>
|
|
<span class="k">foreach</span> <span class="p">(</span><span class="nv">$posts</span> <span class="k">as</span> <span class="nv">$post</span><span class="p">)</span> <span class="p">{</span>
|
|
<span class="c1">// ...</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The above will execute a query like the following to lazily load the collection of posts to
|
|
iterate over:</p>
|
|
<div class="highlight-javascript"><div class="highlight"><pre><span class="nx">db</span><span class="p">.</span><span class="nx">BlogPost</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span> <span class="p">{</span> <span class="s1">'user.$id'</span> <span class="o">:</span> <span class="nx">user</span><span class="p">.</span><span class="nx">id</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">Remember that the inverse side, the side which specified <code class="docutils literal"><span class="pre">mappedBy</span></code> is immutable and
|
|
any changes to the state of the reference will not be persisted.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="other-examples">
|
|
<h2>Other Examples<a class="headerlink" href="#other-examples" title="Permalink to this headline">¶</a></h2>
|
|
<p>Here are several examples which implement the <code class="docutils literal"><span class="pre">inversedBy</span></code> and <code class="docutils literal"><span class="pre">mappedBy</span></code> options:</p>
|
|
<div class="section" id="one-to-one">
|
|
<h3>One to One<a class="headerlink" href="#one-to-one" title="Permalink to this headline">¶</a></h3>
|
|
<p>Here is an example where we have a one to one relationship between <code class="docutils literal"><span class="pre">Cart</span></code> and <code class="docutils literal"><span class="pre">Customer</span></code>:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="sd">/** @Document */</span>
|
|
<span class="k">class</span> <span class="nc">Cart</span>
|
|
<span class="p">{</span>
|
|
<span class="c1">// ...</span>
|
|
|
|
<span class="sd">/**</span>
|
|
<span class="sd"> * @ReferenceOne(targetDocument="Customer", inversedBy="cart")</span>
|
|
<span class="sd"> */</span>
|
|
<span class="k">public</span> <span class="nv">$customer</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="sd">/** @Document */</span>
|
|
<span class="k">class</span> <span class="nc">Customer</span>
|
|
<span class="p">{</span>
|
|
<span class="c1">// ...</span>
|
|
|
|
<span class="sd">/**</span>
|
|
<span class="sd"> * @ReferenceOne(targetDocument="Cart", mappedBy="customer")</span>
|
|
<span class="sd"> */</span>
|
|
<span class="k">public</span> <span class="nv">$cart</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The owning side is on <code class="docutils literal"><span class="pre">Cart.customer</span></code> and the <code class="docutils literal"><span class="pre">Customer.cart</span></code> referenced is loaded with a query
|
|
like this:</p>
|
|
<div class="highlight-javascript"><div class="highlight"><pre><span class="nx">db</span><span class="p">.</span><span class="nx">Cart</span><span class="p">.</span><span class="nx">find</span><span class="p">(</span> <span class="p">{</span> <span class="s1">'customer.$id'</span> <span class="o">:</span> <span class="nx">customer</span><span class="p">.</span><span class="nx">id</span> <span class="p">}</span> <span class="p">)</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you want to nullify the relationship between a <code class="docutils literal"><span class="pre">Cart</span></code> instance and <code class="docutils literal"><span class="pre">Customer</span></code> instance
|
|
you must null it out on the <code class="docutils literal"><span class="pre">Cart.customer</span></code> side:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$cart</span><span class="o">-></span><span class="na">setCustomer</span><span class="p">(</span><span class="k">null</span><span class="p">);</span>
|
|
<span class="nv">$dm</span><span class="o">-></span><span class="na">flush</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<div class="admonition note">
|
|
<p class="first admonition-title">Note</p>
|
|
<p class="last">When specifying inverse one-to-one relationships the referenced document is
|
|
loaded directly when the owning document is hydrated instead of using a
|
|
proxy. In the example above, loading a <code class="docutils literal"><span class="pre">Customer</span></code> object from the database
|
|
would also cause the corresponding <code class="docutils literal"><span class="pre">Cart</span></code> to be loaded. This can cause
|
|
performance issues when loading many <code class="docutils literal"><span class="pre">Customer</span></code> objects at once.</p>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="self-referencing-many-to-many">
|
|
<h3>Self-Referencing Many to Many<a class="headerlink" href="#self-referencing-many-to-many" title="Permalink to this headline">¶</a></h3>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">namespace</span> <span class="nx">Documents</span><span class="p">;</span>
|
|
|
|
<span class="sd">/** @Document */</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"> * @ReferenceMany(targetDocument="User", mappedBy="myFriends")</span>
|
|
<span class="sd"> */</span>
|
|
<span class="k">public</span> <span class="nv">$friendsWithMe</span><span class="p">;</span>
|
|
|
|
<span class="sd">/**</span>
|
|
<span class="sd"> * @ReferenceMany(targetDocument="User", inversedBy="friendsWithMe")</span>
|
|
<span class="sd"> */</span>
|
|
<span class="k">public</span> <span class="nv">$myFriends</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">$name</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">name</span> <span class="o">=</span> <span class="nv">$name</span><span class="p">;</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">friendsWithMe</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Doctrine\Common\Collections\ArrayCollection</span><span class="p">();</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">myFriends</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Doctrine\Common\Collections\ArrayCollection</span><span class="p">();</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">addFriend</span><span class="p">(</span><span class="nx">User</span> <span class="nv">$user</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$user</span><span class="o">-></span><span class="na">friendsWithMe</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$this</span><span class="p">;</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">myFriends</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$user</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</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="#">Bi-Directional References</a><ul>
|
|
<li><a class="reference internal" href="#owning-and-inverse-sides">Owning and Inverse Sides</a><ul>
|
|
<li><a class="reference internal" href="#one-to-many">One to Many</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a class="reference internal" href="#other-examples">Other Examples</a><ul>
|
|
<li><a class="reference internal" href="#one-to-one">One to One</a></li>
|
|
<li><a class="reference internal" href="#self-referencing-many-to-many">Self-Referencing Many to Many</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../_sources/reference/bidirectional-references.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> |