api-platform / api-platform/core

loadClassMetadata properties are ignored in GET operations

Abierto
#7,999 3 comentarios 0 reacciones 0 asignados Ver en GitHub
doctrine State
Lenguaje dominante
PHP
Estrellas
2.6k
Forks
980
Merge medio
2 d 5 h
PR fusionados (30 d)
48

Descripción

**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.

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.