doctrine / DoctrineBundle

Symfony Bundle for Doctrine ORM and DBAL
https://www.doctrine-project.org/projects/doctrine-bundle.html
MIT License
4.72k stars 454 forks source link

Unintercepted function call to load entity data #1809

Closed jbcr closed 5 months ago

jbcr commented 5 months ago

Hello, I'm upgrading a project from Sylius 1.12 to 1.13.2.

During the upgrade, I have set the enable_lazy_ghost_objects to true to remove deprecation.

But, if enable_lazy_ghost_objects is true I have an error when I try to access Sylius Ressource translation from a relation.

In Sylius, you have an entity (resource) named Product that can be translated (ProductTranslation). On the Product, I have a method called getTanslation(?string $local = null) to get the current translation or requested translation by $local parameter. This method throws an exception if the current local and the $local parameter are null.

I added a new entity (resource) named Tag with a one-to-many relation to Product.

When I execute $tag->getFirstProduct()->getTranslation()->getName(); the exception is thrown because the product is not loaded.

When enable_lazy_ghost_objects is false, the proxy has a method to initialize the product entity on getTranslation() call.

When enable_lazy_ghost_objects is true, the proxy does not intercept the function call and the object is not loaded. I must call a getter manually to force the object data loading before call getTranslation().

Why has this behavior changed with this option?


Installed Doctrine library:

$ composer show 'doctrine/*'   
doctrine/annotations                2.0.1  Docblock Annotations Parser
doctrine/cache                      2.2.0  PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others.
doctrine/collections                1.8.0  PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.
doctrine/common                     3.4.4  PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better reflection support, proxies and much more.
doctrine/data-fixtures              1.7.0  Data Fixtures for all Doctrine Object Managers
doctrine/dbal                       3.8.6  Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
doctrine/deprecations               1.1.3  A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages.
doctrine/doctrine-bundle            2.11.1 Symfony DoctrineBundle
doctrine/doctrine-migrations-bundle 3.3.1  Symfony DoctrineMigrationsBundle
doctrine/event-manager              1.2.0  The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.
doctrine/inflector                  2.0.10 PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural forms of words.
doctrine/instantiator               2.0.0  A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                      2.1.1  PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.
doctrine/migrations                 3.7.4  PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your database schema and easily deploying changes to it. It is a very easy to use and a powerful tool.
doctrine/orm                        2.15.5 Object-Relational-Mapper for PHP
doctrine/persistence                3.3.3  The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share.
doctrine/sql-formatter              1.4.0  a PHP SQL highlighting library
ostrolucky commented 5 months ago

This is best to report to https://github.com/symfony/symfony repo, since proxy logic is there

jbcr commented 5 months ago

@ostrolucky Why?

All code to generate Entity proxy are in Doctrine ORM and Doctrine Bundle.

nicolas-grekas commented 5 months ago

That's for the ORM indeed.

ostrolucky commented 5 months ago

I thought enable_lazy_ghost_objects utilizes LazyGhostTrait which is in symfony/var-exporter. Either way, this is very unlikely to be an issue in doctrine-bundle, will be ORM or var-exporter.

nicolas-grekas commented 5 months ago

Sure, and that's in the ORM: the issue is that unmapped properties don't trigger initialization, which means postLoad events are not triggered (@stof figured this out).