We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

MongoDb php7 Collections Call to undefined method MongoDB\Driver\Cursor::rewind()

I would like to upgrade to php7 phalcon.

One issue that I have is that my Collections are hooked up to the old Mongo driver which is now deprecated and cannot be used in php7.

MongoDB driver (mongodb-1.1) is a backwards compatible vendor specific database extension (https://php.net/manual/en/set.mongodb.php) and my hope is that a minimal refactor in php 5.6.x will allow me to upgrade to php7.

So that my refactor is minimal I wouls like to use this Php library: https://github.com/mongodb/mongo-php-library whic is installed using composer:

composer require "mongodb/mongodb=^1.0.0"

Services using official wrapper class:

$di->set('mongo', function() use ($config) {
    $mongo = new \MongoDB\Client();
    return $mongo->selectDatabase($config->mongodb->dbname);
}, true);

$di->set('collectionManager', function() {
    return new Phalcon\Mvc\Collection\Manager();
}, true);

So far I have only had to wrap my aggregate query in an array.

However I am having a problem with ::findFirst() in php 5.6 phalcon 3.0.1:

In my collection class::

        $result = self::findFirst([
            ["item_id" => intval($item_id)]
        ])

The error I am getting is:

[10-Nov-2016 13:41:05 Europe/London] Call to undefined method MongoDB\Driver\Cursor::rewind()
#0 [internal function]: Phalcon\Mvc\Collection::_getResultset(Array, Object(Common\Models\ItemRatingStats), Object(MongoDB\Database), true)
#1 ...apps/common/models/ItemRatingStats.php(56): Phalcon\Mvc\Collection::findFirst(Array)

Could anyone help or comment.

I would like to use this vendor wrapper driver class as it offers backwards compatability to php7 from php5.6.x.

It comes recommended from MongoDB and is a favored Vendor Specific Database Extension.



47.7k
edited Nov '16

Now trying with https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Db/Adapter#mongodbclient

Installed Phalcon Incubator with:

composer require "phalcon/incubator=~3.0"

In services:

$di->setShared('mongo', function() use ($config) {

    $mongo = new \Phalcon\Db\Adapter\MongoDB\Client();
    return $mongo->selectDatabase($config->mongodb->dbname);
});

Then the collection extends \Phalcon\Mvc\MongoCollection:

class ItemRatingStats extends \Phalcon\Mvc\MongoCollection
{
...
]
  • Had to wrap aggregate query in another array.