Skip to content

Commit bccdd27

Browse files
committed
PHP-1294: Emtpy Query Result should still return QueryResult
1 parent 183286a commit bccdd27

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

php_phongo.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ void phongo_result_init(zval *return_value, zend_class_entry *result_class, mong
219219
} else {
220220
result->hint = server_hint;
221221
}
222-
/* FIXME: Support empty result cursor. eg. when there simply aren't any matches for a query */
223-
result->firstBatch = bson_copy(bson);
222+
result->firstBatch = bson ? bson_copy(bson) : NULL;
224223
} /* }}} */
225224

226225
void phongo_server_init(zval *return_value, int hint, mongoc_host_list_t *host TSRMLS_DC) /* {{{ */
@@ -580,14 +579,13 @@ int phongo_execute_query(mongoc_client_t *client, char *namespace, php_phongo_qu
580579
if (!mongoc_cursor_next(cursor, &doc)) {
581580
bson_error_t error;
582581

582+
/* Could simply be no docs, which is not an error */
583583
if (mongoc_cursor_error(cursor, &error)) {
584584
phongo_throw_exception_from_bson_error_t(&error TSRMLS_CC);
585585
mongoc_cursor_destroy(cursor);
586586
return false;
587587
}
588588

589-
mongoc_cursor_destroy(cursor);
590-
return false;
591589
}
592590

593591
if (!return_value_used) {

tests/standalone/manager-executeDelete-002.phpt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,7 @@ printWriteResult($result);
2020
echo "\n===> Collection\n";
2121
$cursor = $manager->executeQuery(NS, new MongoDB\Query(array()));
2222

23-
/* FIXME: Returns NULL, not empty iterator when nothing matches */
24-
if ($cursor) {
25-
var_dump(iterator_to_array($cursor));
26-
} else {
27-
var_dump(array());
28-
}
23+
var_dump(iterator_to_array($cursor));
2924

3025
?>
3126
===DONE===
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
--TEST--
2+
MongoDB\Manager::executeDelete() multiple documents
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc" ?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$manager = new MongoDB\Manager(MONGODB_URI);
10+
11+
$manager->executeInsert(NS, array('_id' => 1, 'x' => 1));
12+
$manager->executeInsert(NS, array('_id' => 2, 'x' => 1));
13+
14+
$cursor = $manager->executeQuery(NS, new MongoDB\Query(array("x" => 2)));
15+
16+
var_dump(iterator_to_array($cursor));
17+
18+
?>
19+
===DONE===
20+
<?php exit(0); ?>
21+
--EXPECT--
22+
array(0) {
23+
}
24+
===DONE===

0 commit comments

Comments
 (0)