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

600 lines
44 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>Introduction &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="introduction">
<h1>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline"></a></h1>
<p>Doctrine MongoDB Object Document Mapper is built for PHP 5.3.0+ and
provides transparent persistence for PHP objects to the popular <a class="reference external" href="https://www.mongodb.com/">MongoDB</a> database by <a class="reference external" href="http://www.10gen.com">10gen</a>.</p>
<div class="section" id="features-overview">
<h2>Features Overview<a class="headerlink" href="#features-overview" title="Permalink to this headline"></a></h2>
<ul class="simple">
<li>Transparent persistence.</li>
<li>Map one or many embedded documents.</li>
<li>Map one or many referenced documents.</li>
<li>Create references between documents in different databases.</li>
<li>Map documents with Annotations, XML, YAML or plain old PHP code.</li>
<li>Documents can be stored on the <a class="reference external" href="http://www.php.net/MongoGridFS">MongoGridFS</a>.</li>
<li>Collection per class(concrete) and single collection inheritance supported.</li>
<li>Map your Doctrine 2 ORM Entities to the ODM and use mixed data stores.</li>
<li>Inserts are performed using <a class="reference external" href="http://us.php.net/manual/en/mongocollection.batchinsert.php">MongoCollection::batchInsert()</a></li>
<li>Updates are performed using atomic operators.</li>
</ul>
<p>Here is a quick example of some PHP object documents that demonstrates a few of the features:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">use</span> <span class="nx">Doctrine\Common\Collections\ArrayCollection</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Doctrine\ODM\MongoDB\Mapping\Annotations</span> <span class="k">as</span> <span class="nx">ODM</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">DateTime</span><span class="p">;</span>
<span class="sd">/** @ODM\MappedSuperclass */</span>
<span class="k">abstract</span> <span class="k">class</span> <span class="nc">BaseEmployee</span>
<span class="p">{</span>
<span class="sd">/** @ODM\Id */</span>
<span class="k">private</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @ODM\Field(type=&quot;int&quot;, strategy=&quot;increment&quot;) */</span>
<span class="k">private</span> <span class="nv">$changes</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>
<span class="sd">/** @ODM\Field(type=&quot;collection&quot;) */</span>
<span class="k">private</span> <span class="nv">$notes</span> <span class="o">=</span> <span class="k">array</span><span class="p">();</span>
<span class="sd">/** @ODM\Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$name</span><span class="p">;</span>
<span class="sd">/** @ODM\Field(type=&quot;int&quot;) */</span>
<span class="k">private</span> <span class="nv">$salary</span><span class="p">;</span>
<span class="sd">/** @ODM\Field(type=&quot;date&quot;) */</span>
<span class="k">private</span> <span class="nv">$started</span><span class="p">;</span>
<span class="sd">/** @ODM\Field(type=&quot;date&quot;) */</span>
<span class="k">private</span> <span class="nv">$left</span><span class="p">;</span>
<span class="sd">/** @ODM\EmbedOne(targetDocument=&quot;Address&quot;) */</span>
<span class="k">private</span> <span class="nv">$address</span><span class="p">;</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getId</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">id</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getChanges</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">changes</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">incrementChanges</span><span class="p">()</span> <span class="p">{</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">changes</span><span class="o">++</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getNotes</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">notes</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">addNote</span><span class="p">(</span><span class="nv">$note</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">notes</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$note</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getName</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">name</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setName</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">-&gt;</span><span class="na">name</span> <span class="o">=</span> <span class="nv">$name</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getSalary</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">salary</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setSalary</span><span class="p">(</span><span class="nv">$salary</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">salary</span> <span class="o">=</span> <span class="p">(</span><span class="nx">int</span><span class="p">)</span> <span class="nv">$salary</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getStarted</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">started</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setStarted</span><span class="p">(</span><span class="nx">DateTime</span> <span class="nv">$started</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">started</span> <span class="o">=</span> <span class="nv">$started</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getLeft</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">left</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setLeft</span><span class="p">(</span><span class="nx">DateTime</span> <span class="nv">$left</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">left</span> <span class="o">=</span> <span class="nv">$left</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getAddress</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">address</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setAddress</span><span class="p">(</span><span class="nx">Address</span> <span class="nv">$address</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="nv">$address</span><span class="p">;</span> <span class="p">}</span>
<span class="p">}</span>
<span class="sd">/** @ODM\Document */</span>
<span class="k">class</span> <span class="nc">Employee</span> <span class="k">extends</span> <span class="nx">BaseEmployee</span>
<span class="p">{</span>
<span class="sd">/** @ODM\ReferenceOne(targetDocument=&quot;Documents\Manager&quot;) */</span>
<span class="k">private</span> <span class="nv">$manager</span><span class="p">;</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getManager</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">manager</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setManager</span><span class="p">(</span><span class="nx">Manager</span> <span class="nv">$manager</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">manager</span> <span class="o">=</span> <span class="nv">$manager</span><span class="p">;</span> <span class="p">}</span>
<span class="p">}</span>
<span class="sd">/** @ODM\Document */</span>
<span class="k">class</span> <span class="nc">Manager</span> <span class="k">extends</span> <span class="nx">BaseEmployee</span>
<span class="p">{</span>
<span class="sd">/** @ODM\ReferenceMany(targetDocument=&quot;Documents\Project&quot;) */</span>
<span class="k">private</span> <span class="nv">$projects</span><span class="p">;</span>
<span class="k">public</span> <span class="nx">__construct</span><span class="p">()</span> <span class="p">{</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">projects</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">ArrayCollection</span><span class="p">();</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getProjects</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">projects</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">addProject</span><span class="p">(</span><span class="nx">Project</span> <span class="nv">$project</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">projects</span><span class="p">[]</span> <span class="o">=</span> <span class="nv">$project</span><span class="p">;</span> <span class="p">}</span>
<span class="p">}</span>
<span class="sd">/** @ODM\EmbeddedDocument */</span>
<span class="k">class</span> <span class="nc">Address</span>
<span class="p">{</span>
<span class="sd">/** @ODM\Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$address</span><span class="p">;</span>
<span class="sd">/** @ODM\Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$city</span><span class="p">;</span>
<span class="sd">/** @ODM\Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$state</span><span class="p">;</span>
<span class="sd">/** @ODM\Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$zipcode</span><span class="p">;</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getAddress</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">address</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setAddress</span><span class="p">(</span><span class="nv">$address</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="nv">$address</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getCity</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">city</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setCity</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">city</span> <span class="o">=</span> <span class="nv">$city</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getState</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">state</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setState</span><span class="p">(</span><span class="nv">$state</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">state</span> <span class="o">=</span> <span class="nv">$state</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getZipcode</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">zipcode</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setZipcode</span><span class="p">(</span><span class="nv">$zipcode</span><span class="p">)</span> <span class="p">{</span> <span class="nv">$this</span><span class="o">-&gt;</span><span class="na">zipcode</span> <span class="o">=</span> <span class="nv">$zipcode</span><span class="p">;</span> <span class="p">}</span>
<span class="p">}</span>
<span class="sd">/** @ODM\Document */</span>
<span class="k">class</span> <span class="nc">Project</span>
<span class="p">{</span>
<span class="sd">/** @ODM\Id */</span>
<span class="k">private</span> <span class="nv">$id</span><span class="p">;</span>
<span class="sd">/** @ODM\Field(type=&quot;string&quot;) */</span>
<span class="k">private</span> <span class="nv">$name</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">-&gt;</span><span class="na">name</span> <span class="o">=</span> <span class="nv">$name</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getId</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">id</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">getName</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">name</span><span class="p">;</span> <span class="p">}</span>
<span class="k">public</span> <span class="k">function</span> <span class="nf">setName</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">-&gt;</span><span class="na">name</span> <span class="o">=</span> <span class="nv">$name</span><span class="p">;</span> <span class="p">}</span>
<span class="p">}</span>
</pre></div>
</div>
<p>Now those objects can be used just like you weren't using any
persistence layer at all and can be persisted transparently by
Doctrine:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">use</span> <span class="nx">Documents\Employee</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Documents\Address</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Documents\Project</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Documents\Manager</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">DateTime</span><span class="p">;</span>
<span class="nv">$employee</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Employee</span><span class="p">();</span>
<span class="nv">$employee</span><span class="o">-&gt;</span><span class="na">setName</span><span class="p">(</span><span class="s1">&#39;Employee&#39;</span><span class="p">);</span>
<span class="nv">$employee</span><span class="o">-&gt;</span><span class="na">setSalary</span><span class="p">(</span><span class="mi">50000</span><span class="p">);</span>
<span class="nv">$employee</span><span class="o">-&gt;</span><span class="na">setStarted</span><span class="p">(</span><span class="k">new</span> <span class="nx">DateTime</span><span class="p">());</span>
<span class="nv">$address</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Address</span><span class="p">();</span>
<span class="nv">$address</span><span class="o">-&gt;</span><span class="na">setAddress</span><span class="p">(</span><span class="s1">&#39;555 Doctrine Rd.&#39;</span><span class="p">);</span>
<span class="nv">$address</span><span class="o">-&gt;</span><span class="na">setCity</span><span class="p">(</span><span class="s1">&#39;Nashville&#39;</span><span class="p">);</span>
<span class="nv">$address</span><span class="o">-&gt;</span><span class="na">setState</span><span class="p">(</span><span class="s1">&#39;TN&#39;</span><span class="p">);</span>
<span class="nv">$address</span><span class="o">-&gt;</span><span class="na">setZipcode</span><span class="p">(</span><span class="s1">&#39;37209&#39;</span><span class="p">);</span>
<span class="nv">$employee</span><span class="o">-&gt;</span><span class="na">setAddress</span><span class="p">(</span><span class="nv">$address</span><span class="p">);</span>
<span class="nv">$project</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Project</span><span class="p">(</span><span class="s1">&#39;New Project&#39;</span><span class="p">);</span>
<span class="nv">$manager</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Manager</span><span class="p">();</span>
<span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">setName</span><span class="p">(</span><span class="s1">&#39;Manager&#39;</span><span class="p">);</span>
<span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">setSalary</span><span class="p">(</span><span class="mi">100000</span><span class="p">);</span>
<span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">setStarted</span><span class="p">(</span><span class="k">new</span> <span class="nx">DateTime</span><span class="p">());</span>
<span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">addProject</span><span class="p">(</span><span class="nv">$project</span><span class="p">);</span>
<span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">persist</span><span class="p">(</span><span class="nv">$employee</span><span class="p">);</span>
<span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">persist</span><span class="p">(</span><span class="nv">$address</span><span class="p">);</span>
<span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">persist</span><span class="p">(</span><span class="nv">$project</span><span class="p">);</span>
<span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">persist</span><span class="p">(</span><span class="nv">$manager</span><span class="p">);</span>
<span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">flush</span><span class="p">();</span>
</pre></div>
</div>
<p>The above would insert the following:</p>
<div class="highlight-default"><div class="highlight"><pre>Array
(
[000000004b0a33690000000001c304c6] =&gt; Array
(
[name] =&gt; New Project
)
)
Array
(
[000000004b0a33660000000001c304c6] =&gt; Array
(
[changes] =&gt; 0
[notes] =&gt; Array
(
)
[name] =&gt; Manager
[salary] =&gt; 100000
[started] =&gt; MongoDate Object
(
[sec] =&gt; 1275265048
[usec] =&gt; 0
)
[projects] =&gt; Array
(
[0] =&gt; Array
(
[$ref] =&gt; projects
[$id] =&gt; 4c0300188ead0e947a000000
[$db] =&gt; my_db
)
)
)
)
Array
(
[000000004b0a336a0000000001c304c6] =&gt; Array
(
[changes] =&gt; 0
[notes] =&gt; Array
(
)
[name] =&gt; Employee
[salary] =&gt; 50000
[started] =&gt; MongoDate Object
(
[sec] =&gt; 1275265048
[usec] =&gt; 0
)
[address] =&gt; Array
(
[address] =&gt; 555 Doctrine Rd.
[city] =&gt; Nashville
[state] =&gt; TN
[zipcode] =&gt; 37209
)
)
)
</pre></div>
</div>
<p>If we update a property and call <code class="docutils literal"><span class="pre">-&gt;flush()</span></code> again we'll get an
efficient update query using the atomic operators:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="nv">$newProject</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Project</span><span class="p">(</span><span class="s1">&#39;Another Project&#39;</span><span class="p">);</span>
<span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">setSalary</span><span class="p">(</span><span class="mi">200000</span><span class="p">);</span>
<span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">addNote</span><span class="p">(</span><span class="s1">&#39;Gave user 100k a year raise&#39;</span><span class="p">);</span>
<span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">incrementChanges</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span>
<span class="nv">$manager</span><span class="o">-&gt;</span><span class="na">addProject</span><span class="p">(</span><span class="nv">$newProject</span><span class="p">);</span>
<span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">persist</span><span class="p">(</span><span class="nv">$newProject</span><span class="p">);</span>
<span class="nv">$dm</span><span class="o">-&gt;</span><span class="na">flush</span><span class="p">();</span>
</pre></div>
</div>
<p>The above could would produce an update that looks something like
this:</p>
<div class="highlight-default"><div class="highlight"><pre>Array
(
[$inc] =&gt; Array
(
[changes] =&gt; 2
)
[$pushAll] =&gt; Array
(
[notes] =&gt; Array
(
[0] =&gt; Gave user 100k a year raise
)
[projects] =&gt; Array
(
[0] =&gt; Array
(
[$ref] =&gt; projects
[$id] =&gt; 4c0310718ead0e767e030000
[$db] =&gt; my_db
)
)
)
[$set] =&gt; Array
(
[salary] =&gt; 200000
)
)
</pre></div>
</div>
<p>This is a simple example, but it demonstrates well that you can
transparently persist PHP objects while still utilizing the
atomic operators for updating documents! Continue reading to learn
how to get the Doctrine MongoDB Object Document Mapper setup and
running!</p>
</div>
<div class="section" id="setup">
<h2>Setup<a class="headerlink" href="#setup" title="Permalink to this headline"></a></h2>
<p>Before we can begin, we'll need to install the Doctrine MongoDB ODM library and
its dependencies. The easiest way to do this is with <a class="reference external" href="http://getcomposer.org/">Composer</a>:</p>
<div class="highlight-default"><div class="highlight"><pre>$ composer require &quot;doctrine/mongodb-odm&quot;
</pre></div>
</div>
<p>Once ODM and its dependencies have been downloaded, we can begin by creating a
<code class="docutils literal"><span class="pre">bootstrap.php</span></code> file in our project's root directory, where Composer's
<code class="docutils literal"><span class="pre">vendor/</span></code> directory also resides. Let's start by importing some of the classes
we'll use:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">use</span> <span class="nx">Doctrine\MongoDB\Connection</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Doctrine\ODM\MongoDB\Configuration</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Doctrine\ODM\MongoDB\DocumentManager</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver</span><span class="p">;</span>
</pre></div>
</div>
<p>The first bit of code will be to import Composer's autoloader, so these classes
can actually be loaded:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="c1">// ...</span>
<span class="k">if</span> <span class="p">(</span> <span class="o">!</span> <span class="nb">file_exists</span><span class="p">(</span><span class="nv">$file</span> <span class="o">=</span> <span class="nx">__DIR__</span><span class="o">.</span><span class="s1">&#39;/vendor/autoload.php&#39;</span><span class="p">))</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">RuntimeException</span><span class="p">(</span><span class="s1">&#39;Install dependencies to run this script.&#39;</span><span class="p">);</span>
<span class="p">}</span>
<span class="nv">$loader</span> <span class="o">=</span> <span class="k">require_once</span> <span class="nv">$file</span><span class="p">;</span>
</pre></div>
</div>
<p>Note that instead of simply requiring the file, we assign its return value to
the <code class="docutils literal"><span class="pre">$loader</span></code> variable. Assuming document classes will be stored in the
<code class="docutils literal"><span class="pre">Documents/</span></code> directory (with a namespace to match), we can register them with
the autoloader like so:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="c1">// ...</span>
<span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">add</span><span class="p">(</span><span class="s1">&#39;Documents&#39;</span><span class="p">,</span> <span class="nx">__DIR__</span><span class="p">);</span>
</pre></div>
</div>
<p>Ultimately, our application will utilize ODM through its <code class="docutils literal"><span class="pre">DocumentManager</span></code>
class. Before we can instantiate a <code class="docutils literal"><span class="pre">DocumentManager</span></code>, we need to construct the
<code class="docutils literal"><span class="pre">Connection</span></code> and <code class="docutils literal"><span class="pre">Configuration</span></code> objects required by its factory method:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="c1">// ...</span>
<span class="nv">$connection</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Connection</span><span class="p">();</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Configuration</span><span class="p">();</span>
</pre></div>
</div>
<p>Next, we'll specify some essential configuration options. The following assumes
that we will store generated proxy and hydrator classes in the <code class="docutils literal"><span class="pre">Proxies/</span></code> and
<code class="docutils literal"><span class="pre">Hydrators/</span></code> directories, respectively. Additionally, we'll define a default
database name to use for document classes that do not specify a database in
their mapping.</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="c1">// ...</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setProxyDir</span><span class="p">(</span><span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/Proxies&#39;</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setProxyNamespace</span><span class="p">(</span><span class="s1">&#39;Proxies&#39;</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setHydratorDir</span><span class="p">(</span><span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/Hydrators&#39;</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setHydratorNamespace</span><span class="p">(</span><span class="s1">&#39;Hydrators&#39;</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setDefaultDB</span><span class="p">(</span><span class="s1">&#39;doctrine_odm&#39;</span><span class="p">);</span>
</pre></div>
</div>
<p>The easiest way to define mappings for our document classes is with annotations.
We'll need to specify an annotation driver in our configuration (with one or
more paths) and register the annotations for the driver:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="c1">// ...</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setMetadataDriverImpl</span><span class="p">(</span><span class="nx">AnnotationDriver</span><span class="o">::</span><span class="na">create</span><span class="p">(</span><span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/Documents&#39;</span><span class="p">));</span>
<span class="nx">AnnotationDriver</span><span class="o">::</span><span class="na">registerAnnotationClasses</span><span class="p">();</span>
</pre></div>
</div>
<p>At this point, we have everything necessary to construct a <code class="docutils literal"><span class="pre">DocumentManager</span></code>:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="c1">// ...</span>
<span class="nv">$dm</span> <span class="o">=</span> <span class="nx">DocumentManager</span><span class="o">::</span><span class="na">create</span><span class="p">(</span><span class="nv">$connection</span><span class="p">,</span> <span class="nv">$config</span><span class="p">);</span>
</pre></div>
</div>
<p>The final <code class="docutils literal"><span class="pre">bootstrap.php</span></code> file should look like this:</p>
<div class="highlight-php"><div class="highlight"><pre><span class="cp">&lt;?php</span>
<span class="k">use</span> <span class="nx">Doctrine\MongoDB\Connection</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Doctrine\ODM\MongoDB\Configuration</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Doctrine\ODM\MongoDB\DocumentManager</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver</span><span class="p">;</span>
<span class="k">if</span> <span class="p">(</span> <span class="o">!</span> <span class="nb">file_exists</span><span class="p">(</span><span class="nv">$file</span> <span class="o">=</span> <span class="nx">__DIR__</span><span class="o">.</span><span class="s1">&#39;/vendor/autoload.php&#39;</span><span class="p">))</span> <span class="p">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="nx">RuntimeException</span><span class="p">(</span><span class="s1">&#39;Install dependencies to run this script.&#39;</span><span class="p">);</span>
<span class="p">}</span>
<span class="nv">$loader</span> <span class="o">=</span> <span class="k">require_once</span> <span class="nv">$file</span><span class="p">;</span>
<span class="nv">$loader</span><span class="o">-&gt;</span><span class="na">add</span><span class="p">(</span><span class="s1">&#39;Documents&#39;</span><span class="p">,</span> <span class="nx">__DIR__</span><span class="p">);</span>
<span class="nv">$connection</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Connection</span><span class="p">();</span>
<span class="nv">$config</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">Configuration</span><span class="p">();</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setProxyDir</span><span class="p">(</span><span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/Proxies&#39;</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setProxyNamespace</span><span class="p">(</span><span class="s1">&#39;Proxies&#39;</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setHydratorDir</span><span class="p">(</span><span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/Hydrators&#39;</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setHydratorNamespace</span><span class="p">(</span><span class="s1">&#39;Hydrators&#39;</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setDefaultDB</span><span class="p">(</span><span class="s1">&#39;doctrine_odm&#39;</span><span class="p">);</span>
<span class="nv">$config</span><span class="o">-&gt;</span><span class="na">setMetadataDriverImpl</span><span class="p">(</span><span class="nx">AnnotationDriver</span><span class="o">::</span><span class="na">create</span><span class="p">(</span><span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/Documents&#39;</span><span class="p">));</span>
<span class="nx">AnnotationDriver</span><span class="o">::</span><span class="na">registerAnnotationClasses</span><span class="p">();</span>
<span class="nv">$dm</span> <span class="o">=</span> <span class="nx">DocumentManager</span><span class="o">::</span><span class="na">create</span><span class="p">(</span><span class="nv">$connection</span><span class="p">,</span> <span class="nv">$config</span><span class="p">);</span>
</pre></div>
</div>
<p>That is it! Your <code class="docutils literal"><span class="pre">DocumentManager</span></code> instance is ready to be used!</p>
</div>
<div class="section" id="using-php-7">
<h2>Using PHP 7<a class="headerlink" href="#using-php-7" title="Permalink to this headline"></a></h2>
<p>You can use Doctrine MongoDB ODM with PHP 7, but there are a few extra steps during
the installation. Since the legacy driver (referred to as <code class="docutils literal"><span class="pre">ext-mongo</span></code>) is not
available on PHP 7, you will need the new driver (<code class="docutils literal"><span class="pre">ext-mongodb</span></code>) installed and
use a polyfill to provide the API of the legacy driver.</p>
<p>To do this, you have to require <code class="docutils literal"><span class="pre">alcaeus/mongo-php-adapter</span></code> before adding a composer
dependency to ODM. To do this, run the following command:</p>
<div class="highlight-default"><div class="highlight"><pre>$ composer require &quot;alcaeus/mongo-php-adapter&quot;
</pre></div>
</div>
<p>Next, manually add a <code class="docutils literal"><span class="pre">provide</span></code> section to your <code class="docutils literal"><span class="pre">composer.json</span></code>:</p>
<div class="highlight-json"><div class="highlight"><pre>&quot;provide&quot;: {
&quot;ext-mongo&quot;: &quot;1.6.14&quot;
}
</pre></div>
</div>
<p>This section needs to be added to work around a composer issue with libraries
providing platform packages (such as <code class="docutils literal"><span class="pre">ext-mongo</span></code>). Now, you may install ODM as
described above:</p>
<div class="highlight-default"><div class="highlight"><pre>$ composer require &quot;doctrine/mongodb-odm&quot;
</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="#">Introduction</a><ul>
<li><a class="reference internal" href="#features-overview">Features Overview</a></li>
<li><a class="reference internal" href="#setup">Setup</a></li>
<li><a class="reference internal" href="#using-php-7">Using PHP 7</a></li>
</ul>
</li>
</ul>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/reference/introduction.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>