首页 > 解决方案 > Api Platform using Output with custom Persister

问题描述

I create an ApiResource model class MyCustomModel (not Doctrine Entity) that has a declaration of output.

/**
 * @ApiResource(
 * itemOperations={},
 * collectionOperations={"post"={}},
 * output=SomeOutput::class
 * )
 */

Because I want to have custom operation made on it (part of data saved to DB via doctrine entity and part sent on queue) I created and registered MyCustomPersister to handle this. Inside this persister I do operations and in the end I return the created doctrine entity SomeEntity (because I want to have access to this entity when building the output - I want to return its id and status) and NOT the $data that was originally passed to persister.

I created SomeOutput class and created SomeOutputDataTransformer classes assuming they will be used to build output for the request and that the SomeEntity I returned form MyCustomPersister will be passed there but they are not used, the Transformer is not used at all. The SomeOutput structure is visible in SwaggerUI as a Response for this request but as result of the request the whole SomeEntity I return in MyCustomPersister is returned.

What am I missing? Where I went wrong? Is there a better concept of doing this?

What I want to achieve is have a non entity ApiResource for which part of it will be saved as entity to DB and part of it sent to queue and in the end I want to return to user 3 out of 5 fields from this newly created entity.

标签: api-platform.com

解决方案


推荐阅读