Phalcon\Db\Adapter\MongoDB\Operation\ListDatabases::execute PHP Метод

execute() публичный Метод

Execute the operation.
См. также: Executable::execute()
public execute ( MongoDB\Driver\Server $server ) : Phalcon\Db\Adapter\MongoDB\Model\DatabaseInfoIterator
$server MongoDB\Driver\Server
Результат Phalcon\Db\Adapter\MongoDB\Model\DatabaseInfoIterator
    public function execute(Server $server)
    {
        $cmd = ['listDatabases' => 1];
        if (isset($this->options['maxTimeMS'])) {
            $cmd['maxTimeMS'] = $this->options['maxTimeMS'];
        }
        $cursor = $server->executeCommand('admin', new Command($cmd));
        $cursor->setTypeMap(['root' => 'array', 'document' => 'array']);
        $result = current($cursor->toArray());
        if (!isset($result['databases']) || !is_array($result['databases'])) {
            throw new UnexpectedValueException('listDatabases command did not return a "databases" array');
        }
        /* Return an Iterator instead of an array in case listDatabases is
         * eventually changed to return a command cursor, like the collection
         * and index enumeration commands. This makes the "totalSize" command
         * field inaccessible, but users can manually invoke the command if they
         * need that value.
         */
        return new DatabaseInfoLegacyIterator($result['databases']);
    }

Usage Example

Пример #1
0
 /**
  * List databases.
  *
  * @see ListDatabases::__construct() for supported options
  * @return DatabaseInfoIterator
  */
 public function listDatabases(array $options = [])
 {
     $operation = new ListDatabases($options);
     $server = $this->manager->selectServer(new ReadPreference(ReadPreference::RP_PRIMARY));
     return $operation->execute($server);
 }