php-doc-en/reference/mongo/mongocollection/insert.xml
Kristina Chodorow 13d9a87041 added safe options
git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@296280 c90b9560-bf6c-de11-be94-00142212c4b1
2010-03-16 15:15:54 +00:00

194 lines
4.3 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<refentry xml:id="mongocollection.insert" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
<refnamediv>
<refname>MongoCollection::insert</refname>
<refpurpose>Inserts an array into the collection</refpurpose>
</refnamediv>
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<modifier>public</modifier> <type>mixed</type><methodname>MongoCollection::insert</methodname>
<methodparam><type>array</type><parameter>a</parameter></methodparam>
<methodparam choice="opt"><type>array</type><parameter>options</parameter><initializer>array()</initializer></methodparam>
</methodsynopsis>
</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
<para>
<variablelist>
<varlistentry>
<term>
<parameter>a</parameter>
</term>
<listitem>
<para>
An array.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<parameter>options</parameter>
</term>
<listitem>
<para>
Options for the insert.
<itemizedlist>
<listitem>
<para>
<literal>"safe"</literal>
</para>
<para>
Check that the insert succeeded.
</para>
</listitem>
</itemizedlist>
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role="returnvalues">
&reftitle.returnvalues;
<para>
If <parameter>safe</parameter> was set, returns an array containing the status of the insert.
Otherwise, returns a boolean representing if the array was not empty (an empty array will not
be inserted).
</para>
</refsect1>
<refsect1 role="errors">
&reftitle.errors;
<para>
Throws MongoCursorException if the "safe" option is set and the insert fails. (Version 1.0.1+)
</para>
</refsect1>
<refsect1 role="changelog">
&reftitle.changelog;
<para>
<informaltable>
<tgroup cols="2">
<thead>
<row>
<entry>&Version;</entry>
<entry>&Description;</entry>
</row>
</thead>
<tbody>
<row>
<entry>1.0.5</entry>
<entry>
Changed second parameter to an array of options. Pre-1.0.5, the
second parameter was a boolean indicating the "safe" option.
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>MongoCollection::insert</function> _id example</title>
<para>
Inserting an object will add an _id field to it, unless it is passed by reference.
</para>
<programlisting role="php">
<![CDATA[
<?php
$a = array('x' => 1);
$collection->insert($a);
var_dump($a)
$b = array('x' => 1);
$ref = &$b;
$collection->insert($ref);
var_dump($ref);
?>
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
array(2) {
["x"]=>
int(1)
["_id"]=>
object(MongoId)#4 (0) {
}
}
array(1) {
["x"]=>
int(1)
}
]]>
</screen>
</example>
<example>
<title><function>MongoCollection::insert</function> safe example</title>
<para>
This example shows inserting two elements with the same _id, which causes
a <classname>MongoCursorException</classname> to be thrown, as
<parameter>safe</parameter> was set.
</para>
<programlisting role="php">
<![CDATA[
<?php
$person = array("name" => "Joe", "age" => 20);
$collection->insert($person, true);
// now $person has an _id field, so if we save it
// again, we will get an exception
try {
$collection->insert($person, true);
}
catch(MongoCursorException $e) {
echo "Can't save the same person twice!\n";
}
?>
]]>
</programlisting>
</example>
</refsect1>
<refsect1 role="seealso">
&reftitle.seealso;
<para>
MongoDB core docs on <link xlink:href="&url.mongodb.dochub.insert;">insert</link>.
</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
-->