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

Is there a MongoDB backward compatibility break with Phalcon 3 + PHP 7 ?

Hi,

I currently work on a project with Phalcon + PHP + MongoDB.

I started the project on Ubuntu 14.04 with Phalcon 2.0.x and PHP 5.5, and since I migrated to Ubuntu 16.04 + Phalcon 3 + PHP 7 I have some issues when I try to make ODM work.

The main reason is that MongoDB has changed its API making new API not backward compatible with the old one, as told in this post : https://stackoverflow.com/questions/34486808/installing-the-php-7-mongodb-client-driver

Then I tried to install the Mongo Client class from composer, hoping it will solve my problem :

composer require "mongodb/mongodb=^1.0.0"

then use it in service initialization :

$di->setShared('mongo', function () use ($config) {
    $client = new MongoDB\Client();
    $db = $client->selectDatabase($config->mongo_db);

    //...

    return $db;
});

But it's not solve the issue. When I try a findFirst on a collection I can see this error message :

Call to undefined method ::rewind()
#0 [internal function]: Phalcon\Mvc\Collection::_getResultset(Array, Object(Urls), Object(MongoDB\Database), true)
#1 .../app/tasks/ManagerTask.php(25): Phalcon\Mvc\Collection::findFirst(Array)
etc..

What do you think about that ? What should I do if I need to use collection with PHP 7 (and so with new Mongo DB library) ?

Thanks for your upcoming answers :)



131

Sorry for the late response Mikachou. I just ran into the same error as you, although our situations seem a bit different. In my case, I was using the Phalcon Incubator compatibility class, and got the error from using the wrong base class for my models. Maybe you can try using the Incubator wrapper class instead of the one you mentioned. Or, maybe you can keep using the same class, but extend your models from a different base class - well, hopefully you fixed your bug at some stage in the past months.

More info on my situation in this thread: https://forum.phalcon.io/discussion/14885/mongodb-support-in-phalcon-3-php-7#C48123



4.0k

I'm sorry for my late answer too :)

But in fact it's not serious, because I gave up using MongoDB a few time after the beginning of my project :)