Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit 58c2081

Browse files
committed
Merge pull request #845
2 parents 9ace894 + 9afd144 commit 58c2081

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

collection.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2733,11 +2733,12 @@ PHP_METHOD(MongoCollection, distinct)
27332733
{
27342734
char *key;
27352735
int key_len;
2736-
zval *cmd, **values, *tmp, *query = NULL;
2736+
HashTable *query = NULL;
2737+
zval *cmd, **values, *tmp, *zquery = NULL;
27372738
mongo_collection *c;
27382739
mongo_db *db;
27392740

2740-
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|a!", &key, &key_len, &query) == FAILURE) {
2741+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|H!", &key, &key_len, &query) == FAILURE) {
27412742
return;
27422743
}
27432744

@@ -2752,9 +2753,11 @@ PHP_METHOD(MongoCollection, distinct)
27522753
zval_add_ref(&c->name);
27532754
add_assoc_stringl(cmd, "key", key, key_len, 1);
27542755

2755-
if (query) {
2756-
add_assoc_zval(cmd, "query", query);
2757-
zval_add_ref(&query);
2756+
if (query && zend_hash_num_elements(query) > 0) {
2757+
MAKE_STD_ZVAL(zquery);
2758+
array_init(zquery);
2759+
zend_hash_copy(HASH_P(zquery), query, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
2760+
add_assoc_zval(cmd, "query", zquery);
27582761
}
27592762

27602763
tmp = php_mongo_runcommand(c->link, &c->read_pref, Z_STRVAL_P(db->name), Z_STRLEN_P(db->name), cmd, NULL, 0, NULL TSRMLS_CC);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
MongoCollection::distinct()
3+
--SKIPIF--
4+
<?php require_once "tests/utils/standalone.inc"; ?>
5+
--FILE--
6+
<?php require_once "tests/utils/server.inc"; ?>
7+
<?php
8+
9+
$host = MongoShellServer::getStandaloneInfo();
10+
$m = new MongoClient($host);
11+
$c = $m->selectCollection(dbname(), collname(__FILE__));
12+
$c->drop();
13+
14+
$c->insert(array('x' => 1));
15+
$c->insert(array('x' => 2));
16+
$c->insert(array('x' => 2));
17+
$c->insert(array('x' => 3));
18+
19+
printf("Distinct x values: %s\n", json_encode($c->distinct('x')));
20+
printf("Distinct x values where x >= 2: %s\n", json_encode($c->distinct('x', array('x' => array('$gte' => 2)))));
21+
22+
?>
23+
===DONE===
24+
--EXPECT--
25+
Distinct x values: [1,2,3]
26+
Distinct x values where x >= 2: [2,3]
27+
===DONE===
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
MongoCollection::distinct() requires query to be a hash
3+
--SKIPIF--
4+
<?php require_once "tests/utils/standalone.inc"; ?>
5+
--FILE--
6+
<?php require_once "tests/utils/server.inc"; ?>
7+
<?php
8+
9+
$host = MongoShellServer::getStandaloneInfo();
10+
$m = new MongoClient($host);
11+
$c = $m->selectCollection(dbname(), collname(__FILE__));
12+
$c->drop();
13+
14+
$c->insert(array('x' => 1));
15+
$c->insert(array('x' => 2));
16+
$c->insert(array('x' => 2));
17+
$c->insert(array('x' => 3));
18+
19+
echo json_encode($c->distinct('x', array())), "\n";
20+
echo json_encode($c->distinct('x', new stdClass)), "\n";
21+
echo json_encode($c->distinct('x', 1)), "\n";
22+
23+
?>
24+
===DONE===
25+
--EXPECTF--
26+
[1,2,3]
27+
[1,2,3]
28+
29+
Warning: MongoCollection::distinct() expects parameter 2 to be array, integer given in %s on line %d
30+
null
31+
===DONE===

0 commit comments

Comments
 (0)