api-platform / api-platform/core

loadClassMetadata properties are ignored in GET operations

未关闭
#7,999 3 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
doctrine State
主要语言
PHP
星标
2.6k
派生
980
平均合并
2 天 5 小时
30 天内合并 PR
48

描述

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

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。