mongodb-odm-docs-dash/Doctrine ODM.docset/Contents/Resources/Documents/reference/embedded-mapping.html

426 lines
22 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>Embedded Mapping &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="embedded-mapping">
<h1>Embedded Mapping<a class="headerlink" href="#embedded-mapping" title="Permalink to this headline"></a></h1>
<p>This chapter explains how embedded documents are mapped in
Doctrine.</p>
<div class="section" id="embed-one">
<span id="id1"></span><h2>Embed One<a class="headerlink" href="#embed-one" title="Permalink to this headline"></a></h2>
<p>Embed a single document:</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="sd">/** @Document */</span>
<span class="k">class</span> <span class="nc">User</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="sd">/** @EmbedOne(targetDocument=&quot;Address&quot;) */</span>
<span class="k">private</span> <span class="nv">$address</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">Address</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
<li><em>XML</em><div class="highlight-xml"><div class="highlight"><pre><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="nt">&lt;doctrine-mongo-mapping</span> <span class="na">xmlns=</span><span class="s">&quot;http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping&quot;</span>
<span class="na">xmlns:xsi=</span><span class="s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
<span class="na">xsi:schemaLocation=</span><span class="s">&quot;http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping</span>
<span class="s"> http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;document</span> <span class="na">name=</span><span class="s">&quot;Documents\User&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;embed-one</span> <span class="na">field=</span><span class="s">&quot;address&quot;</span> <span class="na">target-document=</span><span class="s">&quot;Address&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/document&gt;</span>
<span class="nt">&lt;/doctrine-mongo-mapping&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">User</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">type</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">document</span>
<span class="l l-Scalar l-Scalar-Plain">embedOne</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">address</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">targetDocument</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">Address</span>
<span class="l l-Scalar l-Scalar-Plain">Address</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">type</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">embeddedDocument</span>
</pre></div>
</div>
</li>
</ul>
</div>
</div>
<div class="section" id="embed-many">
<span id="id2"></span><h2>Embed Many<a class="headerlink" href="#embed-many" title="Permalink to this headline"></a></h2>
<p>Embed many documents:</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="sd">/** @Document */</span>
<span class="k">class</span> <span class="nc">User</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="sd">/** @EmbedMany(targetDocument=&quot;Phonenumber&quot;) */</span>
<span class="k">private</span> <span class="nv">$phonenumbers</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">Phonenumber</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="p">}</span>
</pre></div>
</div>
</li>
<li><em>XML</em><div class="highlight-xml"><div class="highlight"><pre><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span>
<span class="nt">&lt;doctrine-mongo-mapping</span> <span class="na">xmlns=</span><span class="s">&quot;http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping&quot;</span>
<span class="na">xmlns:xsi=</span><span class="s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span>
<span class="na">xsi:schemaLocation=</span><span class="s">&quot;http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping</span>
<span class="s"> http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping.xsd&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;document</span> <span class="na">name=</span><span class="s">&quot;Documents\User&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;embed-many</span> <span class="na">field=</span><span class="s">&quot;phonenumbers&quot;</span> <span class="na">target-document=</span><span class="s">&quot;Phonenumber&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/document&gt;</span>
<span class="nt">&lt;/doctrine-mongo-mapping&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">User</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">type</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">document</span>
<span class="l l-Scalar l-Scalar-Plain">embedMany</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">phonenumbers</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">targetDocument</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">Phonenumber</span>
<span class="l l-Scalar l-Scalar-Plain">Phonenumber</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">type</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">embeddedDocument</span>
</pre></div>
</div>
</li>
</ul>
</div>
</div>
<div class="section" id="mixing-document-types">
<span id="embed-mixing-document-types"></span><h2>Mixing Document Types<a class="headerlink" href="#mixing-document-types" title="Permalink to this headline"></a></h2>
<p>If you want to store different types of embedded documents in the same field,
you can simply omit the <code class="docutils literal"><span class="pre">targetDocument</span></code> option:</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="sd">/** @Document */</span>
<span class="k">class</span> <span class="nc">User</span>
<span class="p">{</span>
<span class="c1">// ..</span>
<span class="sd">/** @EmbedMany */</span>
<span class="k">private</span> <span class="nv">$tasks</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>
</li>
<li><em>XML</em><div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;embed-many</span> <span class="na">field=</span><span class="s">&quot;tasks&quot;</span> <span class="nt">/&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">embedMany</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">tasks</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">~</span>
</pre></div>
</div>
</li>
</ul>
</div>
<p>Now the <code class="docutils literal"><span class="pre">$tasks</span></code> property can store any type of document! The class name will
be automatically stored in a field named <code class="docutils literal"><span class="pre">_doctrine_class_name</span></code> within
the embedded document. The field name can be customized with the
<code class="docutils literal"><span class="pre">discriminatorField</span></code> option:</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="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"> * @EmbedMany(discriminatorField=&quot;type&quot;)</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$tasks</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>
</li>
<li><em>XML</em><div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;embed-many</span> <span class="na">field=</span><span class="s">&quot;tasks&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;discriminator-field</span> <span class="na">name=</span><span class="s">&quot;type&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/embed-many&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">embedMany</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">tasks</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">discriminatorField</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">type</span>
</pre></div>
</div>
</li>
</ul>
</div>
<p>You can also specify a discriminator map to avoid storing the <a href="#id3"><span class="problematic" id="id4">|FQCN|</span></a>
in each embedded document:</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="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"> * @EmbedMany(</span>
<span class="sd"> * discriminatorMap={</span>
<span class="sd"> * &quot;download&quot;=&quot;DownloadTask&quot;,</span>
<span class="sd"> * &quot;build&quot;=&quot;BuildTask&quot;</span>
<span class="sd"> * }</span>
<span class="sd"> * )</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$tasks</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>
</li>
<li><em>XML</em><div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;embed-many</span> <span class="na">field=</span><span class="s">&quot;tasks&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;discriminator-map&gt;</span>
<span class="nt">&lt;discriminator-mapping</span> <span class="na">value=</span><span class="s">&quot;download&quot;</span> <span class="na">class=</span><span class="s">&quot;DownloadTask&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;discriminator-mapping</span> <span class="na">value=</span><span class="s">&quot;build&quot;</span> <span class="na">class=</span><span class="s">&quot;BuildTask&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/discriminator-map&gt;</span>
<span class="nt">&lt;/embed-many&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">embedMany</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">tasks</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">discriminatorMap</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">download</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">DownloadTask</span>
<span class="l l-Scalar l-Scalar-Plain">build</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">BuildTask</span>
</pre></div>
</div>
</li>
</ul>
</div>
<p>If you have embedded documents without a discriminator value that need to be
treated correctly you can optionally specify a default value for the
discriminator:</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="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"> * @EmbedMany(</span>
<span class="sd"> * discriminatorMap={</span>
<span class="sd"> * &quot;download&quot;=&quot;DownloadTask&quot;,</span>
<span class="sd"> * &quot;build&quot;=&quot;BuildTask&quot;</span>
<span class="sd"> * },</span>
<span class="sd"> * defaultDiscriminatorValue=&quot;download&quot;</span>
<span class="sd"> * )</span>
<span class="sd"> */</span>
<span class="k">private</span> <span class="nv">$tasks</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>
</li>
<li><em>XML</em><div class="highlight-xml"><div class="highlight"><pre><span class="nt">&lt;embed-many</span> <span class="na">field=</span><span class="s">&quot;tasks&quot;</span><span class="nt">&gt;</span>
<span class="nt">&lt;discriminator-map&gt;</span>
<span class="nt">&lt;discriminator-mapping</span> <span class="na">value=</span><span class="s">&quot;download&quot;</span> <span class="na">class=</span><span class="s">&quot;DownloadTask&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;discriminator-mapping</span> <span class="na">value=</span><span class="s">&quot;build&quot;</span> <span class="na">class=</span><span class="s">&quot;BuildTask&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/discriminator-map&gt;</span>
<span class="nt">&lt;default-discriminator-value</span> <span class="na">value=</span><span class="s">&quot;download&quot;</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/embed-many&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">embedMany</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">tasks</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">discriminatorMap</span><span class="p p-Indicator">:</span>
<span class="l l-Scalar l-Scalar-Plain">download</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">DownloadTask</span>
<span class="l l-Scalar l-Scalar-Plain">build</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">BuildTask</span>
<span class="l l-Scalar l-Scalar-Plain">defaultDiscriminatorValue</span><span class="p p-Indicator">:</span> <span class="l l-Scalar l-Scalar-Plain">download</span>
</pre></div>
</div>
</li>
</ul>
</div>
</div>
<div class="section" id="cascading-operations">
<h2>Cascading Operations<a class="headerlink" href="#cascading-operations" title="Permalink to this headline"></a></h2>
<p>All operations on embedded documents are automatically cascaded.
This is because embedded documents are part of their parent
document and cannot exist without those by nature.</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="#">Embedded Mapping</a><ul>
<li><a class="reference internal" href="#embed-one">Embed One</a></li>
<li><a class="reference internal" href="#embed-many">Embed Many</a></li>
<li><a class="reference internal" href="#mixing-document-types">Mixing Document Types</a></li>
<li><a class="reference internal" href="#cascading-operations">Cascading Operations</a></li>
</ul>
</li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/reference/embedded-mapping.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>