added getters

git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@292172 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
Kristina Chodorow 2009-12-15 14:34:15 +00:00
parent 680e38f276
commit d35b3508e7
15 changed files with 276 additions and 27 deletions

View file

@ -18,8 +18,8 @@
$m = new Mongo();
// select a database
$db = $m->selectDB("comedy");
$collection = $db->selectCollection("cartoons");
$db = $m->comedy;
$collection = $db->cartoons;
// add an element
$obj = array( "title" => "Calvin and Hobbes", "author" => "Bill Watterson" );

View file

@ -0,0 +1,87 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mongo.get" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>Mongo::__get</refname>
<refpurpose>Gets a database</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>MongoDB</type><methodname>Mongo::__get</methodname>
<methodparam><type>string</type><parameter>dbname</parameter></methodparam>
</methodsynopsis>
<para>
This is the cleanest way of getting a database. If the database name has any
special characters, <function>Mongo::selectDB</function> will need to be
used. However, in most cases, this should be sufficient.
<programlisting role="php">
<![CDATA[
<?php
$mongo = new Mongo();
// the following two lines are equivalent
$db = $mongo->selectDB("foo");
$db = $mongo->foo;
?>
]]>
</programlisting>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term>
<parameter>dbname</parameter>
</term>
<listitem>
<para>
The database name.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns a new db object.
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
Throws InvalidArgumentException if the database name is invalid.
</para>
</refsect1>
</refentry>
<!-- 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
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
-->

View file

@ -56,7 +56,7 @@ for ($i=0; $i<100; $i++) {
}
$m = new Mongo();
$c = $m->selectCollection("foo", "bar.baz");
$c = $m->foo->bar->baz;
$c->batchInsert($batch);
$cursor = $c->find()->sort(array("i" => 1));

View file

@ -48,8 +48,8 @@
<![CDATA[
<?php
$songs = $db->selectCollection('songs');
$playlists = $db->selectCollection('playlists');
$songs = $db->songs;
$playlists = $db->playlists;
// create a reference to a song
$manamana = $songs->findOne(array('title' => 'Ma na ma na'));

View file

@ -51,7 +51,7 @@
<![CDATA[
<?php
$m = new Mongo();
$c = $m->selectCollection("example", "indices");
$c = $m->example->indices;
// create an index
$c->ensureIndex(array("i"=>1));

View file

@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mongocollection.get" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoCollection::__get</refname>
<refpurpose>Gets a collection</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>MongoCollection</type><methodname>MongoCollection::__get</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
</methodsynopsis>
<para>
A concise syntax for getting a collection with a dot-separated name. If a
collection name contains strange characters, you may have to use
<function>MongoDB::selectCollection</function> instead.
<programlisting role="php">
<![CDATA[
<?php
$mongo = new Mongo();
// the following two lines are equivalent
$collection = $mongo->selectDB("foo")->selectCollection("bar.baz");
$collection = $mongo->foo->bar->baz;
?>
]]>
</programlisting>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term>
<parameter>name</parameter>
</term>
<listitem>
<para>
The next string in the collection name.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the collection.
</para>
</refsect1>
</refentry>
<!-- 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
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
-->

View file

@ -48,7 +48,7 @@
<![CDATA[
<?php
$playlists = $db->selectCollection('playlists');
$playlists = $db->playlists;
$myList = $playlists->findOne(array('username' => 'me'));

View file

@ -35,7 +35,7 @@
<?php
$m = new Mongo();
$c = $m->selectDB("foo")->selectCollection("bar.baz");
$c = $m->foo->bar->baz;
echo "Working with collection " . $c->getName() . ".\n";

View file

@ -59,7 +59,7 @@
<![CDATA[
<?php
$radioactive = $db->selectCollection('radioactive');
$radioactive = $db->radioactive;
// count how much more plutonium there is
$remaining = $radioactive->count(array('type' => 94));

View file

@ -35,9 +35,12 @@
<?php
$m = new Mongo();
$c = $m->selectDB("foo")->selectCollection("bar.baz");
echo "Working with collection $c.";
$c1 = $m->foo->bar->baz;
echo "Working with collection $c1.";
$c2 = $m->selectCollection('[]', '&');
echo "Working with collection $c2.";
?>
]]>
@ -46,6 +49,7 @@ echo "Working with collection $c.";
<screen>
<![CDATA[
Working with collection foo.bar.baz.
Working with collection [].&.
]]>
</screen>
</example>

View file

@ -14,13 +14,18 @@
<methodparam choice="opt"><type>boolean</type><parameter>tail</parameter><initializer>true</initializer></methodparam>
</methodsynopsis>
<para>
Mongo has a feature known as tailable cursors which are similar to the Unix "tail -f" command.
Mongo has a feature known as tailable cursors which are similar to the Unix
"tail -f" command.
</para>
<para>
Tailable means cursor is not closed when the last data is retrieved. rather, the cursor marks the final object's position. you can resume using the cursor later, from where it was located, if more data were received.
Tailable means cursor is not closed when the last data is retrieved. Rather,
the cursor marks the final object's position. you can resume using the
cursor later, from where it was located, if more data were received.
</para>
<para>
Like any "latent cursor", the cursor may become invalid at some point -- for example if that final object it references were deleted. Thus, you should be prepared to requery if the cursor is dead().
Like any "latent cursor", the cursor may become invalid at some point -- for
example if that final object it references were deleted. Thus, you should be
prepared to requery if the cursor is <function>MongoCursor::dead</function>.
</para>
</refsect1>

View file

@ -0,0 +1,76 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mongodb.get" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoDB::__get</refname>
<refpurpose>Gets a collection</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>MongoCollection</type><methodname>MongoDB::__get</methodname>
<methodparam><type>string</type><parameter>name</parameter></methodparam>
</methodsynopsis>
<para>
This is the easiest way of getting a collection from a database object. If a
collection name contains strange characters, you may have to use
<function>MongoDB::selectCollection</function> instead.
<programlisting role="php">
<![CDATA[
<?php
$mongo = new Mongo();
// the following two lines are equivalent
$collection = $mongo->selectDB("foo")->selectCollection("bar");
$collection = $mongo->foo->bar;
?>
]]>
</programlisting>
</para>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term>
<parameter>name</parameter>
</term>
<listitem>
<para>
The name of the collection.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
Returns the collection.
</para>
</refsect1>
</refentry>
<!-- 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
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
-->

View file

@ -25,8 +25,8 @@
<![CDATA[
<?php
$people = $db->selectCollection("people");
$addresses = $db->selectCollection("addresses");
$people = $db->people;
$addresses = $db->addresses;
$myAddress = array("line 1" => "123 Main Street",
"line 2" => null,

View file

@ -72,8 +72,8 @@
<programlisting role="php">
<![CDATA[
<?php
$addresses = $db->selectCollection('addresses');
$people = $db->selectCollection('people');
$addresses = $db->addresses;
$people = $db->people;
// save $address so it has an _id
$addresses->insert($address);

View file

@ -32,7 +32,7 @@ $connection = new Mongo( "example.com:65432" ); // connect to a remote host at a
<![CDATA[
<?php
$db = $connection->selectDB( "dbname" );
$db = $connection->dbname;
?>
]]>
@ -45,9 +45,9 @@ $db = $connection->selectDB( "dbname" );
<![CDATA[
<?php
$db = $connection->selectDB( "mybiglongdbname" );
$db = $connection->mybiglongdbname;
// do some stuff
$db = $connection->selectDB( "mybiglongdbnme" );
$db = $connection->mybiglongdbnme;
// now connected to a different database!
]]>
@ -58,16 +58,17 @@ $db = $connection->selectDB( "mybiglongdbnme" );
<section>
<title>Getting A Collection</title>
<para>
To get a specific collection, use
<function>MongoDB::selectCollection</function>. Similarly to
<function>MongoDB::selectDB</function>, this creates the collection if it
does not already exist.
Getting a collection has the same syntax as getting a database:
<programlisting role="php">
<![CDATA[
<?php
$collection = $db->selectCollection( "foobar" );
$db = $connection->baz
$collection = $db->foobar;
// or, more succinctly
$collection = $connection->baz->foobar;
?>
]]>
@ -110,7 +111,7 @@ $doc = array( "name" => "MongoDB",
<?php
$m = new Mongo();
$collection = $m->selectDB( "foo" )->selectCollection( "bar" );
$collection = $m->foo->bar;
$collection->insert( $doc );
?>