エンティティを 'PATCH'メソッドで更新したい(サブミットされたフィールドのみを更新する)。FOSRestBundle - PATCHが正常に動作しない
/* Edit an existing Content entity.
*
* @Rest\Patch(
* "/{content}.{_format}",
* requirements={"content" = "\d+"},
* defaults = { "_format" = "json" }
*)
*
* @Rest\View(serializerGroups={"user""admin"})
* @param Content $content
* @return View
* @throws \NotFoundHttpException*
*
* @ApiDoc(
* resource="/api/content/",
* description="Updates content data",
*
* input="ContentBundle\Form\ContentType",
*
* output={
* "class"="EntriesBundle\Entity\Content",
* "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"},
* "groups"={"user","admin"}
* }
*)
*/
public function editAction(Request $request, Content $content)
{
if (!$content) {
throw $this-createNotFoundException();
}
$editForm = $this-createForm('ContentBundle\Form\ContentType', $content);
$editForm-submit($request-request-get($editForm-getName()));
$view = View::create()
-setSerializationContext(SerializationContext::create()-setGroups(['user']));
if ($editForm-isSubmitted() && $editForm-isValid()) {
$em = $this-getDoctrine()-getManager();
$em-persist($content);
$em-flush();
$view
-setStatusCode(Codes::HTTP_OK)
-setTemplate("ContentBundle:content:show.html.twig")
-setTemplateVar('contents')
-setData($content);
} else {
$view
-setStatusCode(Codes::HTTP_BAD_REQUEST)
-setTemplateVar('error')
-setData($editForm)
-setTemplateData(['message' = $editForm-getErrors(true)])
-setTemplate('ContentBundle:content:show.html.twig');
}
return $this-get('fos_rest.view_handler')-handle($view);
}
フォーム:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class)
->add('description', TextareaType::class)
->add('eng', CheckboxType::class, [
'required' => false
]);
}
私は 'ENG' TRUE
に設定してエンティティを持っています。 title
フィールドのみを更新するクエリを実行すると、eng
はfalse
に変更されます。 description
をnullに設定します。なぜどんなアイデア?
PATCH要求を持つとき、あなたは2番目の引数として 'false'をを渡すことになるでしょう不足しているプロパティがクリアされないように 'Form :: submit()'に追加します。 – xabbuh
PATCHを使用して真偽値をfalseに更新すると、機能しない可能性があります。詳細と解決方法についてはこの質問をご覧ください。 http://stackoverflow.com/questions/35187867/symfony3-how-to-update-boolean-to-false-using-patch-method – Roubi