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

343 lines
20 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>Migrating Schemas &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="migrating-schemas">
<h1>Migrating Schemas<a class="headerlink" href="#migrating-schemas" title="Permalink to this headline"></a></h1>
<p>Even though MongoDB is schemaless, introducing some kind of object mapper means
that your object definitions become your schema. You may have a situation where
you rename a property in your object model but need to load values from older
documents where the field is still using the former name. While you could use
MongoDB's <a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/update/rename/">$rename</a> operator to migrate everything, sometimes a lazy migration
is preferable. Doctrine offers a few different methods for dealing with this
problem!</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The features in this chapter were inspired by <a class="reference external" href="https://github.com/objectify/objectify">Objectify</a>, an object mapper
for the Google App Engine datastore. Additional information may be found in
the <a class="reference external" href="https://github.com/objectify/objectify/wiki/SchemaMigration">Objectify schema migration</a> documentation.</p>
</div>
<div class="section" id="renaming-a-field">
<h2>Renaming a Field<a class="headerlink" href="#renaming-a-field" title="Permalink to this headline"></a></h2>
<p>Let's say you have a simple document that starts off with the following fields:</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">Person</span>
<span class="p">{</span>
<span class="sd">/** @Id */</span>
<span class="k">public</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">public</span> <span class="nv">$name</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Later on, you need rename <code class="docutils literal"><span class="pre">name</span></code> to <code class="docutils literal"><span class="pre">fullName</span></code>; however, you'd like to
hydrate <code class="docutils literal"><span class="pre">fullName</span></code> from <code class="docutils literal"><span class="pre">name</span></code> if the new field doesn't exist.</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">Person</span>
<span class="p">{</span>
<span class="sd">/** @Id */</span>
<span class="k">public</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) @AlsoLoad(&quot;name&quot;) */</span>
<span class="k">public</span> <span class="nv">$fullName</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>When a Person is loaded, the <code class="docutils literal"><span class="pre">fullName</span></code> field will be populated with the value
of <code class="docutils literal"><span class="pre">name</span></code> if <code class="docutils literal"><span class="pre">fullName</span></code> is not found. When the Person is persisted, this
value will then be stored in the <code class="docutils literal"><span class="pre">fullName</span></code> field.</p>
<div class="admonition caution">
<p class="first admonition-title">Caution</p>
<p class="last">A caveat of this feature is that it only affects hydration. Queries will not
know about the rename, so a query on <code class="docutils literal"><span class="pre">fullName</span></code> will only match documents
with the new field name. You can still query using the <code class="docutils literal"><span class="pre">name</span></code> field to
find older documents. The <a class="reference external" href="https://docs.mongodb.com/manual/reference/operator/query/or/">$or</a> query operator could be used to match both.</p>
</div>
</div>
<div class="section" id="transforming-data">
<h2>Transforming Data<a class="headerlink" href="#transforming-data" title="Permalink to this headline"></a></h2>
<p>You may have a situation where you want to migrate a Person's name to separate
<code class="docutils literal"><span class="pre">firstName</span></code> and <code class="docutils literal"><span class="pre">lastName</span></code> fields. This is also possible by specifying the
<code class="docutils literal"><span class="pre">&#64;AlsoLoad</span></code> annotation on a method, which will then be invoked immediately
before normal hydration.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/** @Document @HasLifecycleCallbacks */</span>
<span class="k">class</span> <span class="nc">Person</span>
<span class="p">{</span>
<span class="sd">/** @Id */</span>
<span class="k">public</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">public</span> <span class="nv">$firstName</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">public</span> <span class="nv">$lastName</span><span class="p">;</span>
<span class="sd">/** @AlsoLoad({&quot;name&quot;, &quot;fullName&quot;}) */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">populateFirstAndLastName</span><span class="p">(</span><span class="nv">$fullName</span><span class="p">)</span>
<span class="p">{</span>
<span class="k">list</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">firstName</span><span class="p">,</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">lastName</span><span class="p">)</span> <span class="o">=</span> <span class="nb">explode</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">,</span> <span class="nv">$fullName</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The annotation is defined with one or a list of field names. During hydration,
these fields will be checked in order and, for each field present, the annotated
method will be invoked with its value as a single argument. Since the
<code class="docutils literal"><span class="pre">firstName</span></code> and <code class="docutils literal"><span class="pre">lastName</span></code> fields are mapped, they would then be updated
when the Person was persisted back to MongoDB.</p>
<p>Unlike lifecycle callbacks, the <code class="docutils literal"><span class="pre">&#64;AlsoLoad</span></code> method annotation does not require
the <a class="reference internal" href="annotations-reference.html#haslifecyclecallbacks"><span class="std std-ref">&#64;HasLifecycleCallbacks</span></a> class annotation to be present.</p>
</div>
<div class="section" id="moving-fields">
<h2>Moving Fields<a class="headerlink" href="#moving-fields" title="Permalink to this headline"></a></h2>
<p>Migrating your schema can be a difficult task, but Doctrine provides a few
different methods for dealing with it:</p>
<ul class="simple">
<li><strong>&#64;AlsoLoad</strong> - load values from old fields or transform data through methods</li>
<li><strong>&#64;NotSaved</strong> - load values into fields without saving them again</li>
<li><strong>&#64;PostLoad</strong> - execute code after all fields have been loaded</li>
<li><strong>&#64;PrePersist</strong> - execute code before your document gets saved</li>
</ul>
<p>Imagine you have some address-related fields on a Person document:</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">Person</span>
<span class="p">{</span>
<span class="sd">/** @Id */</span>
<span class="k">public</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">public</span> <span class="nv">$name</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">public</span> <span class="nv">$street</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">public</span> <span class="nv">$city</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Later on, you may want to migrate this data into an embedded Address document:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/** @EmbeddedDocument */</span>
<span class="k">class</span> <span class="nc">Address</span>
<span class="p">{</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">public</span> <span class="nv">$street</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">public</span> <span class="nv">$city</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">$street</span><span class="p">,</span> <span class="nv">$city</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">street</span> <span class="o">=</span> <span class="nv">$street</span><span class="p">;</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">city</span> <span class="o">=</span> <span class="nv">$city</span><span class="p">;</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="sd">/** @Document @HasLifecycleCallbacks */</span>
<span class="k">class</span> <span class="nc">Person</span>
<span class="p">{</span>
<span class="sd">/** @Id */</span>
<span class="k">public</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @Field(type=&quot;string&quot;) */</span>
<span class="k">public</span> <span class="nv">$name</span><span class="p">;</span>
<span class="sd">/** @NotSaved */</span>
<span class="k">public</span> <span class="nv">$street</span><span class="p">;</span>
<span class="sd">/** @NotSaved */</span>
<span class="k">public</span> <span class="nv">$city</span><span class="p">;</span>
<span class="sd">/** @EmbedOne(targetDocument=&quot;Address&quot;) */</span>
<span class="k">public</span> <span class="nv">$address</span><span class="p">;</span>
<span class="sd">/** @PostLoad */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">postLoad</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">street</span> <span class="o">!==</span> <span class="k">null</span> <span class="o">||</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">city</span> <span class="o">!==</span> <span class="k">null</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">address</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Address</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">street</span><span class="p">,</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">city</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Person's <code class="docutils literal"><span class="pre">street</span></code> and <code class="docutils literal"><span class="pre">city</span></code> fields will be hydrated, but not saved. Once
the Person has loaded, the <code class="docutils literal"><span class="pre">postLoad()</span></code> method will be invoked and construct
a new Address object, which is mapped and will be persisted.</p>
<p>Alternatively, you could defer this migration until the Person is saved:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="sd">/** @Document @HasLifecycleCallbacks */</span>
<span class="k">class</span> <span class="nc">Person</span>
<span class="p">{</span>
<span class="c1">// ...</span>
<span class="sd">/** @PrePersist */</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">prePersist</span><span class="p">()</span>
<span class="p">{</span>
<span class="k">if</span> <span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">street</span> <span class="o">!==</span> <span class="k">null</span> <span class="o">||</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">city</span> <span class="o">!==</span> <span class="k">null</span><span class="p">)</span>
<span class="p">{</span>
<span class="nv">$this</span><span class="o">-&gt;</span><span class="na">address</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Address</span><span class="p">(</span><span class="nv">$this</span><span class="o">-&gt;</span><span class="na">street</span><span class="p">,</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">city</span><span class="p">);</span>
<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="annotations-reference.html#haslifecyclecallbacks"><span class="std std-ref">&#64;HasLifecycleCallbacks</span></a> annotation must be present on the class in
which the method is declared for the lifecycle callback to be registered.</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="#">Migrating Schemas</a><ul>
<li><a class="reference internal" href="#renaming-a-field">Renaming a Field</a></li>
<li><a class="reference internal" href="#transforming-data">Transforming Data</a></li>
<li><a class="reference internal" href="#moving-fields">Moving Fields</a></li>
</ul>
</li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/reference/migrating-schemas.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>