334 lines
20 KiB
HTML
334 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>Blending the ORM and MongoDB ODM — 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="blending-the-orm-and-mongodb-odm">
|
|
<h1>Blending the ORM and MongoDB ODM<a class="headerlink" href="#blending-the-orm-and-mongodb-odm" title="Permalink to this headline">¶</a></h1>
|
|
<p>Since the start of the <a class="reference external" href="http://www.doctrine-project.org/projects/mongodb_odm">Doctrine MongoDB Object Document Mapper</a> project people have asked how it can be integrated with the <a class="reference external" href="http://www.doctrine-project.org/projects/orm">ORM</a>. This article will demonstrates how you can integrate the two transparently, maintaining a clean domain model.</p>
|
|
<p>This example will have a <cite>Product</cite> that is stored in MongoDB and the <cite>Order</cite> stored in a MySQL database.</p>
|
|
<div class="section" id="define-product">
|
|
<h2>Define Product<a class="headerlink" href="#define-product" title="Permalink to this headline">¶</a></h2>
|
|
<p>First lets define our <cite>Product</cite> document:</p>
|
|
<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">Product</span>
|
|
<span class="p">{</span>
|
|
<span class="sd">/** @Id */</span>
|
|
<span class="k">private</span> <span class="nv">$id</span><span class="p">;</span>
|
|
|
|
<span class="sd">/** @Field(type="string") */</span>
|
|
<span class="k">private</span> <span class="nv">$title</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">-></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">getTitle</span><span class="p">()</span>
|
|
<span class="p">{</span>
|
|
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">title</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">setTitle</span><span class="p">(</span><span class="nv">$title</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">title</span> <span class="o">=</span> <span class="nv">$title</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="define-entity">
|
|
<h2>Define Entity<a class="headerlink" href="#define-entity" title="Permalink to this headline">¶</a></h2>
|
|
<p>Next create the <cite>Order</cite> entity that has a <cite>$product</cite> and <cite>$productId</cite> property linking it to the <cite>Product</cite> that is stored with MongoDB:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="k">namespace</span> <span class="nx">Entities</span><span class="p">;</span>
|
|
|
|
<span class="k">use</span> <span class="nx">Documents\Product</span><span class="p">;</span>
|
|
|
|
<span class="sd">/**</span>
|
|
<span class="sd"> * @Entity</span>
|
|
<span class="sd"> * @Table(name="orders")</span>
|
|
<span class="sd"> */</span>
|
|
<span class="k">class</span> <span class="nc">Order</span>
|
|
<span class="p">{</span>
|
|
<span class="sd">/**</span>
|
|
<span class="sd"> * @Id @Column(type="integer")</span>
|
|
<span class="sd"> * @GeneratedValue(strategy="AUTO")</span>
|
|
<span class="sd"> */</span>
|
|
<span class="k">private</span> <span class="nv">$id</span><span class="p">;</span>
|
|
|
|
<span class="sd">/**</span>
|
|
<span class="sd"> * @Column(type="string")</span>
|
|
<span class="sd"> */</span>
|
|
<span class="k">private</span> <span class="nv">$productId</span><span class="p">;</span>
|
|
|
|
<span class="sd">/**</span>
|
|
<span class="sd"> * @var Documents\Product</span>
|
|
<span class="sd"> */</span>
|
|
<span class="k">private</span> <span class="nv">$product</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">-></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">getProductId</span><span class="p">()</span>
|
|
<span class="p">{</span>
|
|
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">productId</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">setProduct</span><span class="p">(</span><span class="nx">Product</span> <span class="nv">$product</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">productId</span> <span class="o">=</span> <span class="nv">$product</span><span class="o">-></span><span class="na">getId</span><span class="p">();</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">product</span> <span class="o">=</span> <span class="nv">$product</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">getProduct</span><span class="p">()</span>
|
|
<span class="p">{</span>
|
|
<span class="k">return</span> <span class="nv">$this</span><span class="o">-></span><span class="na">product</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
<div class="section" id="event-subscriber">
|
|
<h2>Event Subscriber<a class="headerlink" href="#event-subscriber" title="Permalink to this headline">¶</a></h2>
|
|
<p>Now we need to setup an event subscriber that will set the <cite>$product</cite> property of all <cite>Order</cite> instances to a reference to the document product so it can be lazily loaded when it is accessed the first time. So first register a new event subscriber:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$eventManager</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">getEventManager</span><span class="p">();</span>
|
|
<span class="nv">$eventManager</span><span class="o">-></span><span class="na">addEventListener</span><span class="p">(</span>
|
|
<span class="k">array</span><span class="p">(</span><span class="nx">\Doctrine\ORM\Events</span><span class="o">::</span><span class="na">postLoad</span><span class="p">),</span> <span class="k">new</span> <span class="nx">MyEventSubscriber</span><span class="p">(</span><span class="nv">$dm</span><span class="p">)</span>
|
|
<span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>So now we need to define a class named <cite>MyEventSubscriber</cite> and pass a dependency to the <cite>DocumentManager</cite>. It will have a <cite>postLoad()</cite> method that sets the product document reference:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</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\ORM\Event\LifecycleEventArgs</span><span class="p">;</span>
|
|
|
|
<span class="k">class</span> <span class="nc">MyEventSubscriber</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="nx">DocumentManager</span> <span class="nv">$dm</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$this</span><span class="o">-></span><span class="na">dm</span> <span class="o">=</span> <span class="nv">$dm</span><span class="p">;</span>
|
|
<span class="p">}</span>
|
|
|
|
<span class="k">public</span> <span class="k">function</span> <span class="nf">postLoad</span><span class="p">(</span><span class="nx">LifecycleEventArgs</span> <span class="nv">$eventArgs</span><span class="p">)</span>
|
|
<span class="p">{</span>
|
|
<span class="nv">$order</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getEntity</span><span class="p">();</span>
|
|
<span class="nv">$em</span> <span class="o">=</span> <span class="nv">$eventArgs</span><span class="o">-></span><span class="na">getEntityManager</span><span class="p">();</span>
|
|
<span class="nv">$productReflProp</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">getClassMetadata</span><span class="p">(</span><span class="s1">'Entities\Order'</span><span class="p">)</span>
|
|
<span class="o">-></span><span class="na">reflClass</span><span class="o">-></span><span class="na">getProperty</span><span class="p">(</span><span class="s1">'product'</span><span class="p">);</span>
|
|
<span class="nv">$productReflProp</span><span class="o">-></span><span class="na">setAccessible</span><span class="p">(</span><span class="k">true</span><span class="p">);</span>
|
|
<span class="nv">$productReflProp</span><span class="o">-></span><span class="na">setValue</span><span class="p">(</span>
|
|
<span class="nv">$order</span><span class="p">,</span> <span class="nv">$this</span><span class="o">-></span><span class="na">dm</span><span class="o">-></span><span class="na">getReference</span><span class="p">(</span><span class="s1">'Documents\Product'</span><span class="p">,</span> <span class="nv">$order</span><span class="o">-></span><span class="na">getProductId</span><span class="p">())</span>
|
|
<span class="p">);</span>
|
|
<span class="p">}</span>
|
|
<span class="p">}</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The <cite>postLoad</cite> method will be invoked after an ORM entity is loaded from the database. This allows us to use the <cite>DocumentManager</cite> to set the <cite>$product</cite> property with a reference to the <cite>Product</cite> document with the product id we previously stored.</p>
|
|
</div>
|
|
<div class="section" id="working-with-products-and-orders">
|
|
<h2>Working with Products and Orders<a class="headerlink" href="#working-with-products-and-orders" title="Permalink to this headline">¶</a></h2>
|
|
<p>First create a new <cite>Product</cite>:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$product</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Documents\Product</span><span class="p">();</span>
|
|
<span class="nv">$product</span><span class="o">-></span><span class="na">setTitle</span><span class="p">(</span><span class="s1">'Test Product'</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">$product</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>Now create a new <cite>Order</cite> and link it to a <cite>Product</cite> in MySQL:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$order</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">\Entities\Order</span><span class="p">();</span>
|
|
<span class="nv">$order</span><span class="o">-></span><span class="na">setProduct</span><span class="p">(</span><span class="nv">$product</span><span class="p">);</span>
|
|
<span class="nv">$em</span><span class="o">-></span><span class="na">persist</span><span class="p">(</span><span class="nv">$order</span><span class="p">);</span>
|
|
<span class="nv">$em</span><span class="o">-></span><span class="na">flush</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>Later we can retrieve the entity and lazily load the reference to the document in MongoDB:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nv">$order</span> <span class="o">=</span> <span class="nv">$em</span><span class="o">-></span><span class="na">find</span><span class="p">(</span><span class="s1">'Order'</span><span class="p">,</span> <span class="nv">$order</span><span class="o">-></span><span class="na">getId</span><span class="p">());</span>
|
|
|
|
<span class="c1">// Instance of an uninitialized product proxy</span>
|
|
<span class="nv">$product</span> <span class="o">=</span> <span class="nv">$order</span><span class="o">-></span><span class="na">getProduct</span><span class="p">();</span>
|
|
|
|
<span class="c1">// Initializes proxy and queries the database</span>
|
|
<span class="k">echo</span> <span class="s2">"Order Title: "</span> <span class="o">.</span> <span class="nv">$product</span><span class="o">-></span><span class="na">getTitle</span><span class="p">();</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>If you were to print the <cite>$order</cite> you would see that we got back regular PHP objects:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="cp"><?php</span>
|
|
|
|
<span class="nb">print_r</span><span class="p">(</span><span class="nv">$order</span><span class="p">);</span>
|
|
</pre></div>
|
|
</div>
|
|
<p>The above would output the following:</p>
|
|
<div class="highlight-php"><div class="highlight"><pre><span class="x">Order Object</span>
|
|
<span class="x">(</span>
|
|
<span class="x"> [id:Entities\Order:private] => 53</span>
|
|
<span class="x"> [productId:Entities\Order:private] => 4c74a1868ead0ed7a9000000</span>
|
|
<span class="x"> [product:Entities\Order:private] => Proxies\DocumentsProductProxy Object</span>
|
|
<span class="x"> (</span>
|
|
<span class="x"> [__isInitialized__] => 1</span>
|
|
<span class="x"> [id:Documents\Product:private] => 4c74a1868ead0ed7a9000000</span>
|
|
<span class="x"> [title:Documents\Product:private] => Test Product</span>
|
|
<span class="x"> )</span>
|
|
<span class="x">)</span>
|
|
</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="#">Blending the ORM and MongoDB ODM</a><ul>
|
|
<li><a class="reference internal" href="#define-product">Define Product</a></li>
|
|
<li><a class="reference internal" href="#define-entity">Define Entity</a></li>
|
|
<li><a class="reference internal" href="#event-subscriber">Event Subscriber</a></li>
|
|
<li><a class="reference internal" href="#working-with-products-and-orders">Working with Products and Orders</a></li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<h3>This Page</h3>
|
|
<ul class="this-page-menu">
|
|
<li><a href="../_sources/cookbook/blending-orm-and-mongodb-odm.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> |