question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How do I create custom GET and POST without having it going to the database

See original GitHub issue

Hi 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:closed
  • Created 5 years ago
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
leonzu21commented, Aug 25, 2022

@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:

collectionOperations: [
    'get_device' => [
        'method' => 'POST',
        'path' => '/get-device',
        'controller' => GetDeviceByDeviceId::class,
        'defaults' => [
            '_api_persist' => false,
        ],
    ],
],
itemOperations: [],)]
1reaction
ahmed-bhscommented, May 10, 2020

@cve

I found a solution to skip persist listener:

            // skip persist Listener
            $request->attributes->set('_api_persist', false);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Create a new database - Microsoft Support
It explains how to create a desktop database by using a template, and how to build a database from scratch by creating your...
Read more >
WordPress Custom Post Types: The All-In-One Guide ... - Kinsta
Learn exactly what WordPress custom post types are, how they're different from standard posts, and how to create and use them to extend...
Read more >
Pure JavaScript Send POST Data Without a Form
You can send it and insert the data to the body: var xhr = new XMLHttpRequest(); xhr.open("POST", yourUrl, true); xhr.
Read more >
How to Create Custom Post Types in WordPress - WPBeginner
By querying the database, you can retrieve items from a custom post type. You will need to copy the following code snippet into...
Read more >
Working with forms - Django documentation
This tells the browser to return the form data to the URL /your-name/ , using the POST method. It will display a text...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found