Skip to content

Commit f1d053a

Browse files
committed
PHPC-267; _id generated on embedded document
1 parent d64698a commit f1d053a

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/bson.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ PHONGO_API void zval_to_bson(zval *data, php_phongo_bson_flags_t flags, bson_t *
837837
key_len = bson_uint32_to_string(index, (const char **)&key, numbuf, sizeof(numbuf));
838838
}
839839

840-
phongo_bson_append(bson, flags, key, key_len, Z_TYPE_PP(entry), *entry TSRMLS_CC);
840+
phongo_bson_append(bson, flags & ~PHONGO_BSON_ADD_ID, key, key_len, Z_TYPE_PP(entry), *entry TSRMLS_CC);
841841
}
842842

843843
if (flags & PHONGO_BSON_ADD_ID) {
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
--TEST--
2+
_id should only be generated for top-level document, not embedded docs
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; CLEANUP(STANDALONE) ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$manager = new MongoDB\Driver\Manager(STANDALONE);
10+
11+
$manager->executeInsert(NS, array('_id' => 1, 'x' => 1));
12+
$manager->executeInsert(NS, array('_id' => 2, 'x' => 1));
13+
14+
$user = array(
15+
"username" => "bob",
16+
"address" => array(
17+
"street" => "Main St.",
18+
"city" => "New York",
19+
),
20+
);
21+
22+
$bulk = new MongoDB\Driver\BulkWrite();
23+
$user["_id"] = $bulk->insert($user);
24+
$result = $manager->executeBulkWrite(NS, $bulk);
25+
26+
echo "Dumping inserted user document with injected _id:\n";
27+
var_dump($user);
28+
29+
$cursor = $manager->executeQuery(NS, new MongoDB\Driver\Query(array("_id" => $user["_id"])));
30+
31+
echo "\nDumping fetched user document:\n";
32+
$array = $cursor->toArray();
33+
var_dump($array[0]);
34+
35+
?>
36+
===DONE===
37+
<?php exit(0); ?>
38+
--EXPECTF--
39+
Dumping inserted user document with injected _id:
40+
array(3) {
41+
["username"]=>
42+
string(3) "bob"
43+
["address"]=>
44+
array(2) {
45+
["street"]=>
46+
string(8) "Main St."
47+
["city"]=>
48+
string(8) "New York"
49+
}
50+
["_id"]=>
51+
object(BSON\ObjectID)#%d (%d) {
52+
["oid"]=>
53+
string(24) "%s"
54+
}
55+
}
56+
57+
Dumping fetched user document:
58+
array(3) {
59+
["_id"]=>
60+
object(BSON\ObjectID)#%d (%d) {
61+
["oid"]=>
62+
string(24) "%s"
63+
}
64+
["username"]=>
65+
string(3) "bob"
66+
["address"]=>
67+
object(stdClass)#%d (%d) {
68+
["street"]=>
69+
string(8) "Main St."
70+
["city"]=>
71+
string(8) "New York"
72+
}
73+
}
74+
===DONE===

0 commit comments

Comments
 (0)