doctrine/DoctrineMongoDBBundle

Symfony 5.1 and PHP 7.4 generate proxies error

markitosgv opened this issue · 8 comments

Hi,

I've upgraded my symfony App to use ODM 2 and Symfony 5. I've an issue when option:

auto_generate_proxy_classes: false

This options is set by default when you're running on production environment. When I login to my app, user data in session file is not serialized. When auto_generate_proxy_classes is true, user data in session is filled.

I've notice too that running proxies generator command from bundle doesn't generate any proxies in cache directory if auto_generate_proxy_classes is set to false.

I'm using:

php 7.4.11
doctrine/mongodb-odm-bundle: 4.2.2
doctrine/mongodb-odm: 2.1.2
ocramius/proxy-manager 2.8.1

We highly recommend pregenerating all proxies, hydrators, and custom collections along with warming up Symfony's cache when running in production mode. To do that we are providing commands like doctrine:mongodb:generate:proxies and others.

I've notice too that running proxies generator command from bundle doesn't generate any proxies in cache directory if auto_generate_proxy_classes is set to false.

What's the output when you run the command? The auto_generate_proxy_classes should not have any impact on the CLI commands

Hi @malarzm ,

Yes, exactly when I'm running generator proxy command output is:

....
Processing document "App\Document\Plan"

Proxy classes generated to "xxx/app/var/cache/dev/doctrine/odm/mongodb/Proxies"

But any files are generated in folder if auto_generate_proxy_classes = false. Both prod and dev environments.
If auto_generate_proxy_classes = true, works.

Ok, after the second look the behaviour you're experiencing is correct. auto_generate_proxy_classes while set to false makes ProxyFactory to use EvaluatingGeneratorStrategy which will create proxy classes on the fly as needed and not store them in the file. Only the true value will generate files. So in your case I'd advise to have it set to true in production mode. But also the thing I didn't noticed before:

When I login to my app, user data in session file is not serialized.

It seems that you're serializing a proxy object somewhere which is (almost) never a good idea. You should serialize a document's identifier and later fetch the related document (or proxy it) when it's needed. That way you will only serialize simple values and not get into any weird situations where you'd serialize initialized proxy's persistent state (and entire dcument manager) by accident.

Thanks @malarzm but a fresh installation of Symfony 5 with this bundle, creates a default prod/doctrine_mongodb.yml configuration with auto_generate_hydrator_classes, auto_generate_proxy_classes setted false by default.

Anyway previous version of this bundle works with user serialization on login when auto_generate_proxy_classes is false.

Anyway previous version of this bundle works with user serialization on login when auto_generate_proxy_classes is false.

It's not really up for the bundle. If you're seeing a behaviour change it means you've upgraded from ODM 1.x to 2.x and breaking changes are to be expected

Ok, after the second look the behaviour you're experiencing is correct. auto_generate_proxy_classes while set to false makes ProxyFactory to use EvaluatingGeneratorStrategy which will create proxy classes on the fly as needed and not store them in the file. Only the true value will generate files. So in your case I'd advise to have it set to true in production mode.

Just a note that the conclusion is wrong here: the cache warmer doesn't do anything if auto_generate_proxy_classes is true, so it needs to be false for the warmer to work (which is why the recipe is configured as it is).

Ok, I get it.

But a Default Symfony 5 project using ODM 2, with a logged User with any relation (i.e a reference_one), is going to fail in production mode.

stale commented

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed in a week if no further activity occurs. Thank you for your contributions.