downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/class.mongodbref.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/book.mongo.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'class.mongodbref.php',
   
1 => 'MongoDBRef',
  ),
 
'up' =>
  array (
   
0 => 'book.mongo.php',
   
1 => 'Mongo',
  ),
 
'prev' =>
  array (
   
0 => 'mongobindata.tostring.php',
   
1 => 'MongoBinData::__toString',
  ),
 
'next' =>
  array (
   
0 => 'mongodbref.create.php',
   
1 => 'MongoDBRef::create',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div>

 <h1 class="title">The MongoDBRef class</h1>
 

 <div class="partintro">


  <div id="mongodbref.intro" class="section">
   <h2 class="title">Introduction</h2>
   <p class="para">
    This class can be used to create lightweight links between objects in
    different collections.
   </p>
   <p class="para">
    <em class="emphasis">Motivation</em>: Suppose we need to refer to a document in
    another collection. The easiest way is to create a field in the current
    document.  For example, if we had a &quot;people&quot; collection and an &quot;addresses&quot;
    collection, we might want to create a link between each person document and
    and address document:
    </p><div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$people&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">selectCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"people"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$addresses&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">selectCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"addresses"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$myAddress&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"line&nbsp;1"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"123&nbsp;Main&nbsp;Street"</span><span style="color: #007700">,&nbsp;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"line&nbsp;2"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"city"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Springfield"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"state"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Vermont"</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">"country"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"USA"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;save&nbsp;the&nbsp;address<br /></span><span style="color: #0000BB">$addresses</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$myAddress</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;save&nbsp;a&nbsp;person&nbsp;with&nbsp;a&nbsp;reference&nbsp;to&nbsp;the&nbsp;address<br /></span><span style="color: #0000BB">$me&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Fred"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"address"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #0000BB">$myAddress</span><span style="color: #007700">[</span><span style="color: #DD0000">'_id'</span><span style="color: #007700">]);<br /></span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(</span><span style="color: #0000BB">$me</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>
<p>
   </p>
   <p class="para">
    Then, later on, we can find the person&#039;s address by querying the &quot;addresses&quot;
    collection with the <a href="class.mongoid.php" class="classname">MongoId</a> we saved in the &quot;people&quot;
    collection.
   </p>
   <p class="para">
    Suppose now that we have a more general case, where we don&#039;t know which
    collection (or even which database) contains the referenced document. 
    <b class="classname">MongoDBRef</b> is a good choice for this case, as it is a
    common format that all of the drivers and the database understand.
   </p>
   <p class="para">
    If each person had a list of things they liked which could come from
    multiple collections, such as &quot;hobbies&quot;, &quot;sports&quot;, &quot;books&quot;, etc., we could
    use <b class="classname">MongoDBRef</b>s to keep track of what &quot;like&quot; went
    with what collection:
    </p><div class="example-contents programlisting">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$people&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">selectCollection</span><span style="color: #007700">(</span><span style="color: #DD0000">"people"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;model&nbsp;trains&nbsp;are&nbsp;in&nbsp;the&nbsp;"hobbies"&nbsp;collection<br /></span><span style="color: #0000BB">$trainRef&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MongoDBRef</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">"hobbies"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$modelTrains</span><span style="color: #007700">[</span><span style="color: #DD0000">'_id'</span><span style="color: #007700">]);<br /></span><span style="color: #FF8000">//&nbsp;soccer&nbsp;is&nbsp;in&nbsp;the&nbsp;"sports"&nbsp;collection<br /></span><span style="color: #0000BB">$soccerRef&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">MongoDBRef</span><span style="color: #007700">::</span><span style="color: #0000BB">create</span><span style="color: #007700">(</span><span style="color: #DD0000">"sports"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$soccer</span><span style="color: #007700">[</span><span style="color: #DD0000">'_id'</span><span style="color: #007700">]);<br /><br /></span><span style="color: #FF8000">//&nbsp;now&nbsp;we'll&nbsp;know&nbsp;what&nbsp;collections&nbsp;the&nbsp;items&nbsp;in&nbsp;the&nbsp;"likes"&nbsp;array&nbsp;came&nbsp;from&nbsp;when<br />//&nbsp;we&nbsp;retrieve&nbsp;this&nbsp;document<br /></span><span style="color: #0000BB">$people</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">insert</span><span style="color: #007700">(array(</span><span style="color: #DD0000">"name"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"Fred"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"likes"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;array(</span><span style="color: #0000BB">$trainRef</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$soccerRef</span><span style="color: #007700">)));<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>
<p>
   </p>
  </div>


  <div id="mongodbref.synopsis" class="section">
   <h2 class="title">Class synopsis</h2>


   <div class="classsynopsis">
    <div class="ooclass"><b class="classname">MongoDBRef</b></div>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <b class="classname">MongoDBRef</b>
     </span>
     {</div>

   
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Methods */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">array</span> <span class="methodname"><a href="mongodbref.create.php" class="function">create</a></span>
    ( <span class="methodparam"><span class="type">string</span> <tt class="parameter">$collection</tt></span>
   , <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$id</tt></span>
   [, <span class="methodparam"><span class="type">string</span> <tt class="parameter">$database</tt></span>
  ] )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">array</span> <span class="methodname"><a href="mongodbref.get.php" class="function">get</a></span>
    ( <span class="methodparam"><span class="type"><a href="class.mongodb.php" class="type MongoDB">MongoDB</a></span> <tt class="parameter">$db</tt></span>
   , <span class="methodparam"><span class="type">array</span> <tt class="parameter">$ref</tt></span>
   )</div>
<div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="modifier">static</span> <span class="type">boolean</span> <span class="methodname"><a href="mongodbref.isref.php" class="function">isRef</a></span>
    ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.php#language.types.mixed" class="type mixed">mixed</a></span> <tt class="parameter">$ref</tt></span>
   )</div>

   }</div>


  </div>

 </div>

 



 





 





 



<h2>Table of Contents</h2><ul class="chunklist chunklist_reference"><li><a href="mongodbref.create.php">MongoDBRef::create</a> — Creates a new database reference</li><li><a href="mongodbref.get.php">MongoDBRef::get</a> — Fetches the object pointed to by a reference</li><li><a href="mongodbref.isref.php">MongoDBRef::isRef</a> — Checks if an array is a database reference</li></ul>
</div>
<?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites