-
-
Notifications
You must be signed in to change notification settings - Fork 339
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
DI is not found in MongoCollection using Phalcon 3.1 and PHP 7.1 #760
Comments
Related: https://forum.phalconphp.com/discussion/15927/mongocollection-wont-save Class is ignoring This fix in the model or in MongoCollection.php fixes the above DI, saving problem. public function getReservedAttributes()
{
$reserved = parent::$_reserved;
if(null === $reserved) {
$reserved = [
"_connection"=> true,
"_dependencyInjector"=> true,
"_source"=> true,
"_operationMade"=> true,
"_errorMessages"=> true,
"_dirtyState"=> true,
"_modelsManager" => true,
"_skipped" => true,
// last two to be complete
"_reserved" => true,
"_disableEvents" => true
];
parent::$_reserved = $reserved;
}
return $reserved;
} |
Mine works when creating a new object: // works fine
$user = new Users();
$user->email = "user@email.com";
$user->save(); But when updating an entry, I'm getting the same error: // A dependency injector container is required to obtain the services related to the ODM
$user = Users::findFirst([[ "email" => "user@email.com" ]]);
$user->email = "updated@email.com";
$user->save(); I think it's related to issue #762. When saving, it stores even the private fields, including |
I've updated issue, error happens on update not insert |
MongoCollection extends Not sure what happens, it should work. Any idea? What is returned after you do It just looks like you don't have any Di created just |
For me on php 7.0.13 it works but i have:
From code like: $robots = new Robots();
$robots->name = 'test';
$robots->create();
$robots = Robots::findFirst();
var_dump($robots->toArray());
$robots->name = 'test2';
$robots->save(); I guess when updating collection we should remove |
@sergeyklay I'm having the same problem. Updating to PHP 7.1 and Phalcon 3.1.2 somehow broke an application where we are using Mongo. I get Instead, when deleting records I get Error: Call to a member function notifyEvent() on array
/var/www/wiki/vendor/phalcon/incubator/Library/Phalcon/Mvc/MongoCollection.php:467
/var/www/wiki/vendor/phalcon/incubator/Library/Phalcon/Mvc/MongoCollection.php:352 I do not know whether @sergeyklay is the maintainer or someone else, but please this should have very high priority since Mongo currently is not usable. Thanks! UPDATE: The workaround if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
$this->_dependencyInjector = \Phalcon\Di::getDefault();
$this->_modelsManager = $this->_dependencyInjector->getShared("collectionManager");
$this->_modelsManager->initialize($this);
} can also fixes the delete() method. |
I can't reproduce it, i checked it on latest phalcon and incubator and it works fine, i have other error above. |
It's strange you cannot reproduce it, since many people are having the same problem.. Are you using PHP 7.1? Because with PHP 7.0 it probably works fine (at least for me it was working fine with PHP 7.0). UPDATE: |
Yes, using php 7.1 i have:
Though i have other idea why you might have this issue, do you maybe have your own di? Which DI you use FactoryDefault or just Di? |
Interesting question, though in the app I use the FactoryDefault, in the tests it's the |
Then idk, i just don't have it really. Weird issue, and this problem you have in both, like app and tests? |
It seems both, but tomorrow I'll make some more detailed checks! |
I just don't have this error, which is weird, you could try doin something like Di::setDefault($di) but i doubt it will fix it. dependencyInjector is set in parent class. Further it's not lost anywhere as you trying to talk. Though surely there is problem with those reserved attributes, maybe it's causing this di error, not sure. |
@Jurigag ok, it seems the errors are present only in tests. In the parent class I have I agree that's weird.. but several people seem to have the same problem. // [...]
use Phalcon\Db\Adapter\MongoDB\Client;
use Phalcon\Test\UnitTestCase as PhalconTestCase;
abstract class WebTestCase extends PhalconTestCase
{
// [...]
public function setUp()
{
parent::setUp();
// Load any additional services that might be required during testing
$di = Di::getDefault();
// [...]
/**
* Database connection is created based in the parameters defined in the configuration file
*/
$di->setShared('mongo', function () {
$dsn = 'mongodb://' . env('DB_HOST', 'localhost');
$mongo = new Client($dsn);
return $mongo->selectDatabase(env('DB_NAME'));
});
// Collection Manager is required for MongoDB
$di->setShared('collectionManager', function () {
return new Manager();
});
// [...]
}
// [...]
} But before upgrading to PHP 7.1 and Phalcon 3.1.2 it was working without problems. phalcon/incubator: v3.1.1 |
I can confirm I have this problem also in the web (i.e. not in tests). Here is the error when I try to save a model:
Is there any progress about this? |
If someone will post full script to reproduce then sure i guess. |
Can someone test 7.1.5. This is interesting as many static props were undeclared. |
@tkroll still same problem with
|
Boo. Thanks for trying. It was a shot in the dark. |
It works for me on 7.1.0 or 7.0.0, i just have error what i wrote above, someone needs to create repo with full code to reproduce. |
As you see we don't have error with this Di thing, we need script to reproduce. |
@baychae why you thumb down? If you have script to reproduce then post it please. |
@Jurigag @sergeyklay I have now a public repo that shows this problem. PHP 7.0 : just 1 test failing PHP 7.1 with FIX : just 1 test failing Conclusions:
Imho the bug level is Medium or High. |
@micheleangioni i have this thing about _id too, i think it's about mongodb/mongodb extension version, that from some version they disabled option to update |
@Jurigag it might be, but in the failing test I am NOT updating the _id field, that's the point! We have to understand how to fix both problems, because at the moment this Mongo library is not usable :( |
Hi @Jurigag , About the |
+1 |
phalcon/cphalcon#12891 waiting for merging this, then i will change build script because currently incubator uses latest stable phalcon(we need to use source and zephir) and will fix this _id issue and check if tests are passing. |
Okay i merged mod on |
Just did: then composer require phalcon/incubator:^3.2 Got this error: PHP Fatal error: Uncaught Phalcon\Mvc\Collection\Exception: A dependency injector container is required to obtain the services related to the ODM in /var/www/outsmart/shared/utils/incubator/Library/Phalcon/Mvc/MongoCollection.php:77 |
Script to reproduce? |
$u = User::findByEmail('test@test.com'); My User class extends \Phalcon\Mvc\MongoCollection. It echos correctly. |
Hmmmmm. Could you try other database? It's possible that you just have in mongo I guess we will need now to change code to not set those reserved attributes if they returned from mongo database. |
Oh snap! Good call. Did a quick:
|
Yea now the question is:
|
I'd think outside the framework. It's a bit of work to do every time. This worked for me. No warranty on this. Script for removing reservered properties from Mongo: |
Okay, so now i will close it, let me know if anything other occur, also there is this - #794 if you could comment here it could be good. |
@Jurigag I'm still having problems. I am using the last Incubator version Line 1070: Any clue? Thanks Update |
It's fixed line above with |
@Jurigag ok now I get it, the PR has been merged in the 3.2.x branch but NOT in the Master branch. Is that really ok? Shouldn't be merged in both? |
It wasn't? Oh i must forgot it. Let me merge it. Already released. |
Thanks :) I conferm the build is now ok. |
Great, happy to hear, also i would like to know your opinion about #794 |
Getting error in MongoCollection after updating to Phalcon 3.1.2 and PHP 7.1
Current workaround in model class
Details
The text was updated successfully, but these errors were encountered: