Open mmaedler opened 2 years ago
I'm using traits to do a Timestampabletrait on a project with the #[ApiProperty(...)] attribute in the traits and it's working well.
The only difference is I'm not using the writable: false property.
You can try to clear symfony cache with the php bin/console cache:clear
command and ensure the getter and setter are named properly as ApiPlatform need them :
If it doesn't work, perhaps the problem is located on the ApiPlatform side especifically when using the writable property.
Thanks for your reply. In my example for Entity Item
it filtering the writeable works just fine using the trait. However for the Entites that extend a parent entity it doesn't. Prove for the correct implementation is that the doctrine part (setting timestamps onCreate/onUpdate) works for all of them.
Therefore my assumption was, that there might be an issue with serialization process within ApiPlatform.
It seem's API Platform doesn't officially support entity inheritance, see issues :
https://github.com/api-platform/api-platform/issues/593
https://github.com/api-platform/api-platform/issues/372
https://github.com/api-platform/api-platform/issues/266
https://github.com/api-platform/api-platform/issues/277
There is a lot more issues related to inheritance. I recommend using trait inside entities instead when it's possible.
Pros :
Cons : But traits can have sometime have some caveheat when :
API Platform version(s) affected: 2.6.5 (api-platform/core)
Description
To reflect different kinds of people types in my application I have decided to go with doctrines single table inheritance (STI) feature to build my entities. These entities all share the fact that they are timestampable via a trait that gets injected in the parent class
Person
. I have this class structure (simplified) for my entities:As you can see I have set the two properties
$createdAt
and$updatedAt
as non-writable since it wouldn't make any sense to update them via API call. This is working fine when writingItem
ressource entries, but for bothGiver
andDonnee
ressourcescreatedAt
could be set via API.If I move the trait injection from the parent class
Person
into the two subclasses it works as expected (but again won't make too much sense).So not sure if this is really a bug or just a misunderstanding of concepts on my end. Therefore, any hint would be appreciated.
Thank you!