Skip to content

Use it in a Symfony controller

matteosister edited this page Dec 6, 2014 · 4 revisions

Next (Register your own handlers) ►

Here is an example Symfony controller (with FOSRestBundle)

namespace Cypress\TestBundle\Controller;

use Cypress\TestBundle\Entity\Book;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Routing\ClassResourceInterface;
use FOS\RestBundle\Controller\Annotations as Rest;

/**
 * @Rest\View(serializerGroups={"books"})
 */
class BooksController extends FOSRestController implements ClassResourceInterface
{
    /**
     * @Rest\Route("/books")
     */
    public function patchBooksAction()
    {
        $books = $this->get('doctrine.orm.entity_manager')->getRepository(Book::class)->findAll();
        $this->get('patch_manager')->handle($books); // <- patch manager here!
        $this->get('doctrine.orm.entity_manager')->flush();
    }
}

And here is the relative request:

PATCH /books

[{"op": "data", "property":"owned", "value": true},{ "op": "set_as_read" }]

Here we are setting the property owned to true and executing a custom "set_as_read" operation on a collection of books.

The patch manager is responsible only for the operation handling. No magic or WTF at all.

It's up to you fetching the objects from your data layer (doctrine or not, db or not) and then persist it after the patch operation. The Patch Manager operates only at the object level.

Next (Register your own handlers) ►