php-doc-en/reference/mongo/mongodbref.xml
Hannes Magnusson fb8f8617ef Various markup fixes
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@328555 c90b9560-bf6c-de11-be94-00142212c4b1
2012-11-29 05:37:54 +00:00

159 lines
4.7 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<phpdoc:classref xml:id="class.mongodbref" xmlns:phpdoc="http://php.net/ns/phpdoc" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xi="http://www.w3.org/2001/XInclude">
<title>The MongoDBRef class</title>
<titleabbrev>MongoDBRef</titleabbrev>
<partintro>
<!-- {{{ MongoGridfsCursor intro -->
<section xml:id="mongodbref.intro">
&reftitle.intro;
<para>
This class can be used to create lightweight links between objects in
different collections.
</para>
<para>
<emphasis>Motivation</emphasis>: 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 "people" collection and an "addresses"
collection, we might want to create a link between each person document and
an address document:
<example>
<title>Linking documents</title>
<programlisting role="php">
<![CDATA[
<?php
$people = $db->people;
$addresses = $db->addresses;
$myAddress = array("line 1" => "123 Main Street",
"line 2" => null,
"city" => "Springfield",
"state" => "Vermont",
"country" => "USA");
// save the address
$addresses->insert($myAddress);
// save a person with a reference to the address
$me = array("name" => "Fred", "address" => $myAddress['_id']);
$people->insert($me);
?>
]]>
</programlisting>
</example>
</para>
<para>
Then, later on, we can find the person's address by querying the "addresses"
collection with the <classname>MongoId</classname> we saved in the "people"
collection.
</para>
<para>
Suppose now that we have a more general case, where we don't know which
collection (or even which database) contains the referenced document.
<classname>MongoDBRef</classname> is a good choice for this case, as it is a
common format that all of the drivers and the database understand.
</para>
<para>
If each person had a list of things they liked which could come from
multiple collections, such as "hobbies", "sports", "books", etc., we could
use <classname>MongoDBRef</classname>s to keep track of what "like" went
with what collection:
<example>
<title>Creating MongoDBRef links</title>
<programlisting role="php">
<![CDATA[
<?php
$people = $db->selectCollection("people");
// model trains are in the "hobbies" collection
$trainRef = MongoDBRef::create("hobbies", $modelTrains['_id']);
// soccer is in the "sports" collection
$soccerRef = MongoDBRef::create("sports", $soccer['_id']);
// now we'll know what collections the items in the "likes" array came from when
// we retrieve this document
$people->insert(array("name" => "Fred", "likes" => array($trainRef, $soccerRef)));
?>
]]>
</programlisting>
</example>
</para>
<para>
Database references can be thought of as hyperlinks: they give the unique
address of another document, but they do not load it or automatically follow
the link/reference.
</para>
<para>
A database reference is just a normal associative array, not an instance of
<classname>MongoDBRef</classname>, so this class is a little different than
the other data type classes. This class contains exclusively static methods
for manipulating database references.
</para>
</section>
<!-- }}} -->
<section xml:id="mongodbref.synopsis">
&reftitle.classsynopsis;
<!-- {{{ Synopsis -->
<classsynopsis>
<ooclass><classname>MongoDBRef</classname></ooclass>
<!-- {{{ Class synopsis -->
<classsynopsisinfo>
<ooclass>
<classname>MongoDBRef</classname>
</ooclass>
</classsynopsisinfo>
<!-- }}} -->
<classsynopsisinfo role="comment">&Methods;</classsynopsisinfo>
<xi:include xpointer="xmlns(db=http://docbook.org/ns/docbook) xpointer(id('class.mongodbref')/db:refentry/db:refsect1[@role='description']/descendant::db:methodsynopsis[1])" />
</classsynopsis>
<!-- }}} -->
</section>
<section>
&reftitle.seealso;
<para>
MongoDB core docs on <link xlink:href="&url.mongodb.dochub.dbrefs;">databases references</link>.
</para>
</section>
</partintro>
&reference.mongo.entities.mongodbref;
</phpdoc:classref>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
<!-- LocalWords: trainRef soccerRef MongoDBRef
-->