Просмотр исходного кода

Convert legacy types in query for distinct calls

Andreas Braun 9 лет назад
Родитель
Сommit
e95c899548

+ 3 - 0
CHANGELOG-1.0.md

@@ -13,6 +13,9 @@ milestone.
  * [#85](https://github.com/alcaeus/mongo-php-adapter/pull/85) fixes calls to
  `MongoCollection::count` using the legacy syntax of providing `skip` and `limit`
  arguments instead of an `options` array.
+ * [#88](https://github.com/alcaeus/mongo-php-adapter/pull/88) fixes an error
+ where a call to `MongoCollection::distinct` with a query did not convert legacy
+ BSON types to the new driver types.
 
 
 1.0.0 (2016-03-18)

+ 1 - 1
lib/Mongo/MongoCollection.php

@@ -476,7 +476,7 @@ class MongoCollection
     public function distinct($key, array $query = [])
     {
         try {
-            return array_map([TypeConverter::class, 'toLegacy'], $this->collection->distinct($key, $query));
+            return array_map([TypeConverter::class, 'toLegacy'], $this->collection->distinct($key, TypeConverter::fromLegacy($query)));
         } catch (\MongoDB\Driver\Exception\Exception $e) {
             return false;
         }

+ 26 - 0
tests/Alcaeus/MongoDbAdapter/Mongo/MongoCollectionTest.php

@@ -546,6 +546,32 @@ class MongoCollectionTest extends TestCase
         $this->assertEquals(['bar'], $values);
     }
 
+    public function testDistinctWithIdQuery()
+    {
+        $document1 = ['foo' => 'bar'];
+        $document2 = ['foo' => 'bar'];
+        $document3 = ['foo' => 'foo'];
+
+        $collection = $this->getCollection();
+        $collection->insert($document1);
+        $collection->insert($document2);
+        $collection->insert($document3);
+
+        $this->assertSame(
+            ['bar'],
+            $collection->distinct('foo', ['_id' => [
+                '$in' => [$document1['_id'], $document2['_id']]
+            ]])
+        );
+
+        $this->assertEquals(
+            ['bar', 'foo'],
+            $collection->distinct('foo', ['_id' => [
+                '$in' => [$document1['_id'], $document3['_id']]
+            ]])
+        );
+    }
+
     public function testAggregate()
     {
         $collection = $this->getCollection();