Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
}
like image 894
user1359256 Avatar asked Dec 21 '25 04:12

user1359256


1 Answers

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());
like image 129
TiTanium Avatar answered Dec 22 '25 19:12

TiTanium



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!