mirror of
https://github.com/sigmasternchen/php-doc-en
synced 2025-03-15 16:38:54 +00:00
Update configure and tutorial docs for ext-mongodb and library
https://jira.mongodb.org/browse/PHPC-468 git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@338537 c90b9560-bf6c-de11-be94-00142212c4b1
This commit is contained in:
parent
f6242b02c7
commit
c718200b27
5 changed files with 290 additions and 258 deletions
|
@ -4,11 +4,17 @@
|
|||
<article xml:id="mongodb.installation" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
&reftitle.install;
|
||||
|
||||
<section xml:id="mongodb.installation.php">
|
||||
<title>PHP</title>
|
||||
<section xml:id="mongodb.installation.pecl">
|
||||
<title>Installing the MongoDB PHP Driver with PECL</title>
|
||||
|
||||
<para>
|
||||
Run:
|
||||
&pecl.info;
|
||||
<link xlink:href="&url.pecl.package;mongodb">&url.pecl.package;mongodb</link>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Linux, Unix, and OS X users may run the following command to install the
|
||||
driver:
|
||||
<programlisting role="shell">
|
||||
<![CDATA[
|
||||
$ sudo pecl install mongodb
|
||||
|
@ -17,44 +23,184 @@ $ sudo pecl install mongodb
|
|||
</para>
|
||||
|
||||
<para>
|
||||
Add the following line to your php.ini file:
|
||||
Add the following line to your &php.ini; file:
|
||||
<programlisting role="ini">
|
||||
<![CDATA[
|
||||
extension=mongodb.so
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="mongodb.installation.homebrew">
|
||||
<title>Installing the MongoDB PHP Driver on OSX with Homebrew</title>
|
||||
|
||||
<para>
|
||||
Windows users should include <filename>php_mongodb.dll</filename> into &php.ini;
|
||||
If you are using <link xlink:href="&url.mac.homebrew;">Homebrew</link>, the
|
||||
PHP tap includes formulae for installing the driver on various PHP versions.
|
||||
</para>
|
||||
|
||||
<simplelist>
|
||||
<member>php54-mongodb</member>
|
||||
<member>php55-mongodb</member>
|
||||
<member>php56-mongodb</member>
|
||||
<member>php70-mongodb</member>
|
||||
</simplelist>
|
||||
|
||||
<para>
|
||||
For example, you might install the driver for PHP 7.0 using the following
|
||||
command:
|
||||
<programlisting role="shell">
|
||||
<![CDATA[
|
||||
$ brew install php70-mongodb
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="mongodb.installation.windows">
|
||||
<title>Installing the MongoDB PHP Driver on Windows</title>
|
||||
|
||||
<para>
|
||||
Precompiled binaries for each release are available from
|
||||
<link xlink:href="&url.pecl.package;mongodb">PECL</link> for a variety of
|
||||
combinations of versions, thread safety, and VC libraries. Extract the
|
||||
archive and put <filename>php_mongo.dll</filename> in your PHP extension
|
||||
directory ("ext" by default).
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Add the following line to your &php.ini; file:
|
||||
<programlisting role="ini">
|
||||
<![CDATA[
|
||||
extension=php_mongo.dll
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="mongodb.installation.manual">
|
||||
<title>Manually Installing the MongoDB PHP Driver</title>
|
||||
|
||||
<para>
|
||||
For driver developers and people interested in the latest bugfixes, you can
|
||||
compile the driver from the latest source code on
|
||||
<link xlink:href="&url.mongodb.github;">Github</link>. Run the following
|
||||
commands to clone and build the project:
|
||||
<programlisting role="shell">
|
||||
<![CDATA[
|
||||
$ git clone https://github.com/mongodb/mongo-php-driver.git
|
||||
$ cd mongo-php-driver
|
||||
$ git submodule sync && git submodule update --init
|
||||
$ phpize
|
||||
$ ./configure
|
||||
$ make all -j 5
|
||||
$ sudo make install
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
The last step will report where <filename>mongodb.so</filename> has been
|
||||
installed, similar to:
|
||||
<programlisting role="txt">
|
||||
<![CDATA[
|
||||
Installing shared extensions: /usr/lib/php/extensions/debug-non-zts-20151012/
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Ensure that the <link linkend="ini.extension-dir">extension_dir</link> option
|
||||
in &php.ini; points to the directory where <filename>mongodb.so</filename>
|
||||
was installed. You can query the option by running:
|
||||
<programlisting role="shell">
|
||||
<![CDATA[
|
||||
$ php -i | grep extension_dir
|
||||
extension_dir => /usr/lib/php/extensions/debug-non-zts-20151012 =>
|
||||
/usr/lib/php/extensions/debug-non-zts-20151012
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
If the directories differ, either change
|
||||
<link linkend="ini.extension-dir">extension_dir</link> in &php.ini; or
|
||||
manually move <filename>mongodb.so</filename> to the correct directory.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Add the following line to your &php.ini; file:
|
||||
<programlisting role="ini">
|
||||
<![CDATA[
|
||||
extension=mongodb.so
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="mongodb.installation.hhvm">
|
||||
<title>HHVM</title>
|
||||
<title>Manually Installing the MongoDB HHVM Driver</title>
|
||||
|
||||
<para>
|
||||
HHVM users should download the latest from
|
||||
<link
|
||||
xlink:href="https://s3.amazonaws.com/drivers.mongodb.org/hhvm/index.html">S3</link>
|
||||
for the time being.
|
||||
At the time of this writing, HHVM does not have a package manager for
|
||||
extensions. Download the latest driver from
|
||||
<link xlink:href="&url.mongodb.hhvm.releases;">GitHub</link>. Alternatively,
|
||||
you may clone the repository to build the latest source code:
|
||||
<programlisting role="shell">
|
||||
<![CDATA[
|
||||
$ git clone https://github.com/mongodb/mongo-hhvm-driver.git
|
||||
$ cd mongo-hhvm-driver
|
||||
$ git submodule sync && git submodule update --init
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Once downloaded, execute the following steps:
|
||||
<programlisting><![CDATA[
|
||||
hphpize
|
||||
cmake .
|
||||
|
||||
make configlib
|
||||
|
||||
make -j 5
|
||||
make install
|
||||
]]></programlisting>
|
||||
Generate the <code>configure</code> files for the bundled libraries. For this
|
||||
this to work, you need to have the <code>automake</code>,
|
||||
<code>autoconf</code>, and <code>libtool</code> packages installed (e.g.
|
||||
through <code>apt-get</code>).
|
||||
<programlisting role="shell">
|
||||
<![CDATA[
|
||||
$ cd libbson; ./autogen.sh; cd ..
|
||||
$ cd libmongoc; ./autogen.sh; cd ..
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Add the following line to your php.ini file (usually found in /etc/hhvm):
|
||||
Build and install the driver by running:
|
||||
<programlisting role="shell">
|
||||
<![CDATA[
|
||||
$ hphpize
|
||||
$ cmake .
|
||||
$ make -j 5
|
||||
$ sudo make install
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<note>
|
||||
<para>
|
||||
If the <code>hphpize</code> command is not available, you will need to
|
||||
either compile HHVM from source or install the <code>hhvm-dev</code> package
|
||||
(e.g. through <code>apt-get</code>).
|
||||
</para>
|
||||
</note>
|
||||
|
||||
<para>
|
||||
The last step will report where <filename>mongodb.so</filename> has been
|
||||
installed, similar to:
|
||||
<programlisting role="txt">
|
||||
<![CDATA[
|
||||
Installing: /usr/local/hhvm/3.9.1/lib/hhvm/extensions/20150212/mongodb.so
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Add the following line to your &php.ini; file (usually found in "/etc/hhvm"):
|
||||
<programlisting role="ini">
|
||||
<![CDATA[
|
||||
hhvm.dynamic_extension_path=/usr/local/hhvm/3.9.1/lib/hhvm/extensions/20150212
|
||||
|
|
|
@ -10,24 +10,12 @@
|
|||
<para>
|
||||
In this section you will find several tutorials on how to use the MongoDB
|
||||
driver for PHP and HHVM. Because the drivers work exactly the same, they
|
||||
share common usage tutorials, but have a separate installation section.
|
||||
</para>
|
||||
<para>
|
||||
In order to work through them all, pick either <xref
|
||||
linkend="mongodb.tutorial.install.php"/> or <xref
|
||||
linkend="mongodb.tutorial.install.hhvm"/> section and then continue by
|
||||
following the link at the bottom of each of these sections.
|
||||
share common usage tutorials, but have a separate installation sections.
|
||||
</para>
|
||||
</abstract>
|
||||
</info>
|
||||
|
||||
<chapter xml:id="mongodb.tutorial.install" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<title>Installing the driver</title>
|
||||
|
||||
&reference.mongodb.tutorial.hhvm;
|
||||
&reference.mongodb.tutorial.php;
|
||||
</chapter>
|
||||
|
||||
&reference.mongodb.tutorial.hhvm;
|
||||
&reference.mongodb.tutorial.library;
|
||||
</book>
|
||||
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- $Revision: 336263 $ -->
|
||||
|
||||
<section xml:id="mongodb.tutorial.install.hhvm" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<title>Installation with HHVM</title>
|
||||
<chapter xml:id="mongodb.tutorial.hhvm" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<title>Getting Started with HHVM</title>
|
||||
|
||||
<para>
|
||||
In this section you will learn how to set-up NGINX with HHVM and MongoDB.
|
||||
|
@ -11,13 +11,13 @@
|
|||
</para>
|
||||
|
||||
<para>
|
||||
In this tutorial, we will be using a Debian system, with NGINX installed
|
||||
through <code>apt-get</code> and HHVM installed from
|
||||
<emphasis>source</emphasis> into <code>/usr/local/hhvm/3.9.1</code>
|
||||
(with the binary located at <code>/usr/local/hhvm/3.9.1/bin/hhvm</code>).
|
||||
In this tutorial, we will be using a Debian system with NGINX installed
|
||||
through <code>apt-get</code> and HHVM installed from source or pre-built
|
||||
packages into <code>/usr/local/hhvm/3.9.1</code> (with the binary located at
|
||||
<code>/usr/local/hhvm/3.9.1/bin/hhvm</code>).
|
||||
</para>
|
||||
|
||||
<section xml:id="mongodb.tutorial.install.hhvm.nginx">
|
||||
<section xml:id="mongodb.tutorial.hhvm.nginx">
|
||||
<title>NGINX</title>
|
||||
|
||||
<para>
|
||||
|
@ -41,7 +41,7 @@
|
|||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="mongodb.tutorial.install.hhvm.hhvm">
|
||||
<section xml:id="mongodb.tutorial.hhvm.hhvm">
|
||||
<title>HHVM</title>
|
||||
|
||||
<para>
|
||||
|
@ -49,8 +49,8 @@
|
|||
we've installed HHVM from source to faciliate patch development and ensure
|
||||
that debug symbols are available. That said, the folks at Facebook also
|
||||
provide pre-built packages, which is probably what <emphasis>you</emphasis>
|
||||
to use in production and development. You can find installation instructions
|
||||
for these packages on the
|
||||
will want to use in production and development. You can find installation
|
||||
instructions for these packages on the
|
||||
<link xlink:href="&url.facebook.hhvm.prebuild;">HHVM Wiki</link>.
|
||||
</para>
|
||||
|
||||
|
@ -94,7 +94,7 @@ sudo -u www-data -s /usr/local/hhvm/3.9.1/bin/hhvm \
|
|||
</programlisting>
|
||||
</section>
|
||||
|
||||
<section xml:id="mongodb.tutorial.install.hhvm.nginxandhhvm">
|
||||
<section xml:id="mongodb.tutorial.hhvm.nginxandhhvm">
|
||||
<title>Making NGINX talk to HHVM</title>
|
||||
|
||||
<para>
|
||||
|
@ -105,7 +105,7 @@ sudo -u www-data -s /usr/local/hhvm/3.9.1/bin/hhvm \
|
|||
<code>location / { … }</code> section:
|
||||
</para>
|
||||
|
||||
<programlisting>
|
||||
<programlisting role="conf">
|
||||
<![CDATA[
|
||||
location ~ \.php$ {
|
||||
fastcgi_pass unix:/var/run/hhvm/sock;
|
||||
|
@ -172,85 +172,19 @@ phpinfo();
|
|||
</para>
|
||||
</section>
|
||||
|
||||
<section xml:id="mongodb.tutorial.install.hhvm.driver">
|
||||
<section xml:id="mongodb.tutorial.hhvm.driver">
|
||||
<title>MongoDB Driver for HHVM</title>
|
||||
|
||||
<para>
|
||||
The MongoDB driver is the part that links up the PHP in HHVM with the
|
||||
database server. To install and register the driver with HHVM, you need to
|
||||
take the following steps:
|
||||
<itemizedlist>
|
||||
<listitem>
|
||||
<para>
|
||||
Download the latest driver from
|
||||
<link xlink:href="&url.mongodb.hhvm.releases;">GitHub</link> (at the time
|
||||
of this writing, HHVM does not have a package manager for extensions). In
|
||||
the examples below, we will use "x.y.z" as a version placeholder.
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Unpack the archive: <code>tar -xvzf hhvm-mongodb-x.y.z.tgz</code>
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Change to the newly created directory: <code>cd hhvm-mongodb-x.y.z</code>
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Generate the <code>configure</code> files for the bundled libraries. For
|
||||
this to work, you need to have the <code>automake</code>,
|
||||
<code>autoconf</code>, and <code>libtool</code> packages installed
|
||||
(through <code>apt-get</code>).
|
||||
</para>
|
||||
|
||||
<programlisting>
|
||||
<![CDATA[
|
||||
cd libbson; ./autogen.sh; cd ..
|
||||
cd libmongoc; ./autogen.sh; cd ..
|
||||
]]>
|
||||
</programlisting>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Generate the Makefile: <code>hphpize && cmake .</code>
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Build the driver: <code>make -j 5</code>
|
||||
</para>
|
||||
</listitem>
|
||||
<listitem>
|
||||
<para>
|
||||
Install the driver: <code>sudo make install</code>. This last step will
|
||||
report where the binary file <code>mongodb.so</code> has been installed.
|
||||
It should show somthing similar to: <code>Installing:
|
||||
/usr/local/hhvm/3.9.1/lib/hhvm/extensions/20150212/mongodb.so</code>
|
||||
</para>
|
||||
</listitem>
|
||||
</itemizedlist>
|
||||
follow the steps in <xref linkend="mongodb.installation.hhvm"/>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Now that the driver is installed, you need to enable it in HHVM. Add the
|
||||
following lines to <code>/etc/hhvm/php.ini</code>, swapping out the directory
|
||||
in this example for the path shown after running <code>make install</code>:
|
||||
</para>
|
||||
|
||||
<programlisting role="shell">
|
||||
<![CDATA[
|
||||
hhvm.dynamic_extension_path=/usr/local/hhvm/3.9.1/lib/hhvm/extensions/20150212
|
||||
hhvm.dynamic_extensions[mongodb]=mongodb.so
|
||||
]]>
|
||||
</programlisting>
|
||||
|
||||
<para>
|
||||
After you have done this, HHVM will need to be restarted. If you have HHVM
|
||||
running in the shell from earlier, stop it with Ctrl-C and restart it again
|
||||
as above:
|
||||
After you have installed and enabled the extension, HHVM will need to be
|
||||
restarted. If you have HHVM running in the shell from earlier, stop it with
|
||||
Ctrl-C and restart it again as above:
|
||||
</para>
|
||||
|
||||
<programlisting role="shell">
|
||||
|
@ -277,7 +211,7 @@ var_dump(phpversion("mongodb"));
|
|||
|
||||
<para>This should output something similar to:</para>
|
||||
|
||||
<programlisting>
|
||||
<programlisting role="text">
|
||||
<![CDATA[
|
||||
string(5) "x.y.z"
|
||||
]]>
|
||||
|
@ -292,7 +226,7 @@ string(5) "x.y.z"
|
|||
<xref linkend="mongodb.tutorial.library"/>
|
||||
</para>
|
||||
</section>
|
||||
</section>
|
||||
</chapter>
|
||||
<!-- Keep this comment at the end of the file
|
||||
Local variables:
|
||||
mode: sgml
|
||||
|
|
|
@ -2,97 +2,98 @@
|
|||
<!-- $Revision: 336263 $ -->
|
||||
|
||||
<chapter xml:id="mongodb.tutorial.library" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<title>Using the library</title>
|
||||
<title>Using the PHP Library (PHPLIB)</title>
|
||||
|
||||
<para>
|
||||
After the initial set-up, we continue explaining how to get started with the
|
||||
MongoDB driver and library for HHVM to write our first project.
|
||||
After the initial driver set-up, we will continue explaining how to get
|
||||
started with the MongoDB driver and corresponding userland library to write
|
||||
our first project.
|
||||
</para>
|
||||
|
||||
<section>
|
||||
<title>PHP Library (PHPLIB)</title>
|
||||
<section>
|
||||
<title>Installing the PHP Library with Composer</title>
|
||||
|
||||
<para>
|
||||
The last thing we still need to install to get started on the application
|
||||
itself, is the PHP library.
|
||||
<para>
|
||||
The last thing we still need to install to get started on the application
|
||||
itself, is the PHP library.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
The library needs to be installed with
|
||||
<link xlink:href="&url.mongodb.composer;">Composer</link>, a package manager
|
||||
for PHP. Instructions for installing Composer on various platforms may be
|
||||
found on its website.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
The library needs to be installed with Composer. In your project directory
|
||||
(<code>/var/www/html/my-first-project</code>) type:
|
||||
|
||||
Install the library by running:
|
||||
<programlisting role="shell">
|
||||
curl -sS https://getcomposer.org/installer -o installer.php
|
||||
hhvm installer.php
|
||||
rm installer.php
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
This downloads and installs Composer. Wherever it says "Use it: php
|
||||
composer.phar", it of course means <literal>hhvm composer.phar</literal>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
With Composer installed, we can now install the library:
|
||||
|
||||
<programlisting role="shell">
|
||||
hhvm composer.phar require mongodb/mongodb
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
It outputs something akin to:
|
||||
|
||||
<programlisting role="shell">
|
||||
Using version ^0.2.0 for mongodb/mongodb
|
||||
./composer.json has been created
|
||||
Loading composer repositories with package information
|
||||
Updating dependencies (including require-dev)
|
||||
- Installing mongodb/mongodb (0.2.0)
|
||||
Downloading: 100%
|
||||
|
||||
Writing lock file
|
||||
Generating autoload files
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
And it has created several files (<code>composer.json</code>,
|
||||
<code>composer.lock</code>) as well as the <code>vendor</code> directory
|
||||
that contains the library.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Using the Library</title>
|
||||
|
||||
<para>
|
||||
Composer manages your dependencies, and will provide you with a loader that
|
||||
you include with the following at the start of your script:
|
||||
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
$ composer require "mongodb/mongodb=^1.0.0"
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
With this done, you can now use any of the functionality as described in
|
||||
the <link xlink:href="&url.mongodb.library.apidocs;">documentation</link>.
|
||||
It will output something akin to:
|
||||
|
||||
<programlisting role="text">
|
||||
<![CDATA[
|
||||
./composer.json has been created
|
||||
Loading composer repositories with package information
|
||||
Updating dependencies (including require-dev)
|
||||
- Installing mongodb/mongodb (1.0.0)
|
||||
Downloading: 100%
|
||||
|
||||
Writing lock file
|
||||
Generating autoload files
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
If you are familiar with the old driver, it should look too much out of
|
||||
place. The only big difference is that the
|
||||
Composer will create several files: <code>composer.json</code>,
|
||||
<code>composer.lock</code>, and a <code>vendor</code> directory that will
|
||||
contain the library and any other dependencies your project might require.
|
||||
</para>
|
||||
</section>
|
||||
|
||||
<section>
|
||||
<title>Using the PHP Library</title>
|
||||
|
||||
<para>
|
||||
In addition to managing your dependencies, Composer will also provide you
|
||||
with an autoloader (for those dependencies' classes). Ensure that it is
|
||||
included at the start of your script or in your application's bootstrap
|
||||
code:
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
// This path should point to Composer's autoloader
|
||||
require 'vendor/autoload.php';
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
With this done, you can now use any of the functionality as described in the
|
||||
<link xlink:href="&url.mongodb.library.docs;">library documentation</link>
|
||||
and its <link xlink:href="&url.mongodb.library.apidocs;">API</link>.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
If you have previously used the old driver (i.e. <code>mongo</code>
|
||||
extension), the library's API should look familiar. It contains a
|
||||
<link xlink:href="&url.mongodb.library.apidocs;/class-MongoDB.Client.html">Client</link>
|
||||
class for connecting to MongoDB, and
|
||||
<link xlink:href="&url.mongodb.library.apidocs;/class-MongoDB.Database.html">Database</link>
|
||||
class is only used for Database specific operations. The
|
||||
<link xlink:href="&url.wiki.crud;">CRUD</link> operations on the
|
||||
class for database-level operations (e.g. commands, collection management)
|
||||
and a
|
||||
<link xlink:href="&url.mongodb.library.apidocs;/class-MongoDB.Collection.html">Collection</link>
|
||||
class are also renamed for clarity, and to be in accordance with a new
|
||||
language-agnostic
|
||||
class for collection-level operations (e.g.
|
||||
<link xlink:href="&url.wiki.crud;">CRUD</link> methods, index management).
|
||||
Various Collection methods have been renamed for clarity, and to be in
|
||||
accordance with a new language-agnostic
|
||||
<link xlink:href="&url.mongodb.crud;">specification</link>.
|
||||
</para>
|
||||
|
||||
|
@ -100,60 +101,60 @@ Generating autoload files
|
|||
As an example, this is how you insert a document into the
|
||||
<emphasis>beers</emphasis> collection of the <emphasis>demo</emphasis>
|
||||
database:
|
||||
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
require 'vendor/autoload.php'; // include Composer goodies
|
||||
<?php
|
||||
require 'vendor/autoload.php'; // include Composer goodies
|
||||
|
||||
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
|
||||
$collection = new MongoDB\Collection($manager, "demo.beers");
|
||||
$client = new MongoDB\Driver\Client("mongodb://localhost:27017");
|
||||
$collection = $client->demo->beers;
|
||||
|
||||
$result = $collection->insertOne( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
|
||||
$result = $collection->insertOne( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
|
||||
|
||||
echo "Inserted with Object ID '{$result->getInsertedId()}'";
|
||||
?>
|
||||
echo "Inserted with Object ID '{$result->getInsertedId()}'";
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
Instead of the original document being modified to add the newly generated
|
||||
<code>_id</code> field, this is now part of the result that comes back
|
||||
from the <code>insertOne</code> method.
|
||||
Instead of injecting the generated <code>_id</code> field into the input
|
||||
document (as was done in the old driver), it is now made available through
|
||||
the result object returned by the <code>insertOne</code> method.
|
||||
</para>
|
||||
|
||||
<para>
|
||||
After insertion, you can of course also query the data that you have just
|
||||
inserted. For that, you use the <code>find</code> method which returns a
|
||||
cursor that you can iterate over:
|
||||
|
||||
inserted. For that, you use the <code>find</code> method, which returns an
|
||||
iterable cursor:
|
||||
<programlisting role="php">
|
||||
<![CDATA[
|
||||
<?php
|
||||
require 'vendor/autoload.php'; // include Composer goodies
|
||||
<?php
|
||||
require 'vendor/autoload.php'; // include Composer goodies
|
||||
|
||||
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
|
||||
$collection = new MongoDB\Collection($manager, "demo.beers");
|
||||
$client = new MongoDB\Driver\Client("mongodb://localhost:27017");
|
||||
$collection = $client->demo->beers;
|
||||
|
||||
$result = $collection->find( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
|
||||
$result = $collection->find( [ 'name' => 'Hinterland', 'brewery' => 'BrewDog' ] );
|
||||
|
||||
foreach ($result as $entry)
|
||||
{
|
||||
echo $entry->_id, ': ', $entry->name, "\n";
|
||||
}
|
||||
?>
|
||||
foreach ($result as $entry) {
|
||||
echo $entry['_id'], ': ', $entry['name'], "\n";
|
||||
}
|
||||
?>
|
||||
]]>
|
||||
</programlisting>
|
||||
</para>
|
||||
|
||||
<para>
|
||||
You might have noticed that instead of accessing the <code>_id</code> and
|
||||
<code>name</code> fields is no longer done through an array access
|
||||
operator. Instead, they are now properties of a <code>stdClass</code>
|
||||
object. You can find more information on how serialisation and
|
||||
deserialisation between PHP variables and the BSON stored in MongoDB in
|
||||
the <xref linkend="mongodb.persistence"/> specification.
|
||||
While it may not be apparent in the examples, BSON documents and arrays are
|
||||
unserialized as type classes in the library by default. These classes ensure
|
||||
that values preserve their type when being serialized back into BSON, which
|
||||
avoids a caveat in the old driver where arrays might turn into documents,
|
||||
and vice versa. Additionally, the classes extend
|
||||
<classname>ArrayObject</classname> for enhanced usability. You can find more
|
||||
information on how serialization and deserialization between PHP variables
|
||||
and BSON is handled by the driver and library by reading the
|
||||
<xref linkend="mongodb.persistence"/> specification.
|
||||
</para>
|
||||
</section>
|
||||
</chapter>
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- $Revision: 336263 $ -->
|
||||
|
||||
<section xml:id="mongodb.tutorial.install.php" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||||
<title>Installation with PHP</title>
|
||||
|
||||
<para></para>
|
||||
|
||||
<section>
|
||||
<title>Further reading</title>
|
||||
|
||||
<para>
|
||||
Continue this tutorial by jumping to <xref
|
||||
linkend="mongodb.tutorial.library"/>
|
||||
</para>
|
||||
</section>
|
||||
</section>
|
||||
<!-- 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
|
||||
-->
|
Loading…
Reference in a new issue