发布
社区首页 >问答首页 >致命错误:调用未定义的方法MongoCursor::toArray()

致命错误:调用未定义的方法MongoCursor::toArray()
EN

Stack Overflow用户
提问于 2017-03-29 19:10:27
回答 1查看 1.2K关注 0票数 0

我想把一个游标转换成数组,这样我就可以看到游标的结果,所以当我使用mongodb的toArray时,会显示这个错误

“致命错误:调用未定义的方法MongoCursor::toArray()”

下面是我的代码:

代码语言:javascript
代码运行次数:0
复制
$getting_started_collection = $this->getServiceLocator()->get('Common\Collection\ResourcesGettingStarted');
$criteria = array(
    '$or' => array(
        array('affiliate_type' => 'cpl_cpm'), 
        array('affiliate_type' => 'cpl')
    )
);
$columns = array(
    '_id' => true,
    'title' => true,
    'description' => true,
    'logo' => true,
    'pdf' => true
);
$cursor = $getting_started_collection->fetchAll($criteria, $columns, true);
$data_array = $cursor->toArray();
echo("<pre>");
print_r($data_array);
die();

我是如何使用https://docs.mongodb.com/manual/reference/method/cursor.toArray/的?

EN

回答 1

Stack Overflow用户

发布于 2017-03-29 19:34:42

这是因为MongoCursor类没有名为toArray的方法。下面是所有可用方法-- MongoCursor的列表。

您应该像手册中的Example #1一样使用iterator_to_array()

代码语言:javascript
代码运行次数:0
复制
<?php

$cursor = $collection->find();
var_dump(iterator_to_array($cursor));

?>

来源:http://php.net/manual/en/class.mongocursor.php

在您的示例中:

代码语言:javascript
代码运行次数:0
复制
$cursor = $getting_started_collection->fetchAll($criteria, $columns, true);
$data_array = iterator_to_array($cursor);
echo("<pre>");
print_r($data_array);
die();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43091869

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档