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

时间:2017-03-29 11:10:27

标签: php mongodb zend-framework2 toarray php-mongodb

我想将光标转换为数组,所以我看到了光标结果,为此,当我使用amray of mongodb然后显示此错误

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

这是我的代码:

$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/

1 个答案:

答案 0 :(得分:2)

那是因为MongoCursor类没有名为toArray的方法。以下是所有可用方法的列表 - MongoCursor 您应该在iterator_to_array()手册中使用Example #1

<?php

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

?>

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

在你的例子中:

$cursor = $getting_started_collection->fetchAll($criteria, $columns, true);
$data_array = iterator_to_array($cursor);
echo("<pre>");
print_r($data_array);
die();