Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 593
Marc Baumann Avatar asked Apr 16 '26 21:04

Marc Baumann


1 Answers

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.

like image 186
maphe Avatar answered Apr 19 '26 16:04

maphe