-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Refactoring EntityUserProvider::__construct() to not do work, cause cache warm error #16772
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
Refactoring EntityUserProvider::__construct() to not do work, cause cache warm error #16772
Conversation
instead of |
} | ||
|
||
$this->repository = $em->getRepository($class); | ||
$this->em = $registry->getManager($managerName); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
even better: you should store the registry and the manager name instead of instantiating the entity manager here. This would also make it compatible with resetting the EM.
43a8c73
to
4d6d41d
Compare
@@ -125,7 +125,7 @@ public function testLoadUserByUserNameShouldDeclineInvalidInterface() | |||
private function getManager($em, $name = null) | |||
{ | |||
$manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry'); | |||
$manager->expects($this->once()) | |||
$manager->expects($this->any()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We shouldn't really care how many times getManager()
is called anyways. And now, it's called multiple times in some tests, and zero times in others.
@stof good suggestions - I've just updated this PR: we get everything lazily. This is still an internal refactoring - I tried to minimize the changes to make this obvious. Now, would this be a bug fix for 2.3? I have the PR opened against 2.8... which doesn't make any sense :). It seems like this should go into 2.3 or into master for 3.1. I'd like to put it into 2.3. |
Does this fix doctrine/DoctrineBundle#351 ? |
|
||
private function getClassMetadata() | ||
{ | ||
return $this->getEntityManager()->getClassMetadata($this->getClass()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getClassMetadata calls getClass and getClass calls getClassMetadata. Looks like a possible endless loop.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, good eye. In fact, I should only use getClass()
where we actually need a valid PHP class name. I've updated to do that (otherwise, just use the class/alias that was passed in).
@Tobion Yes and no. It definitely fixed one more case where that error happens. But are there other situations that still cause that error? Possibly. But yea, when I saw this issue locally, I knew it was doctrine/DoctrineBundle#351 and wanted to squash it :) |
👍 for 2.3 |
The only other place where 👍 for 2.3 Status: Reviewed |
👍 |
return $class === $this->getClass() || is_subclass_of($class, $this->getClass()); | ||
} | ||
|
||
private function getEntityManager() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would call it getObjectManager
, as this also supports ODMs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated
I agree this should go in 2.3 |
…ny work This helps break a dependency chain during cache warmup, that includes having Doctrine load metadata, which will fail if there is no database on new Doctrine versions.
ca8b843
to
b4246bf
Compare
👍 |
I've just rebased this against 2.3 - it shows as a conflict against 2.8, but will merge cleanly into 2.3. It should be ready for a merge. Tests before my last rebase all passed except for an unrelated hhvm failure. |
Thank you @weaverryan. |
…rk, cause cache warm error (weaverryan) This PR was submitted for the 2.8 branch but it was merged into the 2.3 branch instead (closes #16772). Discussion ---------- Refactoring EntityUserProvider::__construct() to not do work, cause cache warm error | Q | A | ------------- | --- | Bug fix? | "yes" | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | none | License | MIT | Doc PR | n/a This fixes a "Database not Found" error when using `doctrine/orm` 2.5 while warming up your cache under certain situations. Basically, if you use the `EntityUserProvider`, then during cache warmup, Twig requires the `security.authorization_checker` which eventually requires this `EntityUserProvider`, which previously caused Doctrine to calculate the metadata for your User class. If no database exists (and you haven't specified the platform), you'll get the error. More broadly, this simply tries to do less work in the constructor. It's a "bug fix", only kind of, but as it's completely an internal refactoring, it should be quite safe. Thanks! Commits ------- 44a2861 Refactoring EntityUserProvider::__construct() to not do work, cause cache warm error
Thanks @stof! There will be a conflict when merging to 2.8: but it should be simple to fix: use the new side of the diff (i.e. the code from 2.8), but replace |
I merge 2.3 into 2.7 and 2.8 to resolve the conflict |
This fixes a "Database not Found" error when using
doctrine/orm
2.5 while warming up your cache under certain situations. Basically, if you use theEntityUserProvider
, then during cache warmup, Twig requires thesecurity.authorization_checker
which eventually requires thisEntityUserProvider
, which previously caused Doctrine to calculate the metadata for your User class. If no database exists (and you haven't specified the platform), you'll get the error.More broadly, this simply tries to do less work in the constructor. It's a "bug fix", only kind of, but as it's completely an internal refactoring, it should be quite safe.
Thanks!