Marc Baumann
Marc Baumann

Reputation: 77

Symfony2 create Entity from Request

I'm whondering, wheather there is an easy way to pupolate doctrine entities from request objects. I'm building a RESTful API with fos/rest-bundle, so I dont need forms.

Do you know a good way to do this, in a very easy and short way?

// POST /api/products
public function postProductsAction(Request $request)
{
    $product = new Product(); 
}

In addition, I'm whondering wheather its possible to inject instances of entities directly in the controller with post requests.

// PUT /api/product/1
// I need this functionality for post requests too
public function putProductAction(Product $product)
{
    return $product; // { "id" : "1", "name" : "foo" } 
} 

Greetings,

--marc

Upvotes: 3

Views: 2056

Answers (1)

maphe
maphe

Reputation: 1931

What you need is the most common goal of every REST API. And the best way to do this is to use a serializer, in addition to forms (even if you would prefere to not use forms).

I advise you to read for example this tutorial writen by William Durand. It explains every points very well and uses the JMSSerializerBundle to convert entities through the API.

Upvotes: 3

Related Questions