api-platform / api-platform/core

loadClassMetadata properties are ignored in GET operations

Open
#7,999 3 comments 0 reactions 0 assignees View on GitHub
doctrine State
Dominant language
PHP
Stars
2.6k
Forks
980
Avg merge
2d 5h
Merged PRs (30d)
48

Description

**API Platform version(s) affected**: 2.1

**Description**
Properties or associations added through Doctrine's `loadClassMetadata` event do not work in GET operations. They work properly in POST/PUT and the value gets stored in the database, but when doing a GET operation the same properties come up as empty.

My suspicion is that maybe data providers get initialized before doctrine listeners are registered, but my knowledge on the internals of API platform is not good enough to try and fix, so I'm writing it here.

**How to reproduce**
1. Create a doctrine listener and bind `loadClassMetadata` to it
2. Create an entity and add a property or an association to it through the listener.
3. Run a **POST** operation to create an object. The created property will work fine and show the correct value. The database will also have the correct value stored.
4. Run a **GET** operation on either the collection or the item. The created property will be null even if the value in the database is correct. Doctrine effectively does not know about the property.

**Possible Solution**
In my project I worked around the issue by creating a request listener with high priority containing only the following line:

`$this->doctrine->getManager()->getMetadataFactory()->getAllMetadata($curClass);`

The above code fixes the issue, but it is hacky and seems to slightly degrade performance. I would much prefer if we can find the root of the bug and properly fix the issue.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.