Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameter 1 to MongoCollection::insert() expected to be a reference, value given #107

Closed
glowdan opened this issue May 16, 2016 · 5 comments
Labels

Comments

@glowdan
Copy link

glowdan commented May 16, 2016

Hi,
When we upgrade our website from php5.5 to php7.0.6. We got some warning like this:

PHP Warning: Parameter 1 to MongoCollection::insert() expected to be a reference, value given in /website/helper/mongo/collection.php on line 66.

And I saw the same question occurs on wordpress. May I remove the "&" in "alcaeus/mongo-php-adapter/lib/Mongo/MongoCollection.php::insert on line 277"?

@alcaeus
Copy link
Owner

alcaeus commented May 16, 2016

Unfortunately, insert, batchInsert and save can't be implemented the way they were in the legacy driver:

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.

I had the choice:

  1. have the first argument by-value: this would have allowed passing a value to the method but the array will be lacking the _id value that is added by the driver
  2. have the first argument by-reference: this would fix the _id problem but the method signatures would be incompatible.

The original intent of the driver was to have the argument by-reference so that an auto-generated ID could be added. Thus, I settled for option #2, knowing that people can either handle that specific warning in an error handler or ignore it. On the other hand, having code break because you were expecting an _id field in your array seems a lot more severe.

TL;DR: I wouldn't recommend it.

@glowdan
Copy link
Author

glowdan commented May 16, 2016

OK,I solved it. If we need insert or save data to db, we add the reference to the first param.

            if (PHP_VERSION_ID > 70000 && in_array($method, ['insert', 'batchInsert', 'save'])) {
                $saveData = array_shift($param);
                $saveParams = array_shift($param);
                if (NULL==$saveParams) {
                    $saveParams = [];
                }
                $re = call_user_func_array([$collection, $method], [&$saveData, $saveParams]);
            } else {
                $re = call_user_func_array([$collection, $method], $param);
            }

@Ardakilic
Copy link

@glowdan can you please elaborate more ? Where did you add this patch ?

I'm using https://github.com/jenssegers/laravel-mongodb with Laravel 4.2.x along with PHP7.0, and this is the only step I'm struggling with.

@rhclayto
Copy link

@Ardakilic Check this out: LearningLocker/learninglocker#893

@Ardakilic
Copy link

Thanks @rhclayto , will look right away!

fbouliane added a commit to fbouliane/phlask that referenced this issue Oct 30, 2018
alcaeus/mongo-php-adapter is a dropin replacement for php mongo
extension for recent version of php. it allow to use ext-mongo like
ext-mongodb.

We use it to use phlask with php7, however, there are annoying notices
when running because lock->insert and col->save now take arguments by
reference rather than by value.
alcaeus/mongo-php-adapter#107

This should silence the notices.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants