[Question] How do I create custom GET and POST without having it going to the database
See original GitHub issueHi guys.
I am trying to create a write cache in redis that will contain creations and edits on a doctrine (postgresql) entity. This means that my entity is marked as a doctrine entity because I need to be able to select it in different ways. I tried creating custom actions with the __invoke function, custom controllers and custom data persisters but no matter what I try the API Platform always persists and fetches the object from the database while running my code as well.
But I dont want the PUT and POST to actually write to the database and not fetch the object afterwards from the database. I would still like to get the data normalized into an object.
Any idea how I would be able to do this?
I tried to set the _api_recieve to false and manually grab the body from the request and then return the object from the controller but then it creates the entity in the database again even thought I have no persist in the function to the database.
App\Entity\Consent:
collectionOperations:
post:
method: 'POST'
path: '/identities/consents'
controller: 'App\Controller\ConsentOperations::persist'
normalization_context: {'groups': ['post']}
defaults:
_api_receive: false
Controller
public function persist()
{
$data = json_decode($this->requestStack->getCurrentRequest()->getContent());
$consent = new Consent();
$consent->setData($data);
$redisKey = $consent->getSub().'_'.time();
$this->client->hmset($consent->getSub(), [
'sub' => $consent->getSub(),
'version' => $consent->getVersion(),
'createdAt' => $consent->getCreatedAt()
]);
$this->client->zadd('consentQueue', [$redisKey => time()]);
return $consent;
}
Consent Entity
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\ConsentRepository")
* @ORM\HasLifecycleCallbacks
* @ApiResource()
*/
class Consent
{
// REST OF THE CLASS
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:14 (5 by maintainers)

Top Related StackOverflow Question
@ahmed-bhs
Thank you for your suggestion, you sent me on the right track.
Similarly I was able to obtain the same behavior by setting the parameter in the custom operation definition, like so:
@cve
I found a solution to skip persist listener: