Answer the question
In order to leave comments, you need to log in
How to change entities without forms?
I'm trying to implement changing entities. The controller looks like this:
/**
* @param Product $slug
* @param Product $updatedProduct
* @return Product
*
* @Rest\Route(requirements={"slug" = "\d+"})
* @ParamConverter(
* name="updatedProduct",
* converter="fos_rest.request_body",
* class="FiveToFive\ergil\DomainBundle\Entity\Product"
* )
* @Rest\View(serializerGroups={ "Default", "product_details", "product_categories_list", "image_details" })
*/
public function putAction(Product $slug, Product $updatedProduct)
{
$productService = $this->get("product.service");
return $productService->update($updatedProduct);
}
// PUT http://my-api-uyl/api/products/1
{
"id": 1,
"title": "Updated some sort of macgick"
}
@ParamConverter(
name="updatedProduct",
converter="fos_rest.request_body",
class="FiveToFive\ergil\DomainBundle\Entity\Product",
options= { "id" = "slug" }
)
Answer the question
In order to leave comments, you need to log in
The correct way is to not use entities in controllers at all, and never feed them to JMS Serializers or forms.
A more or less correct way is to define your own Object Constructor for the JMS Serializer to make it possible to specify which object to shove data into.
If you remove "id" in the request body...then the PUT method should create the resource. Therefore, we return the result of the postAction method
public function putAction(Product $slug, Product $updatedProduct)
{
if (!$updatedProduct->getId()) {
return $this->postAction(/* $slug | $updatedProduct */);
}
return $this->get('product.service')->update($updatedProduct);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question