symfony: [PropertyAccess] Can't access static property $lazyPropertiesNames on Doctrine proxies

Symfony version(s) affected: 5.1.2, and previous versions.

Description

I’m having an issue with the PropertyAccessor component (while using the Serializer component) on an entity managed by Doctrine: Accessing static property Proxies\__CG__\App\Entity\User::$lazyPropertiesNames as non static

Capture d’écran de 2020-06-18 12-27-48

I don’t know if it comes from recent Doctrine updates or from Symfony itself, but some people got the issue on schmittjoh/serializer, see https://github.com/schmittjoh/serializer/issues/1209.

I’ve tried their workaround (downgrade doctrime/orm from 2.7.3 to 2.7.2) but this does not work.

Here is my composer show if it can helps:

cboden/ratchet                      v0.4.2  PHP WebSocket library
doctrine/annotations                1.10.3  Docblock Annotations Parser
doctrine/cache                      1.10.1  PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongo...
doctrine/collections                1.6.5   PHP Doctrine Collections library that adds additional functionality on top of PHP arrays.
doctrine/common                     2.13.3  PHP Doctrine Common project is a library that provides additional functionality that other Doctrine projects depend on such as better...
doctrine/data-fixtures              1.4.3   Data Fixtures for all Doctrine Object Managers
doctrine/dbal                       2.10.2  Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.
doctrine/doctrine-bundle            2.1.0   Symfony DoctrineBundle
doctrine/doctrine-migrations-bundle 3.0.1   Symfony DoctrineMigrationsBundle
doctrine/event-manager              1.1.0   The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.
doctrine/inflector                  1.4.3   PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural fo...
doctrine/instantiator               1.3.1   A small, lightweight utility to instantiate objects in PHP without invoking their constructors
doctrine/lexer                      1.2.1   PHP Doctrine Lexer parser library that can be used in Top-Down, Recursive Descent Parsers.
doctrine/migrations                 3.0.0   PHP Doctrine Migrations project offer additional functionality on top of the database abstraction layer (DBAL) for versioning your da...
doctrine/orm                        v2.7.3  Object-Relational-Mapper for PHP
doctrine/persistence                1.3.7   The Doctrine Persistence project is a set of shared interfaces and functionality that the different Doctrine object mappers share.
doctrine/reflection                 1.2.1   The Doctrine Reflection project is a simple library used by the various Doctrine projects which adds some additional functionality on...
doctrine/sql-formatter              1.1.0   a PHP SQL highlighting library
egulias/email-validator             2.1.18  A library for validating emails against several RFCs
evenement/evenement                 v3.0.1  Événement is a very simple event dispatching library for PHP
firebase/php-jwt                    v5.2.0  A simple library to encode and decode JSON Web Tokens (JWT) in PHP. Should conform to the current spec.
fzaninotto/faker                    v1.9.1  Faker is a PHP library that generates fake data for you.
guzzlehttp/psr7                     1.6.1   PSR-7 message implementation that also provides common utility methods
hautelook/alice-bundle              2.7.2   Symfony bundle to manage fixtures with Alice and Faker.
lcobucci/jwt                        3.3.2   A simple library to work with JSON Web Token and JSON Web Signature
monolog/monolog                     2.1.0   Sends your logs to files, sockets, inboxes, databases and various web services
myclabs/deep-copy                   1.9.5   Create deep copies (clones) of your objects
nelmio/alice                        3.7.1   Expressive fixtures generator
nikic/php-parser                    v4.5.0  A PHP parser written in PHP
ocramius/package-versions           1.5.1   Composer plugin that provides efficient querying for installed package versions (no runtime IO)
ocramius/proxy-manager              2.2.3   A library providing utilities to generate, instantiate and generally operate with Object Proxies
phpdocumentor/reflection-common     2.1.0   Common reflection classes used by phpdocumentor to reflect the code structure
phpdocumentor/reflection-docblock   5.1.0   With this component, a library can provide support for annotations via DocBlocks or otherwise retrieve information that is embedded i...
phpdocumentor/type-resolver         1.1.0   A PSR-5 based resolver of Class names, Types and Structural Element Names
psr/cache                           1.0.1   Common interface for caching libraries
psr/container                       1.0.0   Common Container Interface (PHP FIG PSR-11)
psr/event-dispatcher                1.0.0   Standard interfaces for event handling.
psr/http-message                    1.0.1   Common interface for HTTP messages
psr/link                            1.0.0   Common interfaces for HTTP links
psr/log                             1.1.3   Common interface for logging libraries
ralouphie/getallheaders             3.0.3   A polyfill for getallheaders.
ratchet/rfc6455                     v0.2.6  RFC6455 WebSocket protocol handler
react/cache                         v1.0.0  Async, Promise-based cache interface for ReactPHP
react/dns                           v1.2.0  Async DNS resolver for ReactPHP
react/event-loop                    v1.1.1  ReactPHP's core reactor event loop that libraries can use for evented I/O.
react/promise                       v2.8.0  A lightweight implementation of CommonJS Promises/A for PHP
react/promise-timer                 v1.5.1  A trivial implementation of timeouts for Promises, built on top of ReactPHP.
react/socket                        v1.4.0  Async, streaming plaintext TCP/IP and secure TLS socket server and client connections for ReactPHP
react/stream                        v1.1.1  Event-driven readable and writable streams for non-blocking I/O in ReactPHP
sebastian/comparator                4.0.2   Provides the functionality to compare PHP values for equality
sebastian/diff                      4.0.1   Diff implementation
sebastian/exporter                  4.0.1   Provides the functionality to export PHP variables for visualization
sebastian/recursion-context         4.0.1   Provides functionality to recursively process PHP variables
sensio/framework-extra-bundle       v5.5.6  This bundle provides a way to configure your controllers with annotations
symfony/asset                       v5.1.2  Symfony Asset Component
symfony/browser-kit                 v5.1.2  Symfony BrowserKit Component
symfony/cache                       v5.1.2  Symfony Cache component with PSR-6, PSR-16, and tags
symfony/cache-contracts             v2.1.2  Generic abstractions related to caching
symfony/config                      v5.1.2  Symfony Config Component
symfony/console                     v5.1.2  Symfony Console Component
symfony/css-selector                v5.1.2  Symfony CssSelector Component
symfony/debug-bundle                v5.1.2  Symfony DebugBundle
symfony/debug-pack                  v1.0.8  A debug pack for Symfony projects
symfony/dependency-injection        v5.1.2  Symfony DependencyInjection Component
symfony/deprecation-contracts       v2.1.2  A generic function and convention to trigger deprecation notices
symfony/doctrine-bridge             v5.1.2  Symfony Doctrine Bridge
symfony/dom-crawler                 v5.1.2  Symfony DomCrawler Component
symfony/dotenv                      v5.1.2  Registers environment variables from a .env file
symfony/error-handler               v5.1.2  Symfony ErrorHandler Component
symfony/event-dispatcher            v5.1.2  Symfony EventDispatcher Component
symfony/event-dispatcher-contracts  v2.1.2  Generic abstractions related to dispatching event
symfony/expression-language         v5.1.2  Symfony ExpressionLanguage Component
symfony/filesystem                  v5.1.2  Symfony Filesystem Component
symfony/finder                      v5.1.2  Symfony Finder Component
symfony/flex                        v1.8.4  Composer plugin for Symfony
symfony/form                        v5.1.2  Symfony Form Component
symfony/framework-bundle            v5.1.2  Symfony FrameworkBundle
symfony/http-client                 v5.1.2  Symfony HttpClient component
symfony/http-client-contracts       v2.1.2  Generic abstractions related to HTTP clients
symfony/http-foundation             v5.1.2  Symfony HttpFoundation Component
symfony/http-kernel                 v5.1.2  Symfony HttpKernel Component
symfony/inflector                   v5.1.2  Symfony Inflector Component
symfony/intl                        v5.1.2  A PHP replacement layer for the C intl extension that includes additional data from the ICU library.
symfony/mailer                      v5.1.2  Symfony Mailer Component
symfony/maker-bundle                v1.19.0 Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate ...
symfony/mime                        v5.1.2  A library to manipulate MIME messages
symfony/monolog-bridge              v5.1.2  Symfony Monolog Bridge
symfony/monolog-bundle              v3.5.0  Symfony MonologBundle
symfony/notifier                    v5.1.2  A library to notify messages
symfony/options-resolver            v5.1.2  Symfony OptionsResolver Component
symfony/orm-pack                    v1.0.8  A pack for the Doctrine ORM
symfony/phpunit-bridge              v5.1.2  Symfony PHPUnit Bridge
symfony/polyfill-intl-grapheme      v1.17.0 Symfony polyfill for intl's grapheme_* functions
symfony/polyfill-intl-icu           v1.17.0 Symfony polyfill for intl's ICU-related data and classes
symfony/polyfill-intl-idn           v1.17.0 Symfony polyfill for intl's idn_to_ascii and idn_to_utf8 functions
symfony/polyfill-intl-normalizer    v1.17.0 Symfony polyfill for intl's Normalizer class and related functions
symfony/polyfill-mbstring           v1.17.0 Symfony polyfill for the Mbstring extension
symfony/polyfill-php73              v1.17.0 Symfony polyfill backporting some PHP 7.3+ features to lower PHP versions
symfony/polyfill-php80              v1.17.0 Symfony polyfill backporting some PHP 8.0+ features to lower PHP versions
symfony/process                     v5.1.2  Symfony Process Component
symfony/profiler-pack               v1.0.4  A pack for the Symfony web profiler
symfony/property-access             v5.1.2  Symfony PropertyAccess Component
symfony/property-info               v5.1.2  Symfony Property Info Component
symfony/routing                     v5.1.2  Symfony Routing Component
symfony/security-bundle             v5.1.2  Symfony SecurityBundle
symfony/security-core               v5.1.2  Symfony Security Component - Core Library
symfony/security-csrf               v5.1.2  Symfony Security Component - CSRF Library
symfony/security-guard              v5.1.2  Symfony Security Component - Guard
symfony/security-http               v5.1.2  Symfony Security Component - HTTP Integration
symfony/serializer                  v5.1.2  Symfony Serializer Component
symfony/serializer-pack             v1.0.3  A pack for the Symfony serializer
symfony/service-contracts           v2.1.2  Generic abstractions related to writing services
symfony/stopwatch                   v5.1.2  Symfony Stopwatch Component
symfony/string                      v5.1.2  Symfony String component
symfony/test-pack                   v1.0.6  A pack for functional and end-to-end testing within a Symfony app
symfony/translation                 v5.1.2  Symfony Translation Component
symfony/translation-contracts       v2.1.2  Generic abstractions related to translation
symfony/twig-bridge                 v5.1.2  Symfony Twig Bridge
symfony/twig-bundle                 v5.1.2  Symfony TwigBundle
symfony/twig-pack                   v1.0.0  A Twig pack for Symfony projects
symfony/validator                   v5.1.2  Symfony Validator Component
symfony/var-dumper                  v5.1.2  Symfony mechanism for exploring and dumping PHP variables
symfony/var-exporter                v5.1.2  A blend of var_export() + serialize() to turn any serializable data structure to plain PHP code
symfony/web-link                    v5.1.2  Symfony WebLink Component
symfony/web-profiler-bundle         v5.1.2  Symfony WebProfilerBundle
symfony/webpack-encore-bundle       v1.7.3  Integration with your Symfony app & Webpack Encore!
symfony/yaml                        v5.1.2  Symfony Yaml Component
theofidry/alice-data-fixtures       v1.1.2  Nelmio alice extension to persist the loaded fixtures.
twig/extra-bundle                   v3.0.3  A Symfony bundle for extra Twig extensions
twig/twig                           v3.0.3  Twig, the flexible, fast, and secure template language for PHP
webmozart/assert                    1.9.0   Assertions to validate method input/output with nice error messages.
zendframework/zend-code             3.4.1   Extensions to the PHP Reflection API, static code scanning, and code generation
zendframework/zend-eventmanager     3.2.1   Trigger and listen to events within a PHP application

How to reproduce

Unfortunately I can’t send a reproducer right now, I’m in a big rush until saturday night.

However @jannmylj posted a reproducer (https://github.com/schmittjoh/serializer/issues/1209#issuecomment-637445190), you may be able to reproducible the issue with the code adapted for the Serializer component.

Possible Solution

I’ve opened a PR to support static properties, see #37335.

I’m not sure this is the real solution, maybe Doctrine-related properties should be ignored by the Serializer component… I don’t know at all 😕

Additional context

No.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 2
  • Comments: 29 (8 by maintainers)

Most upvoted comments

Problem is still here with Sf 5.2 using the standard symfony/serializer. Creating custom normalizers for each entity doesn’t sounds like a fix to me.

[...]
doctrine/annotations                  1.12.1     MIT           
doctrine/cache                        1.10.2     MIT           
doctrine/collections                  1.6.7      MIT           
doctrine/common                       3.1.2      MIT           
doctrine/dbal                         2.13.0     MIT           
doctrine/deprecations                 v0.5.3     MIT           
doctrine/doctrine-bundle              2.3.1      MIT           
doctrine/doctrine-migrations-bundle   3.1.1      MIT           
doctrine/event-manager                1.1.1      MIT           
doctrine/inflector                    2.0.3      MIT           
doctrine/instantiator                 1.4.0      MIT           
doctrine/lexer                        1.2.1      MIT           
doctrine/migrations                   3.1.1      MIT           
doctrine/orm                          2.8.4      MIT           
doctrine/persistence                  2.1.0      MIT           
doctrine/sql-formatter                1.1.1      MIT
[...]
symfony/serializer                    v5.2.4     MIT
[...]

There are many ways to work around this problem, but i don’t think it is the intended behavior of the serializer, or is it? If not, the issue should be re-opened.

Well… I fixed my stuff by using createQueryBuilder. I basically did this:

...
//this is running in a foreach where I query multiple entities, hence the $e
....
 $queryBuilder = $this->entityManager->createQueryBuilder();
            $select = ['u'];
            $queryBuilder
                 ->from($e, 'u')
....
             ;

            $this->getAssociations($queryBuilder, $e, $select, 'u');
            $queryBuilder->select($select);
            $result = $queryBuilder->getQuery()->getResult();
....
    private function getAssociations(QueryBuilder $qb, string $classPath, array& $select, string $alias) : void
    {
        $md = $this->entityManager->getClassMetadata($classPath);
        $assoc = $md->getAssociationNames();

        foreach ($assoc as $a) {
            if ($a === 'parent')
                continue;

            if (!in_array($a, $select, true)) {
                $select[] = $a;
                $qb->join("$alias.$a", $a);
                $this->getAssociations($qb, $md->getAssociationTargetClass($a), $select, $a);
            }
        }
    }

getAssociations basically created a joins for all the associations and that forced doctrine to load everything.

The problem is still there in the serializer but I get what I need.

I just added a few properties to the ignored_attributes, and now it works. image

I just got this error in a fresh Symfony project. Seems like a pretty big deal to me if i can’t use the serializer with regular entities anymore?!