简体   繁体   中英

PHP 7 with Doctrine MongoDB ODM

I have a Symfony 2 application that is using Doctrine MongoDB ODM and I'm trying to get this running with PHP 7.

I have installed PHP 7 successfully but installing dependencies through Composer is giving me grief with the following error:

doctrine/mongodb 1.0.x-dev requires ext-mongo >=1.2.12,<1.7-dev -> the requested PHP extension mongo is missing from your system.

I managed to install the PHP 7 mongo extension through apt:

apt-get install php7.0-mongo

Just to be sure, I have also installed the mongodb extension through PECL:

sudo apt-get install -y php-pear php7.0-dev libcurl3-openssl-dev
sudo pecl install mongodb

However, I'm still get the error that the mongo extension cannot be found. There seems to be a version discrepancy in that ext-mongo >=1.2.12 is required but only 1.1.6 was installed::

$ php -i | grep mongo
/etc/php/7.0/cli/conf.d/20-mongodb.ini,
mongodb
mongodb support => enabled
mongodb version => 1.1.6
mongodb stability => stable
libmongoc version => 1.3.5
mongodb.debug => no value => no value

What is the correct way to install the mongo extension required by Doctrine ODM for PHP 7?

On PHP7 you cannot install mongo extension. What you can do, is to install https://github.com/alcaeus/mongo-php-adapter first, then install doctrine.

composer require alcaeus/mongo-php-adapter

in composer.json I added

"provide" : {
  "ext-mongo": "*"
},

before "require" and this forces to install latest extension

In order to use Symfony with MongoDB you need:

  • Symfony : obviously :-) ;
  • Symfony Doctrine MongoDB Bundle (doctrine/mongodb-odm-bundle): this bundle integrates the Doctrine2 MongoDB Object Document Mapper (ODM) library into Symfony;
  • Doctrine MongoDB Object Document Mapper (doctrine/mongodb-odm): library that provides a PHP object mapping functionality for MongoDB;
  • MongoDB Adapter (alcaeus/mongo-php-adapter): It provides the API of ext-mongo built on top of mongo-php-library, thus being compatible with PHP 7;
  • MongoDB driver library (mongodb/mongodb): provides a high-level abstraction around the lower-level drivers for PHP;
  • MongoDB PHP extension: low level driver extension for PHP and HHVM. Please pay attention on the MongoDB driver. Don't use the legacy MongoDB driver ( http://php.net/manual/en/book.mongo.php ).

You can follow the instructions here: "How to install Symfony3 with MongoDB"

It was written for Symfony3 but for Symfony2 is the same issue. The problem was PHP7 + mongodb driver + Doctrine.

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