简体   繁体   中英

Doctrine MongoDB Bundle: Cursor toArray() Error When “_id” is an Array

Resulting from a MapReduce, I have a MongoDb collection that has the following structure:

{ "_id" : { "id" : NumberLong(1), "date" : "04-26-2012" }, "value" : { "count" : 100 } }

In my controller I am doing the following to return an array to display the results:

$mongoDb         = $mongo->selectDatabase($dbname);        
$mongoCollection = $mongoDb->selectCollection($collname);
$qb              = $mongoCollection->createQueryBuilder(); 
$qb              = $qb->find();        
$resultCursor    = $qb->getQuery()->execute();
                                  ->limit(10);
$resultArray     = $resultCursor  ->toArray();

However, I get an exception: "Notice: Array to string conversion in vendor/doctrine-mongodb/lib/Doctrine/MongoDB/Cursor.php line 154"

Below is line 154 of Cursor.php. Does MongoCursor::key not handle "_id" as an Array?

/** @proxy */
public function key()
{
    return $this->mongoCursor->key();
}

For fast converting mongoDb cursor to array you may use http://php.net/manual/en/function.iterator-to-array.php

$qb = $this->createQueryBuilder();

$qb->hydrate(false);

$query = $qb->getQuery();

$resultArray = iterator_to_array($query->execute());

key() always returns a string (see http://php.net/manual/en/class.iterator.php ), so it's generating that notice trying to convert an array into string form. It is only a notice, though, it should still work.

The easiest way around this is probably just not to call toArray() on the cursor: iterate through it instead ( foreach $resultCursor as $value) ... ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM