Skip to content

Commit 4b5a0a1

Browse files
committed
PHPLIB-530: Fix missing ns entry for idIndex in collection info
1 parent a1af358 commit 4b5a0a1

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

src/Model/CollectionInfoCommandIterator.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace MongoDB\Model;
1919

2020
use IteratorIterator;
21+
use Traversable;
2122

2223
/**
2324
* CollectionInfoIterator for listCollections command results.
@@ -32,6 +33,19 @@
3233
*/
3334
class CollectionInfoCommandIterator extends IteratorIterator implements CollectionInfoIterator
3435
{
36+
/** @var string|null */
37+
private $databaseName;
38+
39+
/**
40+
* @param string|null $databaseName
41+
*/
42+
public function __construct(Traversable $iterator, $databaseName = null)
43+
{
44+
parent::__construct($iterator);
45+
46+
$this->databaseName = $databaseName;
47+
}
48+
3549
/**
3650
* Return the current element as a CollectionInfo instance.
3751
*
@@ -41,6 +55,12 @@ class CollectionInfoCommandIterator extends IteratorIterator implements Collecti
4155
*/
4256
public function current()
4357
{
44-
return new CollectionInfo(parent::current());
58+
$info = parent::current();
59+
60+
if ($this->databaseName !== null && isset($info['idIndex']) && ! isset($info['idIndex']['ns'])) {
61+
$info['idIndex']['ns'] = $this->databaseName . '.' . $info['name'];
62+
}
63+
64+
return new CollectionInfo($info);
4565
}
4666
}

src/Operation/ListCollections.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,6 @@ private function executeCommand(Server $server)
136136
$cursor = $server->executeReadCommand($this->databaseName, new Command($cmd), $this->createOptions());
137137
$cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
138138

139-
return new CollectionInfoCommandIterator(new CachingIterator($cursor));
139+
return new CollectionInfoCommandIterator(new CachingIterator($cursor), $this->databaseName);
140140
}
141141
}

0 commit comments

Comments
 (0)