MongoDB support in Phalcon 3 + PHP 7

Dear Team,

When could we expect MongoDB support in Phalcon 3 + PHP7? I read the other discussions but I could not find any date or current status.

Thanks in advance.

It's already supported in incubator. There is a problem that it could be only done using new class or some hacky stuff. I guess in phalcon 4 we will drop php 5 support and then just switch to proper mongo classes and update code.

it's 3.x now, will version 4.x be available soon?, by when? Until then could you please give some examples of using the MongoDB support in Incubator?



70

Considering how difficult it was to find information on the internet, let me share my experience. First of all, Sanketh, I feel your pain. The lack of compatibility between the technologies involved (PHP 5->7, Phalcon 2->3, MongoClient->MongoDB) made this a painful and confusing process. MongoClient is not compatible with PHP7, MongoDB is not backward-compatible, the doc of the incubator class recommended by Serghei specifically points to Phalcon 2.1. Some other article said that Phalcon 2.1 broke compatibility and would thus be renamed 3.0 though, so that might be a technically accurate yet misleading documentation. Anyway, the incubator class pointed out by Serghei seemed to do the trick for me.

Add the Incubator library - in my case, I ran the following Bash command in the library folder:

$ git clone -b 3.1.x http://github.com/phalcon/incubator.git

Register the Incubator in the class autoloader - in my case, I added one line in the bootstrap:

$loader->registerNamespaces([
    /* other libs */
    'Phalcon' => BASE_DIR . '/' . $config->application->librariesDir . 'incubator/Library/Phalcon/'
] )->register();

Replace the old Mongo library with the Incubator one - in my case, in the $config['di']->set('mongo') function:

$mongoClient = new \Phalcon\Db\Adapter\MongoDB\Client(); // was: new \MongoClient();
return $mongoClient->selectDatabase($config->database->name); // unchanged

That got me a "Call to undefined method rewind" until I changed my models from extends \Phalcon\Mvc\Collection to extends \Phalcon\Mvc\MongoCollection. With that, my app seemed to work as normal.

I hope this will be useful to someone.

Is new model class from the incubator 100% compatible with old one? They look similar but I couldn't find no official remark if new class is compatible and simple changing of parent class won't break anything up.

Considering how difficult it was to find information on the internet, let me share my experience. First of all, Sanketh, I feel your pain. The lack of compatibility between the technologies involved (PHP 5->7, Phalcon 2->3, MongoClient->MongoDB) made this a painful and confusing process. MongoClient is not compatible with PHP7, MongoDB is not backward-compatible, the doc of the incubator class recommended by Serghei specifically points to Phalcon 2.1. Some other article said that Phalcon 2.1 broke compatibility and would thus be renamed 3.0 though, so that might be a technically accurate yet misleading documentation. Anyway, the incubator class pointed out by Serghei seemed to do the trick for me.

Add the Incubator library - in my case, I ran the following Bash command in the library folder:

$ git clone -b 3.1.x http://github.com/phalcon/incubator.git

Register the Incubator in the class autoloader - in my case, I added one line in the bootstrap:

$loader->registerNamespaces([
  /* other libs */
  'Phalcon' => BASE_DIR . '/' . $config->application->librariesDir . 'incubator/Library/Phalcon/'
] )->register();

Replace the old Mongo library with the Incubator one - in my case, in the $config['di']->set('mongo') function:

$mongoClient = new \Phalcon\Db\Adapter\MongoDB\Client(); // was: new \MongoClient();
return $mongoClient->selectDatabase($config->database->name); // unchanged

That got me a "Call to undefined method rewind" until I changed my models from extends \Phalcon\Mvc\Collection to extends \Phalcon\Mvc\MongoCollection. With that, my app seemed to work as normal.

I hope this will be useful to someone.

What you mean compatible? You obviously new need extension and to use mongo client from incubator too, then it should be fine.