升级PHP7 jenssegers/mongodb expected to be a reference

pecl上的mongodb扩展有两个,都是官方出品的,一个叫mongo,一个是mongodb,前者已经被官方废弃,不再提供稳定的更新,官方推荐使用后者,并且后者是支持php7的。在pecl上搜索mongodb即可找到,也可以通过pecl install mongodb安装。mongo和mongodb的结构完全不同,使用方式也大相径庭,不过官方有个php适配包,可以让mongodb看上去和mongo使用体验近似
https://github.com/mongodb/mo…

以上来自这个问题

目前用的 laravel-mongodb

WARNING: The old mongo PHP driver is not supported anymore in versions >= 3.0.

因为现在用的2.1 版本,而升级PHP7后用的mongodb 扩展,不兼容之前的mongo扩展,所以现在有2个选择:

1.升级到3.0版本。
2.使用 https://github.com/alcaeus/mo… 作为中间件,不用修改现有代码。

第二个方案来自 http://php.net/manual/zh/set….
There is an adapter – so old MongoClient / MongoDB code will run on the new PHP7/Mongo mess
https://github.com/alcaeus/mo…

第一个方案就不说了,现在说第二个。

composer config "platform.ext-mongo" "1.6.16" && composer require alcaeus/mongo-php-adapter

然后测试下

Message::create([ 'title'=>'标题','content'=>'内容22','time'=>time()]);
提示:

ErrorException in Collection.php line 42:
Parameter 1 to MongoCollection::insert() expected to be a reference, value given

有问题怎么办?当然搜索啊,于是 Google ,不得不说谷歌牛逼,第一条结果就找到了 https://github.com/alcaeus/mo…

《升级PHP7 jenssegers/mongodb expected to be a reference》

再看看百度

《升级PHP7 jenssegers/mongodb expected to be a reference》

翻不了bing.com 也行啊

《升级PHP7 jenssegers/mongodb expected to be a reference》

或者搜 issue

《升级PHP7 jenssegers/mongodb expected to be a reference》

打开第二个链接 https://github.com/jenssegers…
看最后这个链接 https://github.com/zamrih/lar…

《升级PHP7 jenssegers/mongodb expected to be a reference》

然后修改源文件 jenssegers/mongodb/src/Jenssegers/Mongodb/Collection.php 虽然能解决,但这不是个好的办法。

/**
      * Handle specific insert call.
      * Cannot be handled dynamically because a reference needs to be passed
      * @param  array|object   $document
      * @param  array   $options
      * @return mixed
      */
     public function insert(&$document, array $options = [])
     {
         return $this->__call('insert', array(&$document, $options));
     }
     
    /**
      * Handle specific batchInsert call.
      * Cannot be handled dynamically because a reference needs to be passed
      * @param  array   $a
      * @param  array   $options
      * @return mixed
      */
     public function batchInsert(array &$a, array $options = [])
     {
         return $this->__call('batchInsert', array(&$a, $options));
     }
     
    /**
      * Handle specific save call.https://github.com/zamrih/laravel-mongodb/commit/8c3b4aaaee773e0c2968b4ff215ccec5305394e2
      * Cannot be handled dynamically because a reference needs to be passed
      * @param  array|object   $document
      * @param  array   $options
      * @return mixed
      */
     public function save(&$document, array $options = [])
     {
         return $this->__call('save', array(&$document, $options));
     }

其实 https://github.com/alcaeus/mo… 给出提示了
The insert, batchInsert, and save methods take the first argument by reference. While the original API does not explicitely specify by-reference arguments it does add an ID field to the objects and documents given.

接着又出现新问题:
Fatal error: Call to undefined method MongoDB\Driver\ReadConcern::isDefault() in ..src\Operation\Find.php on line 193
搜索下
https://github.com/alcaeus/mo…
https://github.com/mongodb/mo…
https://github.com/mongodb/mo…

https://github.com/mongodb/mo… 这个提交增加了MongoDBDriverReadConcern::isDefault() 但他要求 MongoDB扩展1.3.0+

因为默认版本是PHP5,所以 pecl install mongodb 会提示

pecl config-show

pecl/mongodb requires PHP (version >= 5.5.0, version <= 7.99.99), installed version is 5.3.29
No valid packages found
install failed

ll /data/php7/bin/
total 124692
-rwxr-xr-x 1 root root      842 Feb  4  2016 pear
-rwxr-xr-x 1 root root      863 Feb  4  2016 peardev
-rwxr-xr-x 1 root root      779 Feb  4  2016 pecl
lrwxrwxrwx 1 root root        9 Feb  4  2016 phar -> phar.phar
-rwxr-xr-x 1 root root    14834 Feb  4  2016 phar.phar
-rwxr-xr-x 1 root root 42332852 Feb  4  2016 php
-rwxr-xr-x 1 root root 42161587 Feb  4  2016 php-cgi
-rwxr-xr-x 1 root root     3583 Feb  4  2016 php-config
-rwxr-xr-x 1 root root 43141643 Feb  4  2016 phpdbg
-rwxr-xr-x 1 root root     4537 Feb  4  2016 phpize

/data/php7/bin/pecl install mongodb

而现在用的是1.1.6版本,那只好升级了 到 http://pecl.php.net/package/m… 下载最新版本 http://pecl.php.net/get/mongo…

tar -zxf mongodb-1.5.2.tgz
cd mongodb-1.5.2
which php7
/data/php7/bin/phpize
which php-config
./configure --with-php-config=/data/php7/bin/php-config
make && make install
ll /data/php7/lib/php/extensions/no-debug-zts-20151012/
php7 -i |grep extension_dir
extension_dir => /data/php7/lib/php/extensions/no-debug-zts-20151012 => /data/php7/lib/php/extensions/no-debug-zts-20151012
sqlite3.extension_dir => no value => no value

php7 --ini
Configuration File (php.ini) Path: /data/php7/etc/
Loaded Configuration File:         /data/php7/etc/php.ini
Scan for additional .ini files in: (none)
Additional .ini files parsed:      (none)
vi /data/php7/etc/php.ini
extension=mongodb.so
php7 -m |grep mongodb
php7 -i|grep mongodb

链接:
https://github.com/alcaeus/mo…
https://github.com/mongodb/mo…
https://github.com/alcaeus/mo…
http://php.net/manual/en/mong…
http://php.net/manual/zh/mong…
http://php.net/manual/zh/set….
https://github.com/mongodb/mo…
https://secure.php.net/manual…
https://github.com/mongodb/mo…
https://github.com/alcaeus/mo…
pecl 更换对应php版本
车轮升级PHP7踩过的一些坑
《PHP 开发者实践》
PHP 手册拾遗
PHP 7 升级实践
php7cc 辅助进行代码检查
php5 php7不兼容的地方
PHP7内核剖析
升级PHP7操作MongoDB
一篇写给准备升级PHP7的小伙伴的文章

    原文作者:苏生不惑
    原文地址: https://segmentfault.com/a/1190000016184052
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞